Which Inheritance is Not Supported in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Multiple inheritances using classes in Java is not supported. Java only allows for single inheritance, where a class can inherit from only one superclass. This decision was made to avoid the complexities that arise from multiple inheritances, such as issues with casting and constructor chaining. In practice, there are very few situations where multiple inheritances are truly necessary, so it is recommended to avoid them in order to keep the codebase simple and manageable.

One of the challenges with multiple inheritances is the Diamond problem. It occurs when a class A has a method called foo(), and classes B and C both inherit from A and provide their own implementations of foo(). If class D inherits from both B and C, there is ambiguity in determining which foo() method should be invoked when referring to foo() from class D. This inheritance structure resembles a four-edged diamond, hence the name "Diamond problem".

which-inheritance-is-not-supported-in-java-1

We'll still face this ambiguity problem even if we remove the top head of diamond class A which will ultimately lead to multiple inheritances.

Java avoids this ambiguity by supporting Interface Inheritance. The interface simply has a method declaration and no implementation, there will only be one implementation of a specific method, so there will be no ambiguity.

Multiple Inheritance In Java

Multiple Inheritance in java is an object-oriented concept that allows a class to inherit properties from more than one parent class. The issue arises when methods with identical signatures exist in both superclasses and subclasses. The compiler cannot identify which class method should be called or which class method should be prioritized when calling the method.

Multiple inheritances lead to ambiguity. For example, suppose there is a class named Sub and two classes named Super1 and Super2, containing a method named sample(). And if the class Sub inherits both Super1 and Super2, there will be two copies of the sampling method, one from each superclass, and it will be difficult to determine which method to use.

which-inheritance-is-not-supported-in-java-2

Multiple Inheritance Can be Achieved Using an Interface

An interface is similar to a class that has variables and methods, but the methods in an interface are abstract by default, unlike a class. In Java, multiple inheritance by interface occur if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.

Example: In this example, we will have a look at how a Java program demonstrates multiple inheritances through an interface.

  • Each interface, AnimalEat and AnimalTravel, declares one abstract method: eat() and travel() respectively.
  • The Animal class implements both AnimalEat and AnimalTravel interfaces.
  • In the main() method of the Demo class, an object of the Animal class is created.
  • The eat() and travel() methods of the Animal object are invoked, which execute the respective implementations provided by the Animal class.

Output of the above code:

Java Program to Illustrate Unsupportance of Multiple Inheritance

Example 1: In this example, we have two classes, Parent1, and Parent2, both of which contain the same function, fun() (with parameters). A copy of both fun() methods should be made in the subclass object according to the basic rule of inheritance, leaving the subclass with two methods with the same prototype. Then, if you use the subclass object to call the fun() method, the compiler faces an ambiguous situation and gets confused as to which method to call.

As a result, multiple inheritances are not permitted in Java, and you cannot extend more than one other class. Even so, attempting to do so will result in a compile-time error.

Output of the above code:

Example 2: In this example, we'll see how the diamond problem is generated by multiple inheritances. Problem arises when we use the Sub object to call the method fun(), such as whether to call Parent1's fun() or Parent2's fun() method. To avoid such complexities, Java does not support multiple-class inheritance.

As a result, when the fun() method is called, it throws another compiler error since multiple inheritances cause a diamond problem, which is acceptable in other languages such as C++`` and Common Lisp`.

which-inheritance-is-not-supported-in-java-5

Output of the above code:

Learn More

Conclusion

  • Multiple Inheritance occurs when the properties and behavior of multiple classes are passed down to a single class. C++ and Common Lisp are some popular languages that allow for multiple inheritances.
  • Multiple inheritances is not supported in Java to prevent the ambiguity that multiple inheritances can cause. The Diamond problem is one of the most typical problems caused by multiple inheritance.
  • If we wish to implement multiple inheritances in Java, we can do it by utilizing the interface concept in Java, which also eliminates the ambiguity problem.