• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
SenthilNathan CAlagan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why java doesn't support multiple inheritance?
I know indirectly it can be achieved through Interface.
Thanks,
senthil
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is one of the most confusing, problem-causing things in other languages, so the creator of Java decided to not use it.
 
Randall W. Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by SenthilNathan CAlagan:
Why java doesn't support multiple inheritance?
I know indirectly it can be achieved through Interface.
Thanks,
senthil

I'm hardly the one to answer this as I am truly a newbie but I just heard comments on this very subject while listening to Bruce Eckel's Thinking in Java CD. It's chapter 8 slide 4 in case you have it.
It's not allowed because of the problem you run into while upcasting in C++. If you have an object x and object A and B both inherit from x, both A and B have a "sub-object" of x. If you upcast from A to x your asking for the address of the sub-object in A.
Now if you have an object C that inherits from A(with its sub object x) and B (with it's sub object x) you now have two sub objects in C both with their own separate references to x. Here's where the problem comes in, if you then try to upcast to X from C... which sub object do you use? That will cause ambiguity errors. That's what happens in C++. Java takes the "interface/implements" approach to avoid such problems.
What I don't understand is the use for even Java's version of multiple inheritance. I'm still struggling with that chapter of the TIJ book.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic