18

Here's a simple class that demonstrates the issue:

package com.mimvista.debug;

public class DefaultCollisionTest {
    public static interface Interface1 {
        public String getName();
    }

    public static interface Interface2 {
        public default String getName() { return "Mr. 2"; };
    }

    public static <X extends Interface1&Interface2> String extractName(X target) {
        return target.getName();
    }
}

Eclipse (Neon 2) happily compiles this class while javac (JDK 1.8.0_121) spits out the following compile error:

$ javac src/com/mimvista/debug/DefaultCollisionTest.java
src\com\mimvista\debug\DefaultCollisionTest.java:13: error: class INT#1 inherits abstract and default for getName() from types Interface2 and Interface1
        public static <X extends Interface1&Interface2> String extractName(X target) {
                       ^
  where INT#1 is an intersection type:
    INT#1 extends Object,Interface1,Interface2
1 error

I believe that Eclipse is correct in this case but I'm not totally sure. Based on my understanding of the "inherits abstract and default" error, I think it should only be generated when compiling an actual declared class that implements those two interfaces. It seems like javac may be generating an intermediate class under-the-hood to deal with that generic signature and erroneously subjecting it to the default method collision test?

4
  • 3
    Since the aim of the game is to write something javac can compile, by definition, javac is right, unless you've hit an obscure bug, as happened here: stackoverflow.com/questions/42485052/….
    – slim
    Aug 21, 2017 at 13:54
  • @slim I'm not sure who defined the game that you are referring two. Java is defined by JLS not javac :) And the developers of any compiler depend on users reporting bugs like this in order to fix their respective compiler. Until s.o. proves howlger's answer wrong, this should be seen as a bug in javac, indeed. Aug 22, 2017 at 21:40
  • @BonusLord, your hypothesis regarding an intermediate class under-the-hood sounds very convincing to me. It would explain exactly what we are seeing. Aug 22, 2017 at 21:42
  • Anyone still in doubt may want to read Brian Goetz's answer in stackoverflow.com/questions/34644237/… Aug 23, 2017 at 22:30

4 Answers 4

7

Javac is correct according to JLS 9.4.1.3. Interfaces > Inheriting Methods with Override-Equivalent Signatures:

If an interface I inherits a default method whose signature is override-equivalent with another method inherited by I, then a compile-time error occurs. (This is the case whether the other method is abstract or default.)

The small print explains:

[...] when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).

In contrast, the longstanding behavior for inherited concrete methods in classes is that they override abstract methods declared in interfaces (see §8.4.8). The same argument about potential contract violation applies here, but in this case there is an inherent imbalance between classes and interfaces. We prefer, in order to preserve the independent nature of class hierarchies, to minimize class-interface clashes by simply giving priority to concrete methods.

Also compare with 8.4.8.4. Classes > Inheriting Methods with Override-Equivalent Signatures:

It is a compile-time error if a class C inherits a default method whose signature is override-equivalent with another method inherited by C, unless there exists an abstract method declared in a superclass of C and inherited by C that is override-equivalent with the two methods.

This exception to the strict default-abstract and default-default conflict rules is made when an abstract method is declared in a superclass: the assertion of abstract-ness coming from the superclass hierarchy essentially trumps the default method, making the default method act as if it were abstract. However, the abstract method from a class does not override the default method(s), because interfaces are still allowed to refine the signature of the abstract method coming from the class hierarchy.

In even plainer words: the assumption is that the two interfaces are logically unrelated and both specify some kind of a behaviour contract. Therefore it's not safe to assume that the default implementation in Interface2 is a valid fulfilment of the contract of Interface1. It's safer to throw an error and let the developer sort it out.

I didn't find a place in the JLS where it would exactly tackle your case, but I think the error is in the gist of the above specifications - you declare that extractName() should take an object that implements both Interface1 and Interface2. But for such an object it would only be valid if "there exists an abstract method declared in a superclass of C and inherited by C that is override-equivalent with the two methods". Your generic declaration does not specify anything about the superclass of X, so the compiler treats it as a "abstract-default" clash.

