1

I know that similar questions have been asked (here, here, here), but none of the answers seem to apply to my case.

Consider the following set of interfaces:

public interface I1<X> {
    void method(X arg);
}

public interface I2 {
    void method(String arg);
}

public interface I3 extends I1<String>, I2 {
    // empty
}

Now I want to call method(String) on an instance of I3, like so:

public class C implements I3 {
    public void method(String arg) {
        // does nothing
    }

    public static void main(String[] args) {
        ((I3) new C()).method("arg");
    }
}

The OpenJDK (Java 7 or 8, doesn't matter) flags an incompatibility error here:

generics\C.java:10: error: reference to method is ambiguous
    ((I3) new C()).method("arg");
                  ^
both method method(String) in I2 and method method(X) in I1 match
where X is a type-variable:
    X extends Object declared in interface I1

Since X is instantiated to String in I3, I do not see where the problem comes from. Note that Eclipse considers this to be fine.

Community
  • 1
  • 1
Arend
  • 2,363
  • 1
  • 18
  • 18

1 Answers1

1

The answer to the question, "why?" is simple but probably not what you're looking for. It is, "Because Eclipse and Open JDK each have their own compilers and either a) one of them has a bug or b) the language specification (JLS) is ambiguous and they've interpreted it differently."

Figuring out which of a) or b) is the case is a tricky, tedious task. It means, as a starting point, reading the relevant sections of the JLS, trying to compile the same code with Oracle JDK's javac, and possibly diving into the bug tracking systems of Eclipse and OpenJDK.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • "Why" should be expanded to "does anyone have an idea of why the OpenJDK considers this to be incompatible" – Arend Nov 12 '15 at 14:51