Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance

 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is Multiple implementation inheritence not allowed in Java.
Do we have a logic to put forward ?
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple inheritance is not supported because it complicates the compiler
- There may be name collisions of variables and methods of superclasses. Suppose a class C extends Class A and B which has methods show().
- C++ uses explicit referencing of super class methods and attributes but it complicates the complier.
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name collision can't be ruled out even when implementing multiple interfaces. Like two interfaces defining a varibale with same name.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not have multiple inheritance because it has interfaces. These are two different solutions to the same problem. Java chose interfaces over MI because they are cleaner and more elegant (and used by other Smalltalk-derived OO languages such as Objective-C). This does not mean, however, that they are perfect. You could debate the merits of each back and forth and nobody comes away from the argument feeling happy.
That said, I have yet to come across a situation in my coding where I have actually needed multiple inheritance. And, if I did, I could construct a class that actually uses multiple inheritance. It just takes using interfaces and judicious uses of inner classes. Not that I recommend doing this (and I have not tired this out in practice...), but it is possible.
And, as a note, C# has solved the name-collision problem. This is something that I would like to see included in Java as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic