Learn inheritance in java

Hey guys?!! Welcome to flower brackets blog. Today we will learn inheritance in java.

A class in java can be extended and this can be done by creating new classes which can retain the characteristics of the base class.

Base class I mean here is the class from which we are inheriting. Now to understand it completely let’s see an example,

Learn Inheritance In Java

As you can see in the above diagram, I will create a class which can calculate the area of shapes. So it can be rectangle, square, circle or triangle.

I can make a class called polygon. Because polygon shape share rectangle and triangle shape.

Hence a similar property and that property is width and height. So we can define two variables in polygon class and set these width and height value in polygon class using a method setValue().

Also read – learn abstraction in java

In class rectangle and triangle we can create two methods which can calculate area of rectangle and area of triangle.

The common properties which they both share is height and width and these are not required to define in separate places in class rectangle and triangle.

Also we can define them in single place and we can use them using the inheritance property.

Now the question arises, that is, how can we know that classes are going to share inheritance property??

The answer is, if the class you are making have “IS-A” relationship, that is, rectangle is a polygon and triangle is a polygon. So they share “IS-A” relationship between them.

So whenever some class inherits from some other class then that share is a relationship. Let’s see code implementation of above explanation,

public class InheritanceExample
{
   public static void main(String[] args)
   {
      Rectangle re = new Rectangle();
      Triangle tri = new Triangle();

      re.setValues(10,40);
      tri.setValues(20,30);
 
      System.out.println("Area of rectangle: " + re.area());
      System.out.println("Area of triangle: " + tri.area());
   }
}

public class Polygon
{ 
   protected int height;
   protected int width;
 
   public void setValues(int a,int b)
   {
      height = a;
      width = b;
   }
}

public class Rectangle extends Polygon
{ 
   public double area()
   {
      return (height * width);
   }
}

public class Triangle extends Polygon
{ 
   public double area()
   {
      return (height * width) / 2;
   }
}

What we are doing here is we are creating three classes namely polygon, rectangle and triangle.

Now in polygon class we have defined two variables which are protected.

protected int height;
protected int width;

And we have defined method,

public void setValues(int a,int b)
{ 
   height = a;
   width = b;
}

By this method we are going to set the values. Now in rectangle class we are inheriting polygon class by using keyword “extends” like this,

public class Rectangle extends Polygon

So here class rectangle is inheriting from class polygon and class polygon is called as base class and class rectangle is called as deriving class.

In the same way we have extended in the triangle class from polygon. In the next step we are going to calculate area in class rectangle.

Now whenever you are inheriting from a base class to a derived class then all its protected members and public members are accessible in derived class, that is, member variables height, width and setValues can be accessed by rectangle class because we are extending from it.

Now,

public double area()
{ 
   return (height * width);
}

We have not declared width and height inside rectangle class. This height and width in polygon class is accessible in rectangle class also.

In the similar way the area of the triangle,

public double area()
{ 
   return (height * width) / 2;
}

Now we have everything, that is, we have area of triangle we have area of rectangle and we are setting these value via polygon class.

In main method we create instance of rectangle class,

Rectangle re = new Rectangle();

And instance of triangle class,

Triangle tri = new Triangle();

Then for calculating the area we first need to set the value. So we will set the value of rectangle like this,

re.setValues(10,40);

In the same way setValue method from triangle class like this,

tri.setValues(20,30);

Now we just need to call the area. So we can just write like this,

System.out.println("Area of rectangle: " + re.area());
System.out.println("Area of triangle: " + tri.area());

Now after running the program this is the output you get,

Learn Inheritance In Java

Summary

So to conclude this topic, whenever we use inheritance all the property of the base class (polygon class) is available in derived class (rectangle and triangle class).

All the methods which are protected or public are accessible inside rectangle or triangle class.

So in this way you can extend the property of polygon class to the rectangle class and same is for triangle class.

Related Posts