Learn inheritance in java

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

Inheritance means accessing all the data (instance variables and methods) from one class (super class) to another class (subclass). Inheritance represent “IS-A” relationship between super class and subclass.

Syntax:

class SubClass extends SuperClass
{
   // code
}

A class in java can be extended and 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, let’s create a class which can calculate area of shape. It can be a rectangle, square, circle or triangle.

For instance, let’s create a class called polygon. Because polygon shape share rectangle shape and triangle shape.

From these two shapes we have similar property and that property is width and height. So we can define two variables in polygon class and set width and height 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 class rectangle and class 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 these classes are going to share inheritance property??

The answer is, the class you are making is having a “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 class share 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());
   }
}

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

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

// Triangle class is subclass
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 super class (base class) and class rectangle is called subclass (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 superclass to a subclass then all its protected members and public members are accessible in subclass, 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 class rectangle 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 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


Types of inheritance

There are five types of inheritance out of which three are supported they are single, multilevel and hierarchical inheritance.

Remaining two multiple and hybrid inheritance can be implemented through interfaces.

Learn inheritance in java

NOTE : For understanding purpose in the above figure I have used “Parent” and “Grand Parent” for super class and “child” for subclass.

Single inheritance : here subclass extends functionality of one superclass.

class Parent
{
   public void displayHello()
   {
      System.out.println("Hello");
   }
}

class Child extends Parent
{
   public void displayWorld()
   {
      System.out.println("World");
   }
}

public class SingleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj = new Child();
      obj.displayHello();
      obj.displayWorld();
   }
}

Output:

Hello
World


Multilevel inheritance : this inheritance represent hierarchical order where “Parent” is the subclass for super class “Grand Parent” and “child is the subclass for super class “Parent”.

Here subclass itself becomes superclass for another class. Therefore “Child” is inheriting the properties of “Parent” and “Parent” is inheriting the properties of “Grand Parent”.

class GrandParent 
{ 
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 
} 

class Parent extends GrandParent 
{ 
   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
} 

class Child extends Parent 
{ 
   public void displayJava() 
   { 
      System.out.println("Java"); 
   } 
}

public class MultilevelInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj = new Child();
      obj.displayHello();
      obj.displayWorld();
      obj.displayJava();
   }
}

Output:

Hello
World
Java


Hierarchical inheritance : here subclass “Child1”, “Child2” and “Child3 are inheriting the properties of superclass “Parent”.

class GrandParent 
{ 
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 
} 

class Parent extends GrandParent 
{ 
   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
} 

class Child extends Parent 
{ 
   public void displayJava() 
   { 
      System.out.println("Java"); 
   } 
}

public class HierarchicalInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj1 = new Child();
      obj1.displayHello();
      Parent obj2 = new Parent();
      obj2.displayWorld();
      obj1.displayJava();
   }
}

Output:

Hello
World
Java


Multiple inheritance : in this, using one subclass there will be “n” number of super classes. This will result in a bug so java does not support multiple inheritance.

Also read – interface in java

Multiple inheritance can be achieved using interfaces. Since interfaces have only abstract methods with no body and implementation class is accountable to provide unique implementation for abstract method.

interface A 
{ 
   public void displayHello(); 
} 

interface B 
{ 
   public void displayWorld(); 
} 

interface C extends A, B 
{ 
   public void displayHello(); 
}

class Demo implements C 
{ 
   @Override
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 

   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
}

public class MultipleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      obj.displayHello();
      obj.displayWorld();
      obj.displayHello();
   }
}

Output:

Hello
World
Hello

Hybrid inheritance : is a combination of hierarchical and multiple inheritance. Like multiple inheritance, hybrid inheritance can be achieved through interfaces.

Related Posts