• Post Reply Bookmark Topic Watch Topic
  • New Topic

why java does not support multiple inheritance ?  RSS feed

 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
The other day my collegue came up with this question to me
" why java does not support multiple inheritance ? "

i said that a way to achieve is it through interfaces but he still argued that why is it not supported by inheriting multiple classes just like in C++,

I did not have a valid answer for this ??

Can anyone throw light on this issue stating the reason why it was not implemented in java like C++ ??

Thanks in advance,
Bye and Happy Learning,
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the reasons why Java does not support multiple inheritance is to avoid name clashes like you have in C++.
Since all the non private methods in a class are automatically inherited by their subclassess; it follows that two super classess with two methods having same method signatures in the different classess will confuse the compiler as to which class method to call from a subclass. In order to avoid this inherent confusion, the inventors of Java decided in doing away with multiple inheritance.
Anselm Paulinus
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In response to Anselm statement: Only too bad that those name clashes still occur when you are implementing multiple interfaces with the same names for its var members.
Despite the fact that those member vars are final and static etc, you can't call one of those values in the implementing class without causing a name clash. I still wonder why this was allowed while multiple inheritence was not.
Rikko
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted by Vishy:
" why java does not support multiple inheritance ? "

Jave does provide two features namely Inheritance by extension and Nested static inner classes. Together, they can provide the effect of multiple inheritance.
Multiple inheritance as I define here is fact that a subclass wants to inherit from two distinct class hierarchies. Technically, we can not that with inheritance alone because Java provides for extention from one super class only.
The solution that I created here involves two distinct class hierarchies namely:
GrandFather <-- Father
and
GrandMother <-- Mother
The first has two methods gene1() and gene2(). The second one has gene2() and gene3(). As you can see we have name confict with gene2().
Now we want to create a Child class that inherites from both Mother and Father. What about gene2()? Do you want gene2() from Father or Mother class?
Let us we want two Child classes namely:
Mothers_Child -- it will inherite gene2() from Mother class, and
Fathers_Child -- it will inherite gene2() from Father class.
How do we do it? We extend Mothers_Child from Mother class and Father_Child from Father class. Then we nest Mothers_Child class in Father class and Fathers_Child class in Mother class.
We create instances of these sub-classes in Test class and call printGenes(). It will show that Fathers_child will inherit gene1() from Father, gene2() from Father and gene3() from Mother(). And Mothers_Child will inherite gene1() from Father, gene2() from Mother and gene3() from Mother.
Can anyone tell what is one short coming of this solution I created in last three hours? But it was fun...

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted by Rikki:
In response to Anselm statement: Only too bad that those name clashes still occur when you are implementing multiple interfaces with the same names for its var members.
Despite the fact that those member vars are final and static etc, you can't call one of those values in the implementing class without causing a name clash. I still wonder why this was allowed while multiple inheritence was not.
Rikko

You can call them but you have to qualify var with interface name so compiler knows which you want. Here an example:
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because the designers of Java thought that multiple inheritance creates more problems than it solves.
Or as Steve Cook once quoted - "Multiple inheritance is good, but there is no good way to do it" (or was it Alan Snyder? )
This might be the same reason for Java ignoring the operator overloading too, I think.
[ October 02, 2003: Message edited by: Mani Ram ]
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rikko; Barkat has an excellent example to clear the fuzzy parallel you were trying to draw.
Mani; Java did overload the operator + as an arithmetic operator as well as a concatenation operator in String.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anselm Paulinus:
Mani; Java did overload the operator + as an arithmetic operator as well as a concatenation operator in String.

Err...what I actully meant was, it doesn't allow the developers to overload the operators (something which is different from languages like Ada, C++).
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for the great explanations

Thanx again, (prob. now i can answer anyone who comes up with this question )

bye and take care ,
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat:
I had thought about casting the implementing class just before a call is made to a specific interface var. And I agree with you that it solves the problem for the compiler.
But casting should also be able to solve the same problem for multiple inheritance. So why didn't they choose that aprouch on multiple inherintence?
IMHO they where right not to choose for multiple inheritance. But it just seems inconsistent with the interface variables problem/solution. Maybe they shouldn't have allowed interfaces to declare vars?
Rikko
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat, you may wanna think about your code as a good brain teaser question.
It didn't compile, but it took me a while to figure out why. And here is what I think.



Mother extends GrandMother and receives gene3(), but it's an instance method, so Fathers_Child can't call it from a static context! One would think the same problem should occur in Father class, but the code there is fine because Mothers_Child inherits gene3() from Mother and can freely refer to it as an instance method.
Very good stuff!
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vad.
My second last version was exactly what you are suggesting. Then I gave it one more thought about whether it would work in different situations. What bothered me was that the Children can only access the static methods of enclosing class. What if we need to have children access the non-static methods as well. So I decided to keep gene1() static and gene2() non-static. It worked. So futher I decided to keep gene2() in Mother static and gene3() non-static. And I did my first big NO NO. I assumed that is symetrical and it will work. And I posted it without compiling.
Thanks for finding the mistake.
My thoughts for multiple inheritance are:
1. C++ like MI was not borrowed because it was confusing.
2. Inner class were provided way so that a class has two avenues of accessing methods of other classes via super class and via enclosing class.
3. I do not think interfaces have any role in MI as they only provide template for classes to implement the methods. As they themselves do not provide any implementation, no concrete methods can be inherited by the implementing class.
Bad use of MI is what is called 'dead diamond' where B extends A and C extends A, and now programmer wants to have D extend both C and B. That will cause problems. And that is not a realistic situation. What is a realistic requirement is that ' A child will inherit from father and mother where father and mother are two classes in two distinct hierarchies ' These kind of situations can be implemented with the solution I outlined above WITH ONE SHORTCOMING THAT VAD FOUND: THE SUB-CLASS CAN ONLY ACCESS THE STATIC METHODS OF ONE OF THE PARENT CLASS.
There is one more short coming of this solutin. Guess What?
Thanks
Barkat
 
Unni Kainila
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the context of the discussion on the topic, this article may be interesting to you.
Why extends is Evil?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!