Types of Inheritance in Java

In this tutorial, we will learn about the types of inheritance in Java with the help of simple and clear example programs. Before moving on to the types of inheritance, let us first take a brief look at inheritance.

What is Inheritance in Java?


The term inheritance refers to the mechanism by which one class inherits the properties and behaviors of another class. It is one of the most important and fundamental concepts of object-oriented programming (OOP) in Java.

Inheritance allows one class (called subclass / child class) to acquire the properties (variables) and behaviors (methods) of another class (called superclass / parent class). It is mainly used for code reusability. This means code once is written, can be used again and again in the new classes.

Basic Syntax of Inheritance

accessModifier class subclassName extends superclassName
{
  // Variables of subclass
  // Methods of subclass
}

The keyword extends is used for making the relationship between two classes. It indicates that you are deriving a new class (derived class) from the existing class (base class). Once the relationship is done, the subclass can automatically reuse the code of the superclass.

Types of Inheritance in Java


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

  • Single level inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple inheritance
  • Hybrid inheritance

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

Three types of inheritance in Java: single, multiple, and hybrid

Important Note:

Java does not support multiple inheritance and hybrid inheritance using classes due to ambiguity problems (Diamond Problem). However, these two types are supported through the interface only. So, in Java:

Supported using Classes

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance

Not supported using Classes

  • Multiple inheritance
  • Hybrid inheritance

Supported using Interfaces

  • Multiple inheritance
  • Hybrid inheritance

Single Level Inheritance in Java


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. This is the simplest and most commonly used type of inheritance in Java.

In single-level inheritance, there is only one base (parent) class and one derived (child) class. The derived class inherits all the accessible properties and behaviors of the superclass. This type of inheritance can be represented as shown in the figure below:
Single level inheritance in Java

Example 1: Single Level Inheritance

Let us take a very simple example program based on single-level inheritance in Java. In this example, we will create only one superclass and one subclass. The subclass will inherit instance method methodA() from the superclass.

package singleLevelInheritance; 
// Create a base class or superclass. 
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. 
class B extends A 
{ 
  public void methodB() 
  { 
     System.out.println("Child class method"); 
  } 
} 
// Main class
public class Myclass { 
public static void main(String[] args) 
{ 
  // Create an object of class B. 
     B obj = new B(); 
     obj.methodA(); // methodA() of class B will be called because by default, it is available in class B. 
     obj.methodB(); // methodB() of class B will be called. 
  } 
}

Output:

Base class method 
Child class method

In this example:

  • We have created a class A, which contains a single instance method methodA().
  • The statement “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 (superclass) of class B and class B is known as a derived class (child class).
  • In class B, we have defined one method methodB(). So, class B contains:
    • The inherited method methodA() from class A
    • Its own method methodB()

In the main method:

  • We have created an object of class B and called method methodA(), which is inherited from class A and methodB() defined in the class B.

Multilevel Inheritance in Java


A class that is extended by a class and that class is further 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 each level. At the next level, the derived class becomes the base class for the next derived class. This process can continue for multiple levels, forming an inheritance chain. This type of inheritance is represented as shown in the diagram below.

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).

Example 2: Multilevel Inheritance

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

package multilevelInheritance; 
class X 
{ 
   public void methodX() 
   { 
      System.out.println("Class X method"); 
   } 
} 
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

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 method methodY() of class Y is inherited directly from class Y in class Z but the method methodX() of class X is inherited indirectly from class X through class Y in class Z.

Multiple Inheritance in Java


Deriving a subclass from more than one superclass is known as multiple inheritance in Java. In other words, when a class inherits features from multiple parent types, it is referred to as multiple inheritance.

However, Java does not support multiple inheritance through classes. This means a class cannot extend more than one class in Java. Attempting something like this is illegal in Java:

class C extends A, B { } // Not allowed

Java supports only single inheritance through class where a class can extend only one class. Java supports multiple inheritance only through interfaces, where a class can implement multiple interfaces safely. Look at the figure below where class C is derived from two superclasses A and B.

Multiple inheritance in Java with Example

In multiple inheritance (using interfaces):

  • There can be more than one immediate parent interface.
  • There can be one or more implementing classes.

This approach avoids ambiguity problems such as the Diamond Problem.

Different Examples of Multiple Inheritance

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

Examples of Multiple inheritance in Java

Practically, multiple inheritance is rarely used and difficult to manage in software projects because it can create ambiguity, complexity, and confusion when a class inherits methods with the same method signature from more than one parent class. The ambiguity created by the multiple inheritance is known as the diamond problem in Java.

But, the functionality of multiple inheritance in Java can be achieved by using interfaces. However, the functionality of multiple inheritance in Java can be achieved by using interfaces.

When a class implements two or more interfaces that contain methods with the same signature, the class must provide its own implementation of that method. This requirement removes ambiguity and ensures clear behavior. Therefore, Java does not support multiple inheritance through classes, but it does support multiple inheritance through interfaces.

Now, you need to understand why Java does not support multiple inheritance through classes. This is a very common interview question, especially for freshers, and understanding this concept is important for Java interviews.

Why Java Does Not Support Multiple Inheritance Through Classes?


In Java, multiple inheritance through classes can lead to ambiguity and confusion for programmers. This happens when a class inherits methods with the same signature from more than one parent class. But in Java, one class cannot extend more than one class simultaneously. At most, one class can extend only one superclass.

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

