I don’t get it. In java I’m allowed to declare an interface as a return type of a method, like:
public List<String> get(){
return new ArrayList<String>();
}
if I now have an interface lets say I and a class C implementing it, why I’m not allowed to define like this:
public List<I> get(){
return new ArrayList<C>();
}
I know the solution to create an ArrayList<I> and add C to it, but I’m wondering why I’m not allowed to declare it like the one above. I thought that every C is also a I though it should be no problem.
You cannot do this because
List<I>andList<C>are incompatible types.You can do this, however