Let's say I have these four classes:
public interface A {}
public interface B {
public A getA();
}
public class C implements A {}
public class D implements B {
public C getA() {
return new C();
}
}
This compiles well. However if I have this code I get a compilation error:
public interface A {}
public interface B {
public List<A> getA();
}
public class C implements A {}
public class D implements B {
public List<C> getA() {
return new ArrayList<C>();
}
}
public class E implements B {
public List<A> getA() {
return new ArrayList<C>();
}
}
What is the reason that allows to return a C in the method getA, in the first example, but generates an error in the second example when I return a List?
It looks to me that both examples should compile or throw an error.
Thanks.
EDIT I read the post using Dog/Animal, however my doubt is different. I have added the class E. If you call getA in class E you get a List as defined in the interface. Then why it fails compiling for class E?