This works :
public interface A {
A doSomething();
}
public interface B extends A{
B doSomething();
}
This doesn’t :
public interface A {
Collection<A> doSomething();
}
public interface B extends A{
Collection<B> doSomething();
}
Why? and what can I do to get the functionality I want in the second example?
Change it to:
and it’ll work properly.
It’s because “a collection of B” does not extend a collection of A”, even though B extends A. A needs to define the return type as a “collection of items that extends A” than you can narrow it further as “a collection of B”. Confusing, yes … but that’s just how it is.