• Post Reply Bookmark Topic Watch Topic
  • New Topic

To the author: Multiple Inheritance  RSS feed

 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Cay and Gary,

Up till now, Java does not allow multiple inheritance, however, I sometimes really find that it is necessary, otherwise, our coding might have duplications.

Do you think Java, say, in next core release, will allow us to do so?

In addition, what is your view on this issue?

Nick
 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, nick, why you feel implement interfaces is not good enough to handle multiple inheritance ?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because we certain need 2 sets of functions, which have been implemented already. If we just let our class to implement the interfaces, each class that require those methods will need to implement themselves. But how about, if the implementation in fact could be the same?

Nick
 
author
Ranch Hand
Posts: 197
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am quite sure that Java will never have multiple inheritance. Indeed, sometimes, it is a pain not to have it. But implementing multiple inheritance is very hard. In C++, you have messy situations with virtual base classes. In Eiffel, you avoid all that, but the implementation cost is huge, particularly in deep inheritance hierarchies.

Some people think that multiple inheritance is a completely academic issue that never comes up in real programming. But sometimes it does. Here is an example.

Remember good old AWT? There are Component objects and Container objects. A Container can hold Component objects. (The "Composite" pattern at work...)

Then Swing came along. JComponent extends Component. Oops...it doesn't. It actually extends Container because SOME Swing components (such as JPanel) are containers. In C++, we could have JContainer extend JComponent and Container (well, maybe, if all the virtual base classes are properly defined...), but Java has no multiple inheritance. Therefore, JComponent and its subclasses such as JButton inherit unsightly methods "add" and "remove", even though it makes no sense to add a component to a JButton.

Maybe someone will figure out how to implement multiple inheritance efficiently, but for now, I think the Java compromise of "single superclass, multiple interfaces" is the best deal in town.

Cheers,

Cay
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maybe someone will figure out how to implement multiple inheritance efficiently, but for now, I think the Java compromise of "single superclass, multiple interfaces" is the best deal in town.


I agree that mutliple inheritance really gave us troubles in old C++ day, however, sometimes, as your example, we really need to have this. Otherwise, we might either (i) merge the related classes to avoid the inheritance; (ii) duplicate the codes in different subclasses.

In case we really have this situation (which I am facing now), which way you would suggest?

Nick
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!