Multiple Inheritance in Java | Example Program

Deriving subclasses from multiple superclass is known as multiple inheritance in Java. In other words, when a single class inherits the properties of multiple superclasses, then we call it as multiple inheritance.

In multiple inheritance, there can be more than one immediate superclass and there can be one or more subclasses.

Some programming languages like C++, Python allow us to derive a subclass from multiple parent classes. We know this feature as multiple inheritance in object-oriented languages.

Java, however, does not allow this type of inheritance. This is because Java allows a class to extend one class and implement one or more interfaces.

Java supports only single inheritance through class and does not support multiple inheritance directly. It is a single inheritance language like Smalltalk, SOFL.

Look at the below figure where a class C extends two superclasses A and B.

Java does not support multiple inheritance

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

An example of multiple inheritance in Java

Practically, it is very infrequent and hard to use in a realtime software project because it creates ambiguity, complexity, and confusion when a class inherits methods from two superclasses with the same method signature.
[adinserter block=”5″]
The ambiguity generated by the multiple inheritance is called diamond problem in Java.

Java does not Support Multiple Inheritance through Classes


If a subclass inherits from more than one superclass, it receives all the accessible methods and member variables of the superclass. If we use multiple inheritance in Java, it can lead ambiguity, complexity, and confusion for the programmer.

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

Consider a scenario as shown in the below figure. In this scenario, there are three classes A, B, and C. Class C extends two parent classes such as class A and class B. Assume that class A and class B have the same method msg() (say) with different implementations.

Multiple inheritance is not supported by Java

According to inheritance concepts, both methods will inherit to class C. If we construct an object of class C and call the msg() method using the child class object reference, which msg() method will be invoked? (i.e. which copy of msg() method will be available in class C?)

In this situation, Java compiler will not be able to determine which of the methods in the parent class is to be called. Hence, it will create ambiguity and confusion in calling 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 we extend more than one class. So, whether you have the same or different method signature still you will get compile-time error.


Let us understand programmatically how does not Java support multiple inheritance.
[adinserter block=”2″]
Example 1:

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

As you can see in the above program, multiple inheritance leads to confusion for the programmer because class C extends more than one superclass.

Since Java is a single inheritance language, it does not allow a class to extend multiple classes simultaneously. To reduce the ambiguity, complexity, and confusion, Java does not support multiple inheritance directly through classes.

Another reason of not to support multiple inheritance is a performance. If a class extends only one superclass, JVM executes method invocations very quickly. But, class of which many superclasses, having the same method name, it becomes difficult to arrange memory for maximum efficiency.

Multiple Inheritance using Interface in Java


We can achieve the functionality of multiple inheritance in Java by using interface. A class cannot extend more than one class, but a class can implement multiple interfaces simultaneously.

In Java, interface allows us to define a method with no body (i.e. without implementation). Therefore, the child class cannot inherit implementation from interfaces.

So, there will be no ambiguity over which method to call. Besides it, when two or more interfaces are implemented by a single class, only one method can share the method signature.

Let’s write a simple program to implement multiple inheritance in Java through interfaces.

Example 2:

package multipleInheritance;
// Program to achieve the functionality of multiple inheritance through interface.
public interface Printable 
{
// Declare an abstract method. 
// Since methods in an interface are public and abstract by default. 
// So, it is not mandatory to write abstract keyword. 
   void print(); // an abstract method must not have a body.
}
public interface Showable 
{
  void show(); // no body. 
}
public class Test implements Printable, Showable 
{
// A concrete class which implements interfaces
// must implement all abstract methods defined in the interfaces. 
   public void print() 
  { 
     System.out.println("Hello"); // Implementation. 
  } 
  public void show() 
  { 
     System.out.println("Java"); // Implementation. 
  }
public static void main(String[] args) 
{
 Test t =  new Test();
  t.print();
  t.show();
 }
}
Output:
      Hello
      Java

In this example program, we have created two interfaces named Printable and Showable, respectively. Inside both interfaces, we have declared two methods named print() and show() with no implementations, respectively.

A class named Test implements both interfaces and provides implementations in both methods. Then, we have created an object of class Test and invoked both methods using reference variable t.

Thus, we can implement multiple inheritance in Java by using interfaces. If you are facing any difficulty in understanding the above code, don’t worry. When you will learn about interface in Java, you can easily understand the code.


Let’s take another example program for best practice to implement multiple inheritance through interface.

Example 3:

package multipleInheritance;
public interface Father 
{
 void msg1();
}
public interface Mother 
{
  void msg2();
}
public class Child1 implements Father, Mother 
{
 public void msg1() {
   System.out.println("Every father loves his child in the world.");
 }
 public void msg2() {
   System.out.println("Every mother loves his child in the world.");
 }
}
public class Child2 implements Father, Mother
{
 public void msg1() {
   System.out.println("I love my father very much.");
 }
 public void msg2() {
   System.out.println("I love my mother very much.");
 }
}
public class Execution {
public static void main(String[] args) 
{
 Child1 ch1 = new Child1();
  ch1.msg1();
  ch1.msg2();
 Child2 ch2 = new Child2();
  ch2.msg1();
  ch2.msg2();
  }
}
Output:
      Every father loves his child in the world.
      Every mother loves his child in the world.
      I love my father very much.
      I love my mother very much.

Why is implementing multiple inheritance by using Interface not so useful in Java?


By using interface, we can indirectly achieve multiple inheritance in Java programming. But there is a lack of real flexibility provided by multiple inheritance.

Assume that if we achieve multiple inheritance through classes, complete methods become available that can be called and directly used in the subclasses.

But with interface, all methods defined in interfaces will have no implementations. Therefore, programmers will have to provide an implementation for all methods of the interfaces in the implementation classes. Hence, achieving multiple inheritance by using interfaces is not so useful in Java.

⇐ Prev Next ⇒