aspose file tools*
The moose likes Java in General and the fly likes Why cannot a java class extend multiple classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why cannot a java class extend multiple classes?" Watch "Why cannot a java class extend multiple classes?" New topic
Author

Why cannot a java class extend multiple classes?

vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
Wouldn't it be cool if java could have multiple inheritence just like c++. I know multiple inheritence can still be achieved using interfaces. But I still can't get why java doesn't allow a class to extend more than one class?
[ June 13, 2006: Message edited by: vishwanath nadimpally ]
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

Simplicity and control. Too much sub-classing can be a huge anti-pattern. Its not that they couldn't support multiple inheritance, its that it was decided (based on previous history) it would be a bad idea to let programmers use it.

Some things exist in the Java API as they are because too many programmers easily abused them.


My Blog: Down Home Country Coding with Scott Selikoff
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
The reasoning I recall the designers giving was that interfaces achieved the benefits of multiple inheritance without the drawbacks and consequently there was no convincing reason to permit it.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Bsically classes in java represent noun.



A child can't hav two Fathers.

Whereas interfaces can extends more than one.

interfaces are not noun, they r adjectives. They represent some behavior.

behavior can extend more than one behavior.

Since instances of class hav certain existance on this earth. A Child instance will always be associated with only one Father instance not two father instances.
[ June 13, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Interfaces were introduced to Java to enhance Java's single-inheritance model. The designers of Java decided that multiple inheritance created too many problems for programmers and compiler writers, and decided that a single inheritance model was better overall. Some of the problems described in the previous discussion on the single-inheritance model are solved in a more elegant fashion by the use of interfaces.


http://java.sun.com/docs/white/langenv/Object.doc2.html#6185
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116


very enlightening. Thanks!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Naseem Khan:
Bsically classes in java represent noun.

A child can't hav two Fathers.

Since instances of class hav certain existance on this earth. A Child instance will always be associated with only one Father instance not two father instances.


Where is the object's mother? A child has exactly one mother, and you know the old saying: "mother's baby, father's maybe". No, classes are mothers and interfaces are fathers.
[ June 14, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
A subclass is not a child, the analogy is absurd on a number of levels and so is debating whether or not it's a mother or a father because it's neither and not even close to comparison.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

As Jeff and Ken say, an inheritance relation between two classes is not analogous to a parent - child relation.

Inheritance means "is a". The derived class is a specialized version of the superclass.

For example, if you have a class Animal and a class Elephant which is a subclass of Animal, the relation is clear - an Elephant is an Animal, and it's a specific kind of (i.e. more specialized version) of Animal. There is no parent - child relationship; the Animal is not the "father" of Elephant.

A thing can be two other, different things at the same time, so multiple inheritance sounds like a logical feature to have.

However, in practice multiple inheritance seems to make software more complex. You get into all kinds of difficult issues, especially if two superclasses of a derived class have a common base class, etc. In practice, multiple inheritance is never really necessary.

The designers of the Java language decided to keep the language simple and easy to use. Fortunately they didn't include it in Java because it's "cool"...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

True that. I have a mother and a father, but I don't think I can squeeze out any Children

Multiple interface inheritance is only a little better than multiple implementation inheritance. Both solved by better design.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Both solved by better design.
I think I know what you mean, but please explain for those who don't.

And when I did obstetrics it was never anybody called "Mr" squeezing out children. They all seemed to be called "Mrs" who were doing it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

Multiple interface inheritance does not cause problems like you have with multiple class inheritance.

One of the problems with multiple class inheritance is what I already mentioned above: what happens if your derived class has two superclasses and those superclasses have a common base class?

As you know, an instance of a derived class in memory consists of the data of the superclass plus the data of the derived class. Suppose you have this:

What would you expect that the method printText() prints? Should it print "one" or "two"? Are there two instances of class Base that are part of the instance of class Derived?

In C++, this was solved by virtual base classes. You can make the base class virtual, which means there will be only one instance of class Base which will be shared by the SuperOne and SuperTwo instances; or not, which means you'll have two instances of Base.

You don't have this problem with multiple interface inheritance, because an interface does not contain data.

Multiple interface inheritance is something completely different from multiple class inheritance, and I don't see any big problems with multiple interface inheritance.
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
The only problem is if both interfaces declare a method of the same name,
using the same parameter types, but a different return type. These cases,
fortunately, do not even compile. Either the compiler complains that a
method from one of the interfaces is not implemented, or is implemented
falsely, or it complains that the method signature already exists if you
try to define both.
[ June 16, 2006: Message edited by: Martin Simons ]
kheteswar boravat
Greenhorn

Joined: Mar 10, 2011
Posts: 1
Naseem Khan wrote:Bsically classes in java represent noun.



A child can't have two Fathers.

Whereas interfaces can extends more than one.

interfaces are not noun, they are adjectives. They represent some behavior.

behavior can extend more than one behavior.

Since instances of class have certain existence on this earth. A Child instance will always be associated with only one Father instance not two father instances.
[ June 13, 2006: Message edited by: Naseem Khan ]


This is really nice understanding. I suggest people to do not get it literally but understand its soul.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Welcome to the Ranch

I would disagree; the use of "child" and "father" is misleading.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

I agree with Campbell that using names like "child" and "father" when talking about inheritance with regard to object oriented programming is misleading. The biological meaning of inheritance is completely different from the meaning in object oriented programming.

In OO, inheritance means specialization. There is an "is a" relationship between the subclass and the superclass. For example:

A Dog is an Animal. That's what inheritance means in OO.

When you do this:

then what you are saying is: a Child is a Father.

That's obviously wrong.

Taking a wrong analogy and applying it to draw conclusions about multiple inheritance leads to wrong conclusions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why cannot a java class extend multiple classes?