Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need more logical answer to "Why Java has no multiple inheritance" [duplicate]

Tags:

java

Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed

Every answer I have seen so far regarding "Why Java has no multiple inheritance" has only one answer in more specific or detailed way that is "To reduce complexity" but no one defined how it reduces complexity, If we use interface instead of class what difference it makes.Isn't it one and a same thing? what is the difference if we implement an interface and not extend our class?Some one answered with Diamond problem but interfaces can also produce diamond problems.

like image 397
Just_another_developer Avatar asked Feb 23 '26 12:02

Just_another_developer


1 Answers

The difference between multiple inheritance among interfaces vs. classes is when you must inherit implementation. When you inherit method interface through multiple paths, you can say that the implementing class must implement the inherited method. When you inherit from multiple classes, you must decide which of the several implementations to choose. This increases the complexity of the language very significantly, as you can see by examining the way multiple inheritance is implemented in C++.

Here is an illustration:

public class Base {
    public void foo() {System.out.println("base");}
}
public class A extends Base {
    public void foo() {System.out.println("a");}
}
public class B extends Base {
    public void foo() {System.out.println("b");}
}
public class AB extends A, B /* imagine that it's a possibility */{
}

What is going to happen when you do this?

AB ab = new AB();
ab.foo();

With inheritance of interfaces, AB would have to implement foo; with inheritance of implementation, the language would need to decide, or provide you a way to specify it yourself. One way or the other, the complexity is going to grow.

like image 133
Sergey Kalinichenko Avatar answered Feb 25 '26 02:02

Sergey Kalinichenko