aspose file tools*
The moose likes Beginning Java and the fly likes Single Inheritance vs. Multiple Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Single Inheritance vs. Multiple Inheritance" Watch "Single Inheritance vs. Multiple Inheritance" New topic
Author

Single Inheritance vs. Multiple Inheritance

David Leland
Greenhorn

Joined: Jun 19, 2002
Posts: 11
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

Joined: Jan 04, 2003
Posts: 867
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 ]

Francis Siu
SCJP, MCDBA
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

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 ]

    Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Single Inheritance vs. Multiple Inheritance