Understanding the Problem with a Simple Scenario

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

Consider the scenario shown in the below diagram. In this scenario, there are three classes: A, B, and C.

  • Class A contains a method msg().
  • Class B also contains a method with the same signature.
  • 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, this 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.

The Java compiler would not be able to decide which version of the method msg() should be inherited by class C. This ambiguity is known as the Diamond Problem. Because of this problem, Java does not allow a class to extend more than one class.

Understand Programmatically

Let us understand programmatically why Java does not support multiple inheritance using classes.

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(); // Ambiguous: which msg() method should be called? 
 } 
}

Output:

Compile time error

In this example:

  • Class A and class B both define a method named msg() with the same method signature.
  • Class C tries to extend both class A and class B.

If this were allowed, the Java compiler would not be able to determine which msg() method should be invoked when obj.msg() is called. This situation creates ambiguity, which is known as the Diamond Problem in Java.

Because of this ambiguity:

  • Java does not allow a class to extend more than one class.
  • The code fails at compile time.

How Is Multiple Inheritance Implemented in Java?


Multiple inheritance in Java can be implemented by using interfaces. A class cannot extend more than one class, but it can implement more than one interface. Let us see how multiple inheritance is achieved using interfaces in Java.

Example 3: Multiple Inheritance by Interfaces

package multipleInheritancebyInterface;
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. 
} 
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

In this example:

  • We have created two interfaces: Printable and Showable.
  • Each interface declares one abstract method.
  • Class A implements both interfaces, thereby achieving multiple inheritance.
  • The class A provides concrete implementations of all abstract methods.
  • In the main() method, we create an object of class A and call both methods.

Thus, we can implement multiple inheritance in Java by using interfaces.

Hierarchical Inheritance in Java


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 diagram below 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.

Example 4: Hierarchical Inheritance

Let us 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.

package hierarchicalInheritanceEx; 
class A 
{ 
  public void msgA() 
  { 
     System.out.println("Method of class A"); 
  } 
} 
class B extends A 
{ 
   // Empty class B, inherits msgA of parent class A.
} 
class C extends A 
{ 
  // Empty class C, inherits msgA of parent class A. 
} 
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

Example 5: Hybrid Inheritance

package hierarchicalInheritanceEx;
// Base interface
interface A {
    void methodA();
}
// Interface B extends interface A
interface B extends A {
    void methodB();
}
// Interface C extends interface A
interface C extends A {
    void methodC();
}
// Class D implements both B and C
class D implements B, C {
    public void methodA() {
        System.out.println("Method A");
    }
    public void methodB() {
        System.out.println("Method B");
    }
    public void methodC() {
        System.out.println("Method C");
    }
    public static void main(String[] args) {
        D obj = new D();
        obj.methodA();
        obj.methodB();
        obj.methodC();
    }
}

Output:

Method A
Method B
Method C

In this example:

  • Interface A declares methodA().
  • Interfaces B and C extend interface A.
  • Class D implements both B and C.
  • Class D provides implementations for all abstract methods.

This example combines:

  • Multilevel inheritance (A → B, A → C)
  • Multiple inheritance (D implements B and C)

Hence, this is called hybrid inheritance.

Note:
  • In real-world Java projects, developers generally use three types of inheritance in Java. They are single inheritance, multilevel inheritance, and hierarchical inheritance.
  • Developers do not work with multiple inheritance and hybrid inheritance through classes in Java because Java does not support them using classes.
  • Multiple inheritance is supported in C++ using classes, but it is not supported in Java through classes.
  • Although some Java programmers may want to use multiple inheritance, Java deliberately avoids it to prevent ambiguity, complexity, and maintenance issues. Instead, Java provides interfaces as a safe alternative.

Key Points for Types of Inheritance in Java


Inheritance is one of the most fundamental concept in Java OOPs that allows one class to inherit properties and behaviors from another class. Here are the types of inheritance in Java explained through key points:

  • Single Inheritance:
    • A class inherits from only one superclass.
    • Helps in reusing the code and implementing polymorphism.
  • Multilevel Inheritance:
    • In Multilevel inheritance, a class is derived from a class, which is also derived from another class.
    • Creates a chain of inheritance.
    • Allows a class to inherit features from all the classes in the hierarchy.
  • Hierarchical Inheritance:
    • Multiple classes inherit from a single superclass.
    • Useful when multiple classes need to share the same attributes and methods of a common superclass.
  • Multiple Inheritance (Through Interfaces):
    • Java does not support multiple inheritance with classes to avoid complexity and ambiguity (like the Diamond Problem).
    • Achieved through interfaces, where a class can implement multiple interfaces simultaneously.
  • Hybrid Inheritance:
    • A combination of two or more types of inheritance, such as hierarchical inheritance and multiple inheritance.
    • Does not directly supported in Java due to the lack of support for multiple inheritance with classes.
    • Can be implemented using interfaces to achieve desired behavior.

Here, you have learned different types of inheritance supported in Java with the help of example programs. We hope that you will have understood the basic concepts of each type of inheritance and practiced all example programs.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the founder of Scientech Easy and a passionate coding educator with 8 years of professional experience in Java, Python, web development, and core computer science subjects. With expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He consistently publishes in-depth tutorials, practical coding examples, and valuable learning resources for beginners as well as working professionals. Each article is carefully researched and reviewed to ensure accuracy, quality, and real-world relevance.