Wednesday, July 13, 2011

Why multiple inheritances are not supported in Java

Why multiple inheritence is not supported implemented in javaRecently one of my friend appeared for an interview and after few so called easy questions he was asked "Why multiple inheritance is not supported in Java" , though he has a brief idea that in Java we can support multiple inheritance in java via interface but interviewer was keep pressing on why part , may be he was just read any blog post about it :). So after the interview my friend comes to me and in usual talk he told me about this questions and ask me the answer. Well this is very classical question like Why String is immutable in Java; similarity between these two questions is they are mainly driven by design decision taken by java's creator or designer. Though following two reason make sense to me on Why Java doesn't support multiple inheritances:

Why Java doesn't support multiple inheritance

1) First reason is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

           A foo()
           / \
          /   \
   foo() B     C foo()
          \   /
           \ /
            D
           foo()

In my opinion even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

Some times if you give this reason to interviewer he asks if C++ can support multiple inheritance than why not Java. hmmmmm in that case I would try to explain him the second reason which I have given below that its not because of technical difficulty but more to maintainable and clearer design was driving factor though this can only be confirmed by any of java designer and we can just speculate. Wikipedia link has some good explanation on how different language address problem arises due to diamond problem while using multiple inheritances.

2) Second and more convincing reason to me is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity. Also java avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.



Related post:

Please share with your friends if like this article

14 comments:

Javin said...

Thanks Nipuna

Anonymous said...

Similar questions "Why multiple inheritance is not possible in java" is asked to me on an interview, will your answer for Why multiple inheritance is not supported in java , applicable for my questions also ?

Anand said...

A Creative way of explaining the problem Javin. Great job!!!

MUKESH said...

Nice JOb Buddy... Keep it up...

vinoth said...

really very very super

mani said...

i think ambiguity is not a reason for that java doesn't support multiple inheritance because in C++ there is a solution for ambiguity........

Anonymous said...

He guys. multiple inheritance IS supported in Java!

You have to be more precies. It is not supported for classes, it is supported for interfaces.

maruti said...

Re: multiple inheritance Vs interface?

The following are the examples
First one uses the classes and the second one uses Interfaces
/***********************Using Classes*************/
class A
{
public void methodAClassA()
{
System.out.println("In ClassA MethodA");
}
}
class B extends A{
public void methodAClassA(){
System.out.print("in Class B Method ClassA");
}
}
class C extends A,B {
/*
* Which is not possible bcoz
* if u use the above code the compiler does not
* know which instace method could be used in the class C
* So they made it as error it will show the compilation error
*
*/
}
/******************Using Interfaces*******************/
interface A
{
public void methodAClassA();

}
interface B extends A{
public void methodAClassA();
}
class C implements A,B {
/*
* Which is possible bcoz
* if we use the above code there is no need of the
* knowing which method instace could be used
* so that the diamond problem doesnot occured
*/
}


I think now its clear how we achieve multiple inheritance in java.....

Michael said...

This interview question is absolutely worthless in determining if someone is a good java programmer. It is nothing more than trivia.

Favourites said...

thankz for ur clarification buddy

Anonymous said...

Hi Dude as i Know Multiple Inheritence is not supported in java just because of the Violation of super() keyword.
When Ever you called the method/constructor of super class at that time JVm got confused,it cant decide which method you are calling.its something similar to Diamond problem.

And if you need any more help please ask me at
ziaur18@gmail.com.

himayat zehgeer said...

is the definition of the multiple inheritance here enough

Anonymous said...

While both of above reasons are correct to a certain extent, the primary reason is that with inheritance, the inherited class is loaded in the memory each time it's child class object is created. If a class inherited multiple other classes, the memory and load time overhead would get out of control. Which certainly happens in poorly designed C++ applications

An interface implementation is bare bones of a design. More so a restriction upon the child class. With multiple interfaces, only the parts needed by the child class are ever put into it. So the child class can inherit "design restrictions" from multiple classes without any extra overhead at all.

Anonymous said...

Awesome post, thanks! your post combined with this post:

www.programmerinterview.com/index.php/java-questions/multiple-inheritance/

really helped my understanding of multiple inheritance

Post a Comment