In C++, you can extends many classes, so what's the advantages of this design in Java that a class can only extends one class ? Since interface is a pure kind of class(abstract class actually), why not limit the number of interfaces implementation just like class extension ?
Asked
Active
Viewed 1.8k times
7
-
3There's a clear benefit to limiting class inheritance to a single parent class (as per [Chris Hayes's answer](http://stackoverflow.com/a/18863399/535871)). What would be the purpose or benefit of limiting the number of interfaces that a class can implement? – Ted Hopp Sep 18 '13 at 03:35
-
To refer to an interface as an abstract class is incorrect. Abstract classes can have implementation logic in them, and have different default visibility than interfaces. – Chris Hayes Sep 18 '13 at 03:37
-
@ChrisHayes - OP did say "a pure kind" of abstract class. In C++, a pure virtual function has no implementation; I think OP meant that a Java interface is like a C++ class that has _only_ pure virtual functions (no implementation logic at all). – Ted Hopp Sep 18 '13 at 03:40
-
Friend i would suggest you that please read any good java book.kathy sierra's book is one them – The PowerHouse Sep 18 '13 at 04:31
1 Answers
23
Being able to extend only one base class is one way of solving the diamond problem. This is a problem which occurs when a class extends two base classes which both implement the same method - how do you know which one to call?
A.java:
public class A {
public int getValue() { return 0; }
}
B.java:
public class B {
public int getValue() { return 1; }
}
C.java:
public class C extends A, B {
public int doStuff() {
return super.getValue(); // Which superclass method is called?
}
}
Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.

Chris Hayes
- 11,471
- 4
- 32
- 47
-
1I saw that on Wikipedia as well...but I'm not entirely satisfied. There are examples in which the diamond problem is resolved by processing method inheritance in a specific order (so C gets the methods of B first, then A). – Makoto Sep 18 '13 at 03:36
-
@Makoto One could easily argue that that is not a good behavior to have. The creators of Java obviously felt that way. – Chris Hayes Sep 18 '13 at 03:40
-
@Makoto - That resolves certain kinds of problems, but multiple inheritance of behavior can always have problems. If one needs one order for function `a()` and a different order for function `b()`, then a fixed order doesn't solve anything. – Ted Hopp Sep 18 '13 at 03:42
-
@Makoto - Having a fixed order of execution also confuses others who are looking and debugging the code. It would be a huge maintenance headache to checkout for the correct implementation. Moreover in a **c++** scenario where both of the parent classes A & B have 2 functions and I want 1st function from A and 2nd from B, the order doesn't help. – Srihari Sep 18 '13 at 03:48
-
simply specify which method such as if you have parent class a and parent class b and child class c which extends a and b. If they both have method foo(). then simply a.super.foo() and b.super.foo(). All you have to do is specify.... – George Xavier May 21 '19 at 16:34
-
@GeorgeXavier And if you instantiate a `C` and try to call `getValue` on that object? Is the user of `C` supposed to choose which superclass to use? Not that this is unsolvable, but every solution (including denying multiple inheritance) has trade-offs. Preventing the scenario from arising is the solution Java uses, and yes, it also has its downsides. – Chris Hayes May 21 '19 at 17:58