Types of Inheritance in Java with Example

In this tutorial, we will learn about types of inheritance in Java with the help of various example programs.

We know that the term inheritance refers to that one class can inherit all of the properties and behaviors from another class. Inheritance is used for code reusability. That means code once is written, can be used again and again in the new classes.

So far, we have familiarized with practical knowledge of Java inheritance and advantages of using inheritance in software development. If you have any difficulties in inheritance, go for the previous tutorials.

We have nicely explained the basic concepts of inheritance in Java with real-time practical examples. We have also covered the behaviors of access modifiers in the case of inheritance with various examples. Therefore, I recommend that once you definitely see them for practical knowledge. Now we move on to the main topic.

Types of Inheritance in Java


On the basis of class, there are three types of inheritance available in Java programming. They are as follows:

1. Simple/Single level Inheritance
2. Multiple Inheritance
3. Hybrid Inheritance

The classification of inheritance in Java is shown in the below figure.

Three types of inheritance in Java: single, multiple, and hybrid
In Java programming, multiple inheritance and hybrid inheritance are supported through the interface only. We will learn the interface in the further tutorial.

Single level Inheritance in Java with Example


When a class is extended by only one class, it is called single-level inheritance in Java or simply single inheritance.

In other words, creating a subclass from a single superclass is called single inheritance. In single-level inheritance, there is only one base class and can be one derived class.

The derived class inherits all the properties and behaviors only from a single class. It is represented as shown in the below figure:
Single level inheritance in Java

Let’s take a very simple example program based on single inheritance in Java.


In this example, we will create only one superclass and one subclass that will inherit instance method methodA() from the superclass. Look at the program code to understand better.

Example 1:

package singleLevelInheritance; 
// Create a base class or superclass. 
public class A 
{ 
// Declare an instance method. 
   public void methodA() 
   { 
      System.out.println("Base class method"); 
   } 
} 
// Declare a derived class or subclass and extends class A. 
   public class B extends A 
   { 
      public void methodB() 
      { 
         System.out.println("Child class method"); 
      } 
   } 
public class Myclass { 
public static void main(String[] args) 
{ 
  // Create an object of class B. 
     B obj = new B(); 
     obj.methodA(); // methodA() of B will be called because by default, it is available in B. 
     obj.methodB(); // methodB() of B will be called. 
  } 
}
Output: 
      Base class method 
      Child class method

Explanation:

In this example program, we have created a class A which contains a single function methodA(). The line “class B extends A” tells Java compiler that B is a new class inheriting from class A. This makes class A as the base class of class B and class B is known as a derived class.

In class B, we have defined one function i.e. one method methodB(). Class B contains inherited members ( methodA() ) of class A and methodB(). In the main method, we have created an object of class B and called functions methodA() which is inherited from class A and methodB().

Single inheritance is further classified into types that are as follows:

1. Multilevel Inheritance
2. Hierarchical Inheritance

Multilevel Inheritance in Java with Example


A class that is extended by a class and that class is extended by another class forming chain inheritance is called multilevel inheritance in Java.

In multilevel inheritance, there is one base class and one derived class at one level. At the next level, the derived class becomes the base class for the next derived class and so on. This is as shown below in the diagram.

Multilevel inheritance in Java with Example

As you can see in the above diagram, class A is base class of class B (derived class), class B is the base class of class C (derived class).

Let’s take a very simple example program based on multilevel inheritance in Java.

Example 2:

package multilevelInheritance; 
public class X 
{ 
   public void methodX() 
   { 
      System.out.println("Class X method"); 
   } 
} 
public class Y extends X 
{ 
   public void methodY() 
   { 
      System.out.println("Class Y method"); 
   } 
} 
public class Z extends Y 
{ 
   public void methodZ() 
   { 
      System.out.println("Class Z method"); 
   } 
public static void main(String[] args) 
{ 
 // Creating an object of class Z.
    Z z = new Z(); 
    z.methodX(); // Calling X grand class method. 
    z.methodY(); // Calling Y parent class method. 
    z.methodZ(); // Calling Z class local method. 
 } 
}
Output: 
       Class X method 
       Class Y method 
       Class Z method

Explanation:

The above program code demonstrates multilevel inheritance in Java. Class X is the parent class (base class) for class Y (child class) and class Y is the parent class for class Z (child class).

Thus, class X is the grandfather of class Z (grandchild). The function methodY of class Y is inherited directly from class Y in class Z but the function methodX of class X is inherited indirectly from class X through class Y in class Z.

Multiple Inheritance in Java with Example


Deriving subclasses from more than one superclass is known as multiple inheritance in Java. In other words, when a class extends multiple classes, it is known as multiple inheritance.

In multiple inheritance, there can be more than one immediate super class and there can be one or more subclasses. Java does not support multiple inheritance through class. It supports only single inheritance through class.

Look at the below figure where class C is derived from two superclasses A and B.

Multiple inheritance in Java with Example

Now, look at the different examples of multiple inheritance in the below figure to understand better.

Examples of Multiple inheritance in Java

Practically, it is very rare and difficult to use in a software project because it creates ambiguity, complexity, and confusion when a class inherits methods from two superclasses with the same method signature. The ambiguity created by the multiple inheritance is called diamond problem in Java.

But, the functionality of multiple inheritance in Java can be achieved by using interfaces. When two or more interfaces are implemented by a single class, only one method can share the method signature.

Now, you need to understand why Java does not support multiple inheritance through class. May get asked in the interview, especially for freshers.

Why Java does not Support Multiple Inheritance through Classes?


In Java, multiple inheritance leads to confusion for the programmer. This is because a class extends many super classes. But in Java, one class cannot extend more than one class simultaneously. At most, one class can extend only one class.

