Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Super class method and Interface default method conflict resolution

Consider the below example,

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's no connection between SupCls class and Intf interface. But both are defining a common method.

And Testing class is extending SupCls and implementing Intf.

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

From SupCls

Which I think makes sense because extending from a class should take higher priority than implementing from an interface.

But eclipse reports otherwise, as shown in the below screen capture.

Screen Capture of Eclipse

I strongly believe this is a bug in Eclipse.

But before assuming, is this behavior defined & documented in the JLS? Or is there something else which defines this behavior?

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

like image 232
Codebender Avatar asked Sep 09 '15 04:09

Codebender


People also ask

How do you resolve default method conflicts in interface?

Rules for Default Method Conflict Resolution Classes will always win. If a class extends a parent class and implements one or more interfaces, the default method in the class or a superclass will take priority. Otherwise the subinterfaces will take the next level of precedence.

What happens when two interfaces have the same default method?

However, if two interfaces implement the same default method, then there is a conflict. In cases where one interface inherits another interface and both of them implement a default method, an implementing class would use the default method of the child interface.

Can default methods in interface be overridden?

you can override a default method of an interface from the implementing class.

What is the default method priority?

The following rules can be used to determine which method is selected in such cases: A class or superclass method declaration always takes priority over a default method. Otherwise, the method with the most specific default-providing interface is used.


2 Answers

Your assumption is right, the concrete method inherited from the superclass takes precedence over the default method from the interface:

JLS §8.4.8. Inheritance, Overriding, and Hiding

A class C inherits from its direct superclass and direct superinterfaces all abstract and default (§9.4) methods m for which all of the following are true:

  • No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.
  • No concrete method inherited by C from its direct superclass has a signature that is a subsignature of the signature of m.

The second cited bullet applies here, there is a concrete method inherited from the direct superclass with an appropriate signature, so the default method is not inherited.

The documentation even clears any doubt with an additional remark:

Note that it is possible for an inherited concrete method to prevent the inheritance of an abstract or default method. (Later we will assert that the concrete method overrides the abstract or default method "from C".)

So it’s like SupCls.test() overrides Intf.test() when it comes to class Testing.

In other words, you are right, it’s a bug in Eclipse, but as long as it only affects the way the proposal is rendered, I’d consider it a minor bug. The inserted source will be the same, regardless of whether a D has been rendered in the proposal or not.

like image 65
Holger Avatar answered Nov 04 '22 04:11

Holger


This certainly is a bug in eclipse but in the code completion proposals rather than in the compiler. Hovering over the call to test or Open Declaration take you to the SupCls method and running the code correctly prints "From SupCls" which proves this. Please file a bug against jdt ui for investigation

like image 39
Sasikanth Bharadwaj Avatar answered Nov 04 '22 02:11

Sasikanth Bharadwaj