Has it ever occurred to you why Java doesn't support Multiple Inheritance being developed in C and C++ which supports Multiple Inheritance?
Here's why.
- Assume there are 4 classes named class A, class B, class C and class D. B and C are derived from A and D is derived from both B and C using Multiple Inheritance.
- A will contain a method 'find()' which will be inherited by B and C.
- When we create an object from D and try to call this find() method as below,
D d = new D();
d.find();
JVM is confused as to which method to use!!! since both B and C contains an overridden find() method.
This is known as The Deadly Diamond of Death.
In languages like C and C++ you have to use other workarounds as a solution.
But since Java does not support Multiple Inheritance, this is not an issue.
However, you can have a similar implementation to Multiple Inheritance in Java using Interfaces.
If you wonder how the Diamond of Death is overcome by that, this is how,
Comments
Post a Comment