When a class inherits two variables from its parent interfaces, Java insists that any use of the variable name in question be fully qualified. This solves the problem. See Java Language Specification in Section 8.3 :
A class can inherit more than one field with the same name. This situation alone does not cause compile-time errors. However, any attempt within the body of a class to refer to any such field by its simple name will lead to a compile-time error, since such a link is ambiguous.
A similar instruction applies to interfaces ( JLS §9.3 ).
The sample code in Óscar López's answer is excellent. Here is another example:
class Base { int x = 10; } interface Interface { int x = 20; } class SingleInheritance implements Interface { int y = 2 * x;
Edit
Java 8 introduces a limited form of multiple inheritance for methods, as interfaces can now declare default methods that can inherit subinterfaces and implementing classes. Since a class can implement several interfaces, this can cause ambiguity, because different default methods with the same signature can be inherited from several interfaces. 1 Java deals with this using a priority scheme to indicate which default method is actually inherited. It requires explicitly overriding inherited methods by default when the priority scheme does not give a single winner.
Note that in no case does Java have a problem with Diamond, which is a very specific subclass of problems that can have multiple inheritance. 2 The “Diamond” part refers to the form of the class inheritance diagram needed to solve this problem. In C ++, the Diamond problem can occur if class A inherits from two classes B and C, each of which inherits from a common base class D. In this case, any public members of D end up appearing twice in - B and once through C. Besides In addition, whenever an instance of A is built or destroyed, the constructor or destructor for D ends up being called twice (often with catastrophic consequences, hence part of the death of “death”). C ++ solves these problems by providing virtual inheritance. (For more details see the discussion here )
1 Note the use of the word "miscellaneous". There is no problem if the same method is inherited by default through two parent interfaces, which, in turn, extend the common base interface, where the default method is defined; the default method is simply inherited.
2 Other multiple inheritance problems & mdash, such as ambiguities that can occur in Java with interface fields, static methods, and standard methods, technically have nothing to do with the Diamond problem (in fact, the problem of the death diamond). However, most of the literature on this subject (and an earlier version of this answer) ends up with the fact that all the problems of multiple inheritance fall under the heading "Diamond of Death". I think this is just too cool to be used only when it is technically feasible.