Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When a class inherits two variables from parent interfaces, Java insists that any use of the variable name in question be fully qualified. This solves the problem. See the <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3" rel="nofollow noreferrer">Java Language Specification Section 8.3</a>:</p> <blockquote> <p>It is possible for a class to inherit more than one field with the same name. Such a situation does not in itself cause a compile-time error. However, any attempt within the body of the class to refer to any such field by its simple name will result in a compile-time error, because such a reference is ambiguous.</p> </blockquote> <p>A similar statement applies with respect to interfaces (<a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.3" rel="nofollow noreferrer">JLS §9.3</a>).</p> <p>The sample code in <a href="https://stackoverflow.com/a/9860907/535871">the answer by Óscar López</a> is excellent. Here's another example:</p> <pre><code>class Base { int x = 10; } interface Interface { int x = 20; } class SingleInheritance implements Interface { int y = 2 * x; // ok } class MultipleInheritance extends Base implements Interface { int y = 2 * x; // compile-time error int z = 2 * Interface.x; // ok } void aMethod(MultipleInheritance arg) { System.out.println("arg.x = " + arg.x); // compile-time error System.out.println("x = " + Interface.x); // ok } </code></pre> <hr> <h3>Edit</h3> <p>Java 8 introduces a limited form of multiple inheritance for methods because interfaces now can declare <a href="http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html" rel="nofollow noreferrer">default methods</a> that subinterfaces and implementing classes can inherit. Since a class can implement multiple interfaces, this can cause ambiguities because distinct default methods with the same signature could be inherited from multiple interfaces.<sup>1</sup> Java deals with this using a priority scheme to specify which default method is actually inherited. It requires explicitly overriding inherited default methods when the priority scheme fails to yield a single winner.</p> <p>Note that in no case does Java have a Diamond problem, which is a very specific subclass of problems that can come with multiple inheritance.<sup>2</sup> The "Diamond" part refers to the shape of the class inheritance diagram that's required in order to have the problem. In C++, the Diamond problem can arise if a class A inherits from two classes B and C, each of which inherits from a common base class D. In that case, any public members of D ends up appearing twice in A&mdash;once inherited through B and once through C. Also, whenever an instance of A is constructed or destroyed, the constructor or destructor for D ends up being called twice (often with disastrous consequences, hence the "of death" part of the name). C++ solves these issues by providing <em>virtual inheritance</em>. (See the discussion <a href="http://en.cppreference.com/w/cpp/language/derived_class" rel="nofollow noreferrer">here</a> for details.)</p> <p><sup>1</sup>&nbsp;<sub>Note the use of the word "distinct". There is no issue if the <em>same</em> default method is inherited through two parent interfaces that in turn extend a common base interface where the default method is defined; the default method is simply inherited.</sub></p> <p><sup>2</sup>&nbsp;<sub>Other multiple inheritance issues&mdash;like the ambiguities that can arise in Java with interface fields, static methods, and default methods&mdash;technically have nothing to do with the Diamond problem (actually, the Deadly Diamond of Death problem). However, much of the literature on the subject (and an earlier version of this answer) ends up lumping all multiple inheritance problems under the rubric "Diamond of Death." I guess the name is just too cool to be used only when technically appropriate.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload