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:

15 comments :

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 ?

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.

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

Gopal said...

Multiple Inheritance for classes is not supported in Java, but it does support multiple inheritance for interfaces. Main reason for not allowing multiple inheritance for classes is Deadly diamond of Death pattern( also known as DDD). As shown above, if class D class a method defined in both B and C, there is no way to identify which one to call, until a fully qualified name is used. Interface doesn't face problem of deadly diamond because they doesn't contain any behavior, behaviors are only defined in classes, all interface method is abstract, which means calling anyone has same effect. I remember, true reason for not supporting multiple inheritance is not technical difficulty but a thoughtful decision taken by Java community to avoid messy code, probably due to deadly diamond pattern.

toufeeq khan said...

multiple inheritance is not supported in java for classes and also for interfaces because the jvm can create only one object at a time.we cannot say that multiple inheritance is supported for interfaces by simply extending more than one interface at a time, forget about the multiple inheritance,that is not even inheritance because inheritance means using the properties of one class into another and we are not using the properties of interfaces, just providing implementations for the abstract methods of the interfaces. also we cannot create objects for interfaces...

Anonymous said...

is their any perfect answer for this question...all are giving so many reasons..,but whats is the correct answer

Anonymous said...

This is true that the diamond problem comes into play when the multiple inheritance is supported but there's a way around to assign a precedence. For example, in case of a conflict class A will have precedence over class B. But, the efforts required to implement this doesn't justify with the benefits will be achieved through multiple inheritance.

Take an example of source control that supports multiple checkouts. In case of a conflict there's a mechanism of conflict resolution. As it justifies the utility of this feature thus it's supported.

Hope it helps!!!

Regards,
-Mateen.

m satheesh said...

HI Guys can you give me the solution ??
Public static void mail(string args[])throws Exception................why we are using throws Exception

Anonymous said...

Why we use final keyword in java?

Post a Comment