When I write the following code compiler says that
cannot convert from
ArrayList<String>toList<Comparable>
private List<Comparable> get(){
return new ArrayList<String>();
}
But when I write return type with wildcard, the code compiles.
private List<? extends Comparable> get() {
return new ArrayList<String>();
}
Can somebody explain me why?
A
List<String>is not aList<Comparable>.After all, you can put an
Integerinto aList<Comparable>.