Superclass Method and Default Resolution of the Default Interface Method

Consider the example below,

public class Testing extends SupCls implements Intf { public static void main(String[] args) { new Testing().test(); } } class SupCls { public void test() { System.out.println("From SupCls"); } } interface Intf { public default void test() { System.out.println("From Intf"); } } 

As you can see, there is no connection between SupCls and Intf . But both of them define a common method.

And the Testing class is distributed by SupCls and implements Intf .

So, when I call the test() method on Testing , there is output,

 From SupCls 

Which I think makes sense, because an extension from a class should have a higher priority than an implementation from an interface.

But eclipse reports differently, as shown in the screenshot below.

Screen Capture of Eclipse

I strongly believe that this is a bug in Eclipse .

But before assuming this behavior is defined and documented in JLS ? Or is there something else that defines this behavior?

Edit: Eclipse Mars Release version (4.5.0) , if that matters.

+7
java eclipse java-8 default-method
source share
3 answers

Your assumption is true, the specific method inherited from the superclass takes precedence over the default method from interface :

JLS ยง8.4.8. Inheritance, redefinition and hiding

Class C inherits from its direct superclass and direct superinterfaces all abstract and default methods (ยง9.4) m , for which all of the following statements: true /

...

  • No method declared in C has a signature, which is a sub-label (ยง8.4.2) of signature m .
  • No particular method that C inherits from its direct superclass has a signature that is a subheading of signature m .

The second cited mark is applied here, there is a specific method inherited from the direct superclass with the corresponding signature, so the default method is not inherited.

The documentation even eliminated doubts with an additional note:

Note that an inherited concrete method can prevent the inheritance of an abstract method or a default method. (Later we will argue that a particular method overrides the abstract or default method "from C".)

So its like SupCls.test() overridden by Intf.test() when it comes to the Testing class.

In other words, you're right, this is a mistake in Eclipse, but so far it only affects the way the proposal is rendered, Id considers this a minor mistake. The inserted source will be the same regardless of whether D was provided in the proposal or not.

+4
source share

This is, of course, a mistake in eclipse, but in suggestions for completing the code, not in the compiler. When you hover over a call or Open Declaration, you go to the SupCls method and run the From SupCls code correctly, which confirms this. Please register error against jdt ui for research

+5
source share

It seems that the version of Eclipse matters!

In Eclipse Luna (4.4.1), the reference points to SupCls instead of Intf

enter image description here

Probably a bug in Eclipse Mars

+1
source share

All Articles