• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why cannot a java class extend multiple classes?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 809
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:


http://java.sun.com/docs/white/langenv/Object.doc2.html#6185



very enlightening. Thanks!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would disagree; the use of "child" and "father" is misleading.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic