The term inheritance refers that one class can inherit all of the properties and behavior from another class. It is used for the code reusability. That means code once is written, can be used again and again in the new classes. 

In the previous tutorial, we have explained inheritance in java with realtime examples and the best collection of interview programs with explanations. I recommend that once you definitely see, perhaps you will learn something different.

In this chapter, we are going to learn types of inheritance such as single, multilevel, multiple, hierarchical, and hybrid inheritance with practical example programs.

Types of Inheritance in Java

On the basis of class, there are three types of inheritance in Java.
1. Simple/Single level Inheritance
2. Multiple Inheritance
3. Hybrid Inheritance
The classification of inheritance in Java is shown in the below figure.

Types of inheritance in Java

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

Single level Inheritance in Java

When one class is extended by only one class, it is called single level inheritance. In single-level inheritance, we have just one base class and one derived class. The derived class inherits all the properties and behaviors only from a single class. It is represented as:

Single level inheritance in java

Single Inheritance Example Program in Java

Let's see a simple example program related to single inheritance in Java.
Program source code 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
In this example program, we have a class A in which a single function methodA(). The line class B extends A tells the compiler that B is a new class and we are inheriting class A in class B. 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 (i.e methodA() ) of class A and methodB(). In the main, we have created an object of class B and call the function methodA() which are inherited from class A and methodB().

Single inheritance is further classified as follows:
1. Multilevel Inheritance
2. Hierarchical Inheritance

Multilevel Inheritance in Java

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

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

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

Java Multilevel Inheritance Example Program

Program source code 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) { 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
The above program 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

One class has many superclasses is known as multiple inheritance. In other words, When a class extends multiple classes, it is known as multiple inheritance. 

In multiple inheritance, a subclass inherits from more than one immediate superclass. Java does not support multiple inheritance through class. Multiple inheritance is as shown in the below diagram.

Multiple inheritance in Java

Practically, It is very rare and difficult to use in the 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 the diamond problem. 

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. May get asked in the interview, especially for freshers.

Why multiple inheritance is not supported in java through class?

As shown multiple inheritance in the above diagram, one class extends two superclasses or base 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 class. Let's understand with a simple scenario of why java does not support multiple inheritance through the class.

Consider a scenario from the above diagram where there are three classes class A, class B, and class C. Class C extends two parent classes such as class A and class B. 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 you create an object of class C and call the method msg() from the child class object, which msg() method will get called?
Here, 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 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.
Program source code 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 were public static void main(String args[]) { 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 interfaces. Let's see how?
Program source code 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. } 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();; } }
    Output: Hello Java Welcome you
Thus, we can implement multiple inheritace in Java by using interfaces. If you have any problem to understand the above program, you don't worry. When you will learn interface in Java, you can easily understand this program.

Hierarchical Inheritance in Java

A class which is inherited by many subclasses is known as hierarchical inheritance. 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. Hierarchical inheritance is as shown in the below diagram.

Hierarchical inheritance in Java

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

Java Hierarchical Inheritance Example Program

Let's take one simple example program related hierarchical inheritance concept.
Program source code 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

Hybrid Inheritance in Java

A hybrid inheritance in Java is a combination of single and multiple inheritance. A typical flow diagram is shown in the below image. A hybrid inheritance 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. It is not allowed in Java.

Hybrid inheritance in Java

Since Java does not support multiple inheritance through the classes so the program source code for hybrid inheritance will be discussed when we will learn the interface chapter.

Note: Generally, we use three types of inheritance such as single inheritance, multilevel inheritance, and hierarchical inheritance in the project level. We do not work with multiple and hybrid inheritance.

Final words
Hope that this tutorial has covered almost all types of inheritance in java with practical example programs. I hope that you will have understood all types of inheritance and enjoyed them. Please, share it on social networking sites for your friends.
Thanks for reading!
Next ➤ Super Keyword in Java ⏪ Prev Next ⏩