Multiple Inheritance in Java is not supported
For making Java language simple and avoid complexities present in earlier languages like c++, the founder of java(James Gosling) decided that java shouldn’t support Multiple Inheritance.
In a white paper titled “Java: an Overview” James Gosling gives an idea on why multiple inheritance is not supported in Java.
Gosling on omission of Multiple Inheritance in Java
“JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.”
Ambugity problem due to Multiple Inheritance
If we try to do multiple inheritance in java we will come across an ambiguity problem. We will discuss about it below using an example.
We have a super class A having a function called fun().
Two subclasses B and C extend class A providing their own implementation of fun() function.
Assume class D inherits from class B and class C.
If we create an object of class D and try to access function fun() guess what will happen.
i.e D obj=new D(); obj.fun();
This will lead to ambiguity problem. Because it’s difficult for java to decide which version of fun() function to execute, fun() of class B or fun() of class C. The above problem is also referred as diamond problem.
In c++ we have multiple inheritance and the problem is resolved using scope resolution operator. But java founders wanted to keep java simple and avoid complexities. Concepts like pointers, operator overloading are also avoided in java language for the same reason.
Multiple Inheritance in Java is not much required
Extending from two different classes having different implementations of a function is not much required in real world.
Leave a Reply