Today You will learn, Why Multiple Inheritance is not allowed in Java. In Java, Deadly Diamond of Death is a condition of ambiguity which arises due to Multiple Inheritance. The phrase comes from the shape of the class diagram.
Java does not support Multiple Inheritance. Multiple Inheritance is supported by C++ because C++ use pointers so it’s acceptable in C++, Java avoids it.
The reason for avoiding multiple Inheritance is the Deadly Diamond of Death. Actually, the class diagram that is formed in this scenario, is that of a diamond, and it is a no solution output, so the code gets locked, so it is called Deadly Diamond of Death.
Multiple Inheritance in Java
Java supports the Multiple inheritance but indirectly, you can use multiple inheritance using Interface in Java. Thus, the Java language does support multiple implementations, but not multiple Inheritance. So a class can implement many Interfaces, but can not extend more than one class.
Let’s discuss the problem by taking an example. Suppose that Java supports Multiple Inheritance (though it does not, still you can assume it to understand the current concept). Just go through the following points for a clearer understanding –
- Suppose we have a super class, Class A with printValue() method in it.
- Now two concrete class Class B and Class C extend this super class, and override the method in the superclass, in two different ways.
- Now fourth class Class D comes into picture which again extends both the classes Class C and Class D (this is multiple inheritance) . So now by the principle of inheritance class D inherits all the methods of the parent class, but we have a common method printValue() in both of the super classes but with different implementations, so which implementation will be used for the last child class Class D which inherits both these classes?
Actually, no one has got the answer to the above question, and so to avoid this sort of critical issue, Java banned Multiple Inheritance. The class diagram which is formed above is like that of a diamond, but with no solution or outcome, and so it is called Deadly Diamond of Death.
Selenium Testing Video Tutorial: