Multiple Inheritance is a feature of object oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with same signature in both the super classes and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.
Consider the below Java code. It shows error.
From the code, we see that, on calling the method fun() using Test object will cause complications such as whether to call Parent1’s fun() or Parent2’s fun() method.
1. The Diamond Problem:
GrandParent / \ / \ Parent1 Parent2 \ / \ / Test
From the code, we see that: On calling the method fun() using Test object will cause complications such as whether to call Parent1’s fun() or Child’s fun() method.
Therefore, in order to avoid such complications Java does not support multiple inheritance of classes.
2. Simplicity – Multiple inheritance is not supported by Java using classes , handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there are very few scenarios on which we actually need multiple inheritance, so better to omit it for keeping the things simple and straightforward.
Java 8 supports default methods where interfaces can provide default implementation of methods. And a class can implement two or more interfaces. In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.
Default PI1 Default PI2
If we remove implementation of default method from “TestClass”, we get compiler error. See this for a sample run.
If there is a diamond through interfaces, then there is no issue if none of the middle interfaces provide implementation of root interface. If they provide implementation, then implementation can be accessed as above using super keyword.
This article is contributed by Vishal S. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Multiple Inheritance in C++
- Inheritance in Java
- Java | Inheritance | Question 2
- Java | Inheritance | Question 3
- Java | Inheritance | Question 1
- Using final with Inheritance in Java
- Java | Inheritance | Question 8
- Interfaces and Inheritance in Java
- Java | Inheritance | Question 4
- Delegation vs Inheritance in Java
- Java | Inheritance | Question 5
- Java | Inheritance | Question 7
- Inheritance and constructors in Java
- Java | Inheritance | Question 9
- Comparison of Inheritance in C++ and Java