Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Single Inheritance vs. Multiple Inheritance

 
David Leland
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was having a discussion with a C++ friend of mine about the benefits of using Java over C++.
To make a long story short we agreed to disagree, but during the conversation he asked, "Why does Java only allow single inheritance?"
I understand the difference between single and multiple but didn't know the answer to his question. But I knew someplace I could get a straight answer.
So does anyone know why Sun choose to make Java single over multiple?
Thanks,
D
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi David
Actually, I think serveral cases that may produce confusion,I can point out one case to illustrate.
In JAVA coding
super means the superclass that one class is extended by another class.
if one class extends two or more classes, what is the meaning of super?
It is ridiculous that the ones is father,another ones is child. Does it make sense?
I think that more interesting cases generated if multiple inheritance occurs.

[ May 16, 2003: Message edited by: siu chung man ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I understand the difference between single and multiple but didn't know the answer to his question. But I knew someplace I could get a straight answer.

Adding to what siu posted, multiple inheritance produces schizophrenic classes for example:

That's just bad class design. Suppose further:

Now when a CatDog is created we have the following logical objects:
  • (2) Object instances
  • (2) Animal instances
  • (1) Cat instance
  • (1) Dog instance
  • (1) CatDog instance


  • That causes all sorts of problems, both from the compiler's point of view and from the programmer's. The biggest of which is, which Object instance do you use to synchronize the CatDog object in a multi-threaded environment? Say the Animal class defines some basic methods and that some of the same methods are overriden by the Cat and Dog classes, which method does the runtime bind to when a CatDog object calls one of those methods?
    Contrast that with interfaces, which only define a communication protocol:

    Now we only have the following instances to deal with:
  • (1) Object instances
  • (1) Animal instances
  • (1) Cat instance
  • (1) ConfusedCat instance


  • A ConfusedCat can always be cast to a GarbageTruckChaser or used in any context that requires a GarbageTruckChaser.
    Multiple inheritance just causes a lot of problems in design and implementation. Using interfaces instead produces simpler, cleaner code that's more extensible and maintainable.
    [ May 16, 2003: Message edited by: Michael Morris ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic