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 ]
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.
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 ]
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.
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!
Joined: Jul 15, 2003
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.
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"...
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.
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 ]
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.
Joined: Oct 13, 2005
Welcome to the Ranch
I would disagree; the use of "child" and "father" is misleading.
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.