Why Multiple Inheritance is Not Supported in Java?
Inheritance is the feature of Object-oriented programming language where a class can inherit the features of another class which helps in code reusability.
There are various types of inheritance in general like single-level, multi-level, multiple and hybrid. Unlike other Object-oriented programming languages, Java doesn't support multiple inheritance.
Consider a situation where a base class is used to create tow sub-classes Parent1 and Parent2. The base class has a method called sum() which will be inherited by the two sub-classes. If we use the concept of multiple inheritance further to create another class Child that is derived from Parent1 and Parent2.
The class Child will inherit the sum() method from both Parent1 and Parent2. Now when we create a Child class object and call the function sum(), the compiler gets confused about whether to call the sum() method of Parent1 or Parent2. This is the ambiguity problem faced by the compiler hence multiple inheritance is not supported in Java.
Example- Unsupportance of Multiple Inheritance
The above example is illustrated here.
Example-
Output-
This is the error that shows the ambiguity that happens in multiple inheritance in Java. That is why multiple inheritance is not supported in Java.
Diamond Problem in Java
The Diamond Problem in Java is a common problem arising due to multiple inheritance. Consider the example that we saw in the beginning.
Consider a situation where a base class Parent is used to create 2 sub-classes or derived classes Child1 and Child2. The base class has a method called sum() which will be inherited by the two sub-classes. If we use the concept multiple inheritance further to create another class GrandChild that is derived from Child1 and Child2. The class GrandChild will inherit the sum() method from both Child1 and Child2. Now when we create a GrandChild class object and call the function sum(), the compiler gets confused about whether to call the sum() method of Child1 or Child2. This is the ambiguity problem faced by the compiler hence multiple inheritance is not supported in Java. This is commonly known as the Diamond Problem as the inheritance diagram resembles a diamond.
Multiple Inheritance Through Default Methods
Now that we know Java doesn't support multiple inheritance, does it mean there is no other way to implement multiple inheritance? The answer is No. We can implement multiple inheritance through default methods using interface.
This was introduced in Java 8 where we were allowed to have methods with implementation without affecting the classes that implement interfaces.
They are the methods that are defined in the interface using the keyword "default". In the interface, we cannot add a method without using "default" or "static" methods.
If we have a default method in an interface, it is not mandatory to override it in the classes that are implementing it. But if a class implements two different interfaces but they have the same default methods, causes ambiguity. This is called the Diamond Problem.
How to Handle Diamond Problem in Case of Default Methods?
In Java 8, interfaces provide a default implementation of methods so a class can implement two or more interfaces.
If a situation arises where the implemented interfaces contain default methods having the same method signature, the class which is implementing it should specify which method is to be used, or which of the default method should be overridden.
Example-
Output-
We can see that we have used multiple inheritances in Java using interfaces. To handle the diamond problem, we have overridden the sum() method in the class which implements the interface. When we override the sum() method which is present in the implemented interfaces, the compiler gets to know that we have overridden this method.
Learn More
You can learn more about inheritance in Java along with its type in this article
Conclusion
- Multiple inheritance is a feature of Object-oriented programming language where a class can inherit more than one class.
- Java doesn't support multiple inheritances of classes.
- The Diamond Problem in Java is a common problem arising due to multiple inheritances. This is commonly known as the Diamond Problem as the inheritance diagram resembles a diamond.
- We can implement multiple inheritances through default methods using the interface. This was introduced in Java 8 where we are allowed to have methods with implementation without affecting the classes that implement interfaces. They are the methods that are defined in the interface using the keyword "default".