Therefore, To reduce the ambiguity, complexity, and confusion, Java does not support multiple inheritance through classes.

Let’s understand with a simple scenario of why Java does not support multiple inheritance through classes.

Consider a scenario as shown in the below diagram. In this scenario, there are three classes A, B, and C. Class C extends two parent classes such as class A and class B.

Why Java does not support multiple inheritance through class

Suppose class A and class B have the same method msg() (say) having different implementations.

As per inheritance concepts, both methods will be inherited to class C. If we create an object of class C and calls the method msg() from the child class object, which msg() method will get called? (i.e. which copy of msg() method will available in C?)

So, there will create ambiguity and confusion to call the method msg from A or B class.

Since the compile-time error is better than a runtime error, Java will give a compile-time error if you extend two classes. So, whether you have the same or different method signature still you will get compile-time error.

Let’s understand how Java does not support multiple inheritance programmatically.

Example 3:

class A 
{ 
   void msg() 
   { 
      System.out.println("Hello Java"); 
   } 
} 
class B 
{ 
   void msg() 
   { 
      System.out.println("Welcome you"); 
   } 
} 
class C extends A, B { // Suppose if it is.
public static void main(String args[]) 
{ 
 // Creating an instance of class C.
    C obj = new C(); 
    obj.msg(); // Now which msg() method would be called? 
 } 
}
Output: 
       Compile time error

How does Multiple Inheritance Implement in Java?


Multiple inheritance can be implemented in Java by using interfaces. A class cannot extend more than one class but a class can implement more than one interface. Let’s see how?

Example 4:

package multipleInheritancebyInterface; 

// Multiple Inheritance in Java by Interface. 
public interface Printable 
{ 
 // Declare an abstract method. Methods in an interface are public and abstract by default. So, it is not mandatory to write abstract keyword. 
 // Abstract method must not have a body. 
    void print(); // no body. 
} 
public interface Showable 
{ 
   void show(); // No body. 
} 

// Class A implementing multiple interfaces.
public class A implements Printable, Showable 
{ 
// The concrete class which implements an interface must implement the abstract method declared in the interface. 
   public void print() 
   { 
     System.out.println("Hello Java"); // Implementation. 
   } 
  public void show() 
  { 
     System.out.println(" Welcome you"); // Implementation. 
  } 

public static void main(String[] args) 
{ 
 // Create an object of class A and call print() and show() methods using object reference variable obj. 
    A obj = new A(); 
    obj.print(); 
    obj.show(); 
  } 
}
Output: 
       Hello Java 
       Welcome you

Thus, we can implement multiple inheritance in Java by using interfaces. If you have any problem to understand the above program, don’t worry. When you will learn interface in Java, you can easily understand this program.

Why achieving Multiple inheritance by using Interfaces is not so useful in Java?


By using interfaces, we can achieve multiple inheritance in an indirect way in java. But there is a lack of real flexibility provided by multiple inheritance. Suppose if we can achieve multiple inheritance through classes, complete methods are available that can be called and directly used in the subclasses.

But in the case of interface, all methods defined in interfaces will have no method body. So, programmers will have to provide a method body for all methods of the interfaces in the implementation class. Hence, In Java, implementing multiple inheritance by using interfaces is not so useful.

Hierarchical Inheritance in Java with Example


A class that is inherited by many subclasses is known as hierarchical inheritance in Java. In other words, when one class is extended by many subclasses, it is known as hierarchical inheritance.

In this kind of inheritance, one class can be a parent of many other classes. Look at the below diagram to understand hierarchical inheritance.

Hierarchical inheritance example

In the above diagram, class A is the parent (or base class) of all three classes B, C, and D. That is, classes B, C, and D inherit the same class A and can share all fields, methods of class A except private members.

Let’s take an example program where we will create multiple subclasses such as B, C, and D from a single superclass A. All subclasses will inherit msgA() method from class A. Look at the program code to understand better.

Example 5:

package hierarchicalInheritanceEx; 
public class A 
{ 
  public void msgA() 
  { 
     System.out.println("Method of class A"); 
  } 
} 
public class B extends A 
{ 
   // Empty class B, inherits msgA of parent class A.
} 
public class C extends A 
{ 
  // Empty class C, inherits msgA of parent class A. 
} 
public class D extends A 
{ 
  // Empty class D, inherits msgA of parent class A. 
} 
public class MyClass { 
public static void main(String[] args) 
{ 
 // Create the object of class B, class C, and class D. 
    B obj1 = new B(); 
    C obj2 = new C(); 
    D obj3 = new D(); 

 // Calling inherited function from the base class. 
    obj1.msgA(); 
    obj2.msgA(); 
    obj3.msgA(); 
  } 
 }
Output: 
       Method of class A 
       Method of class A 
       Method of class A

As you can observe in the above example, a single superclass is extended by more than one subclass and inherit the behavior of superclass.

Hybrid Inheritance in Java


A hybrid inheritance in Java is a combination of single and multiple inheritance. It can be achieved in the same way as multiple inheritance using interfaces in Java.

By using interfaces, we can achieve multiple as well as a hybrid inheritance in Java. But both are not allowed in Java. Typical hybrid inheritance is shown in the below diagram.

Hybrid inheritance


Note:

1. Generally at the project level, we use three types of inheritance in Java. They are single inheritance, multilevel inheritance, and hierarchical inheritance. We do not work with multiple and hybrid inheritance.

2. Multiple. inheritance is available in C++, but it is not available in Java. In some cases, Java programmers want to use multiple inheritance but due to not supporting, it leads to disappointment Java programmers.

⇐ PrevNext ⇒

Please share your love