Types of Inheritance in java

Posted By on September 16, 2014


Download PDF
Java Character Class
Cosmic Class in Java

Below are Various types of inheritance in Java. We will see each one of them one by one with the help of examples and flow diagrams.

1) Single Inheritance

Single inheritance is damn easy to understand. When a class extends another one class only then we  call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be  a child class of A.

Single Inheritance Types of inheritance in Java: Single,Multiple,Multilevel & Hybrid

Single Inheritance example program in Java

Class A
{
   public void methodA()
   {
     System.out.println("Base class method");
   }
}

Class B extends A
{
   public void methodB()
   {
     System.out.println("Child class method");
   }
   public static void main(String args[])
   {
     B obj = new B();
     obj.methodA(); //calling super class method
     obj.methodB(); //calling local method
  }
}

2) Multiple Inheritance

Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.

Multiple Inheritance Types of inheritance in Java: Single,Multiple,Multilevel & Hybrid

Note 1: Multiple Inheritance is very rarely used in software projects. Using Multiple inheritance often leads to problems in the hierarchy. This results in unwanted complexity when further extending the class.

Note 2: Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.

3) Multilevel Inheritance

Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class. As you can see in below flow diagram C is subclass or child class of B and B is a child class of A. For more details and example refer – Multilevel inheritance in Java.

Multilevel Inheritance Types of inheritance in Java: Single,Multiple,Multilevel & Hybrid

Multilevel Inheritance example program in Java

Class X
{
   public void methodX()
   {
     System.out.println("Class X method");
   }
}
Class Y extends X
{
public void methodY()
{
System.out.println("class Y method");
}
}
Class Z extends Y
{
   public void methodZ()
   {
     System.out.println("class Z method");
   }
   public static void main(String args[])
   {
     Z obj = new Z();
     obj.methodX(); //calling grand parent class method
     obj.methodY(); //calling parent class method
     obj.methodZ(); //calling local method
  }
}

hierarchical inheritance?

Hierarchical diagram Hierarchical Inheritance in java with example program

As you can see in the above diagram that when a class has more than one child classes (sub classes) or in other words more than one child classes have the same parent class then such kind of inheritance is known as hierarchical.

Let’s have a look at the below example program to understand it better –

I’m using the above figure for implementing hierarchical inheritance in the below example-

Class A
{
  public void methodA()
  {
     System.out.println("method of Class A");
  }
}
Class B extends A
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
}
Class C extends A
{
 public void methodC()
 {
 System.out.println("method of Class C");
 }
}
Class D extends A
{
  public void methodD()
  {
     System.out.println("method of Class D");
  }
}
Class MyClass
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     D obj3 = new D();
     obj1.methodA();
     obj2.methodA();
     obj3.methodA();
  }
}

The above would run perfectly fine with no errors and the output would be –
method of Class A
method of Class A
method of Class A

Hybrid Inheritance in Java

Hybrid diagram hybrid inheritance in java with example program

As you can see in the above diagram that it’s a combine form of single and multiple inheritance. Since java doesn’t support multiple inheritance, the hybrid inheritance is also not possible.

Case 1:  Using classes: If in above figure B and C are classes then this inheritance is not allowed as a single class cannot extend more than one class (Class D is extending both B and C). Reason explained below!!

Case 2: Using Interfaces: If B and C are interfaces then the above hybrid inheritance is allowed as a single class can implement any number of interfaces in java.

Let’s understand the above concept with the help of examples:

Example program 1: Using classes to form hybrid

public class A
{
     public void methodA()
     {
           System.out.println("Class A methodA");
      }
}
public class B extends A
{
      public void methodA()
      {
           System.out.println("Child class B is overriding inherited method A");
      }
      public void methodB()
      {
           System.out.println("Class B methodB");
       }
}
public class C extends A
{
      public void methodA()
      {
           System.out.println("Child class C is overriding the methodA");
       }
      public void methodC()
      {
           System.out.println("Class C methodC");
       }
}
public class D extends B, C
{
      public void methodD()
      {
           System.out.println("Class D methodD");
       }
       public static void main(String args[])
       {
              D obj1= new D();
              obj1.methodD();
              obj1.methodA();
       }
}

Output:

Error!!

Why? Most of the times you will find the following explanation of above error – Multiple inheritance is not allowed in java so class D cannot extend two classes(B and C).  But do you know why it’s not allowed? Let’s look at the above code once again, In the above program class B and C both are extending class A and they both have overridden the methodA(), which they can do as they have extended the class A. But since both have different version of methodA(), compiler is confused which one to call when there has been a call made to methodA() in child class D (child of both B and C, it’s object is allowed to call their methods), this is a ambiguous situation and to avoid it, such kind of scenarios are not allowed in java. In C++ it’s allowed.

What’s the solution? Hybrid inheritance implementation using interfaces.

interface A
{
     public void methodA();
}
interface B extends A
{
     public void methodB();
}
interface C extends A
{
     public void methodC();
}
class D implements B, C
{
    public void methodA()
    {
         System.out.println("MethodA");
    }
    public void methodB()
    {
         System.out.println("MethodB");
    }
    public void methodC()
    {
         System.out.println("MethodC");
    }
    public static void main(String args[])
    {
         D obj1= new D();
         obj1.methodA();
         obj1.methodB();
         obj1.methodC();
     }
}

Output:

MethodA
MethodB
MethodC

Note: Even though class D didn’t implement interface “A” still we have to define the methodA() in it. It is because interface B and C extends the interface A.

The above code would work without any issues and that’s how we implemented hybrid inheritance in java using interfaces.

 
Java Character Class
Cosmic Class in Java

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com