+ 1
Why java doesn't support multiple inheritance?
7 Antworten
+ 4
If a parent class has a method, and two subclasses have overridden that method, what method does the class that extends both those classes inherit?
+ 3
Java chose not to support direct multiple inheritance due to ambiguity. The biggest reason, the diamond effect. You can still do multiple inheritance via interfaces though, it's just a different approach.
+ 1
James, can you give example on how ambiguity?
0
thanks
0
you can use interface to inherit from multiple classes
0
class left
{
void m1()
{
sop("m1-method of class left");
}
}
class Right
{
void m1()
{
sop("m1-method of class Right");
}
}
let's assume that Java supports multiple inheritance concept.and let's extend both left and Right classes.
class Test extends left,Right
{
public static void main(String...args)
{
Test t = new Test();
t.m1();
}
}
Now can you say which class m1() method will be executed. the answer is you may not. in the same manner JVM also will get confused which class m1() method should it execute.
conclusion: Because of ambiguity problems Java doesn't support multiple inheritance.
0
It could just establish a hierarchy in the inheritance: class C extends A, B and both classes have a method dE could mean that C inherits the method from the second class (like, first C inherits from A, then B overrides some of its methods)