3
  • 3
    I don't think this part of the spec actually applies here due to that first line: "If an interface 'I' inherits a default method..." In my sample class, there is no 'Interface I' (ie there is no declared class or interface that is actually inheriting the "getName" method) so this rule should not be coming into play. (The rule WOULD have to be applied to any classes that actually satisfy the bounds of the parameter of my 'extractName' method, but the sample case includes no such classes.)
    – BonusLord
    Aug 21, 2017 at 16:50
  • @BonusLord I extended my answer with the specs for classes and some further reasoning. Aug 21, 2017 at 17:33
  • @AdamMichalik Note, javac compiles a class that implements both interfaces, but fails at the generic intersection type declaration (see example in my answer)
    – howlger
    Aug 21, 2017 at 22:54
6

Eclipse is right.

I have not found this javac bug in the Java Bug Database and therefore reported it: JDK-8186643

Better explanation by Stephan Herrmann (see his comment below):

Right, reporting an error against an intersection type should only happen when the intersection type is not well-formed and hence the intersection is empty. But as this answer shows, the intersection is not empty and should thus be legal. Actually, the error message class INT#1 inherits ... makes no sense, because at that point nobody mentioned a class INT#1, we only have the intersection of two interfaces, and that intersection is used only as a bound, not as a type.

A class that implements multiple interfaces of the same method can be compiled with both compilers, even if the method of one interface has a default implementation. The class can be referenced as <T extends I1 & I2> as long as neither I1 nor I2 has a default implementation for a equally named method. Only if one of the two interfaces has a default implementation javac fails.

In case of ambiguity which implementation should apply, the error should already occur when defining a class, not when the class is referred as <T extends ...> (see JLS 4.9. Intersection Types).

See following example which works with <T extends I1 & I2> and <T extends IDefault>, but fails with <T extends I1 & IDefault> and javac:

interface I1 {
    String get();
}

interface I2 {
    String get();
}

interface IDefault {
    default String get() {
        return "default";
    };
}

public class Foo implements I1, I2, IDefault {

    @Override
    public String get() {
        return "foo";
    }

    public static void main(String[] args) {
        System.out.print(getOf(new Foo()));
    }

//  static <T extends I1 & IDefault> String getOf(T t) { // fails with javac
    static <T extends I1 & I2> String getOf(T t) { // OK
        return t.get();
    }

}
4
  • 2
    Right, reporting an error against an intersection type should only happen when the intersection type is not well-formed and hence the intersection is empty. But as this answer shows, the intersection is not empty and should thus be legal. Actually, the error message class INT#1 inherits ... makes no sense, because at that point nobody mentioned a class INT#1, we only have the intersection of two interfaces, and that intersection is used only as a bound, not as a type. Aug 22, 2017 at 21:36
  • @StephanHerrmann Thx for the explanation which I inserted at the beginning of my answer as a quote. I also added a link to the reported bug.
    – howlger
    Aug 23, 2017 at 13:03
  • 1
    Thanks for the bug link. Meanwhile, Dan Smith found this SO thread and added a link into bugs.openjdk.java.net/browse/JDK-7120669 - there he mentions the "notional class" used in JLS ("the imaginary class crutch"), but the key points are that JLS does not mandate checks against that notional class and that JLS needs to spell out exactly what should be checked (tbd). Aug 23, 2017 at 22:21
  • 1
    It also has been confirmed by Brian Goetz in this answer.
    – Holger
    Sep 4, 2017 at 18:15
1

As I understand it, the question is about passing an object of an already compiled class as a parameter. Since you cannot call the extractName(X) method with an abstract class or an interface, the argument object must have it's getName() method resolved and unambiguous. Java uses a late binding for resolving which overridden method is called at runtime, so I would agree with BonusLord that the method could be correctly compiled and run even if javac throws the error.

1

I would say that this is a Javac bug, or at least it should be.

Looks like the implementors of Javac took a shortcut and reused the code for creating an interface in the implementation of generic boundary. IOW, Javac treats <X extends I1&I2> as if it was interface X extends I1, I2.

In reality, however, <X extends I1&I2> is different. It just means that X has the methods of both I1 and I2, but says nothing about the methods' implementations. Therefore absence or presence of default implementation should be irrelevant.

Unfortunately, as @slim says, the goal is to pass JDK's compiler, so Javac has the last word. Submit a bug report, maybe?

2
  • 1
    Yeah I ended up changing my actual use-case to something that javac could compile, but my main goal with this question was to determine if this should be reported as a bug against Eclipse vs a bug against javac~
    – BonusLord
    Aug 21, 2017 at 19:15
  • It's a bit more than having the methods of I1 and I2, any type substituted for X must actually be a (nominal) subtype of both interfaces. But the essence of this answer is still correctish. Aug 22, 2017 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.