Learn Inheritance In Java

Hey guys how are you doing?!! Today we will learn inheritance in java.

In this post we will explore what is inheritance in java and how to use inheritance in java.

So basically 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

Learn Inheritance In Java

As you can see in the above diagram, let’s say for example, 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 i can define two variables in polygon class and set these width and height value in polygon class using a method set value.

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

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

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

Now the question arises, that is, how we can know that classes we are going to make share our 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 in between them.

So whenever some class inherits from some other class then that share is a relationship.

Let’s see code implementation of the above explanation,

public class Inheritance_Example {

public static void main(String[] args) {
 
Rectangle re = new Rectangle();
Triangle tri = new Triangle();
 
re.set_Values(10,40);
tri.set_Values(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 set_Values(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;
 
}

}

Also Read –¬†Area Of Rectangle Program In Java

What we are going to do is we are creating three classes namely polygon, rectangle and triangle.

Now in polygon class we have defined two variable which will be protected.

protected int height;
protected int width;

We have defined method which will be,

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

By this method we are going to set the values.

Now in rectangle class we are going to inherit from polygon class by using keyword “extends” like this,

public class Rectangle extends Polygon

So here rectangle class is inheriting from polygon class and polygon class is called as base class and rectangle class 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 rectangle class. 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 set_Values 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 can create the 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.set_Values(10,40);

In the same way we can access set_Value method from triangle class like this,

tri.set_Values(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 if you run the program this is the output you get,

Learn Inheritance In Java

conclusion:

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.

So i hope you have understood the basic concept of inheritance in java.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *