Lets say we have 2 classes. An empty class Base
, and a subclass of this class Derived
.
public class Base {}
public class Derived extends Base {}
Then we have a few methods in another class:
import java.util.Collection
public class Consumer {
public void test() {
set(new Derived(), new Consumer().get());
}
public <T extends Base> T get() {
return (T) new Derived();
}
public void set(Base i, Derived b) {
System.out.println("base");
}
public void set(Derived d, Collection<? extends Consumer> o) {
System.out.println("object");
}
}
This compiles and runs successfully in Java 7, but does not compile in Java 8. The error:
Error:(8, 9) java: reference to set is ambiguous
both method set(Base,Derived) in Consumer and
method set(Derived,java.util.Collection) in Consumer match
Why does work in Java 7, but not Java 8? How could <T extends Base>
ever match Collection?