Sileria Inc. Interview Question
Software Engineer / DevelopersTeam: Android Application Development
Country: Pakistan
Interview Type: In-Person
I agree with you. Very important and precise reason that we can mention is the diamond problem which occurs in multiple inheritance. For example. Class B and C both inherits class A. And class D inherits both, B and C. So now this is a diamond problem as how will you decide which route will the class D get the heritage, i.e A-->B--D or A--->C-->D? Also a question arises will there be multiple copies of a class A public local variable in class D ?
I wouldn't necessarily say it's a wise decision to exclude multiple inheritance. That depends on perspective. But yes, Java in particular omitted it for simplicity and to avoid a number of problems associated with it.
dimond problem can be handled through virtual inheritance and carefully design. If you do not need member variables from multiple classes, then you should not apply multi-inheritance.
actually, dimond problem is one of the advantages of multi-inheritance, how will you do in Java if D needs both properties in B and C ?
Nothing is impossible in Java. Java is written to program in a beautiful and smart way. As you mentioned above, it will be a design fault if D needs something like that. B and C both extending from A means Generalization to specialization and then you are making D to extend both which doesn't really look like a smart thing to do though I agree that there can be cases when you need to do so. But in order to solve that, I will rather use interfaces, or Make an abstract class and make A extend that class for example. It depends, there can be many design solutions for that in Java. Which we can come up with rather than creating the Diamond lock and confusing class C.
"Nothing is impossible in Java."
Alas, plenty of things are, in fact, impossible in Java, such as placing things at specific memory addresses, etc.
It's not a design fault if D needs something like that. It's a language fault for not giving programmers something they need. There's nothing inherently wrong with wanting to inherit from two types. It was removed because it caused difficulties in C++ and was used somewhat rarely anyway.
Apart from the above mentioned complications, there is one more danger. i.e one addition of class as parent at top of hierarchy can easily bloat the size of objects at child level. This is one reason why embedded systems (like Symbian) allways try to put restriction on multiple inheritance.
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.
- Anonymous November 29, 2011So, we can support multiple inheritance in java using interfaces.