prakash.athani
prakash.athani
Branch Unspecified
07 Jul 2008

Java-Multiple inheritance

Hello CEans. Can anybody please help me know what is the problem with multiple inheritance. Why Java does't support it?
10 years ago
Well, most probably because the designers of Java wanted it to keep it a simple, object oriented language. 😀

Java does support multiple inheritance through 'interfaces':

Check: Java Multiple Inheritance

and

Designing with interfaces - Java World
shalini_goel14

shalini_goel14

Branch Unspecified
10 years ago
Check the following scenario:
class A{
void method1(){
//some code here
}
}

class B{
void method1(){
// some code here
}
class C extends A,B { //do not try..not possible in java
//assuming it inherits method1() method

}
class JavaMultipleInheritenceTest{
public static void main(String args[]){
C cObj=new C();
cObj.method1();//creates an ambiguous situation for JVM to detect which
class's overridden method method1() it is asking for(classA or class B)

}
}
This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.
viktor_ns

viktor_ns

Branch Unspecified
10 years ago
shalini_goel14
Check the following scenario:
class A{
void method1(){
//some code here
}
}

class B{
void method1(){
// some code here
}
class C extends A,B { //do not try..not possible in java
//assuming it inherits method1() method

}
class JavaMultipleInheritenceTest{
public static void main(String args[]){
C cObj=new C();
cObj.method1();//creates an ambiguous situation for JVM to detect which
class's overridden method method1() it is asking for(classA or class B)

}
}
This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.

The problem you defined is simple to solve. It just uses simple method renaming so they have different names. Dynamic binding would know what method to call in the case of polymorphism despite the renaming.

The more complicated problem to solve is the case of repeated inheritance where one class directly or indirectly inherits several times from the same class. But even for this there is elegant solution as implemented in Eiffel.
xero

xero

Branch Unspecified
10 years ago
Starting with what's the problem with multiple inheritance.

Let's start with a funny scenario. Consider a family, comprising of a child and his/her mom and dad

A common behavior of mom and dad is that both are short tempered, with a difference that child's dad starts breaking things when angry, and mom only yells.

Now, the child's also short tempered, but its impossible to know whether the short tempered behavior is his/her's mom's or dad's (assuming that child won't get both the behaviors 😁 )

So I guess, the problem with multiple inheritance is pretty clear 😀


Second question: Why java doesn't support it?

I would rephrase the question, Why java doesn't allow it?
In Java, the above scenario is resolved as ...
in java, if the child is short tempered then its behavior must be known before its born !

Now, as stated in above replies that multiple inheritance is achieved via interfaces.
Interfaces are not objects, and inheritance only exists between objects, so i guess its no more multiple inheritance 😀

I think this should answer your question.. By the way, i hate explaining OOP concepts with code, so if u were expecting it, m sorry, I won't 😀

n'joy
prakash.athani

prakash.athani

Branch Unspecified
10 years ago
Do you guys think that multiple inheritence is an ambiguous feature that an OO language should not allow?

C++ allows multiple inheritence.

Check out this paper by Bjarne Stroustrup (inventor of C++) where he mentions its misconception.

https://www-plan.cs.colorado.edu/diwan/class-papers/mi.pdf

Also for the scenarios mentioned by shalini_goel14 and xero, we just need a scope resolution operator :: to decide which parent class's method should a child class's object invoke

Also can you guys give me examples of some systems developed using C++,
having problems or crashed, because of its multiple inheritence feature ?
xero

xero

Branch Unspecified
10 years ago
I'll start with clearing a mis conception that inheritance is a feature. Inheritance is a characteristic of OO paradigm, so multiple inheritance is not a feature, and it is ambiguous, but not bad 😛

and yes C++ has its own way to implement it using virtual functions if i'm not wrong, so wherever multiple inheritance comes into picture, it would have been / will be implemented using the standard ways of C++ (either using virtual functions or any new mechanism )

And i don't think that applications/systems will ever crash if multiple inheritance is implemented, atleast i know about those written in C++.


And even if it does crash, then its CURSED !! 😁

n'joy
xero

xero

Branch Unspecified
10 years ago
Hey prakash thanks for the linked. Its pretty good, i would suggest everyone to go through it once !!
viktor_ns

viktor_ns

Branch Unspecified
10 years ago
I like this little bit written by Bertrand Meyer:

In some parts of the object-oriented community, the mere mention of multiple inheritance seems to evoke visions of mysterious, uncharted territories where complexity is the law of the land and treacherous ambiguities lie in cover behind every bush, ready to prey on the poor wandering programmer. The purpose of this month's column is to dispel any such image and show that multiple inheritance is in fact as simple to use as it is powerful.
For a full column follow this link.

I must point out one more thing. We do inherit features from our parents but we are not our parents. That is, child is not its mother, neither is its father. Child has a mother and has a father so that can only be a client relationship not an inheritance.

But CHILD can inherit from classes HUMAN_THAT_YELLS and HUMAN_THAT_BREAKS_THINGS that inherit from class HUMAN. If HUMAN has method engage_short_temper() and it is redefined in both classes HTY and HTB problem can be solved in two ways. First is that in CHILD we make one method from one parent deferred and take the method only from other parent. Second is that we rename methods and select one to use with dynamic binding when engage_short_temper() is called on HUMAN that is really a CHILD.

Share this content on your social channels -

Only logged in users can reply.