Advertisements
I don’t get why it’s not okay to put a String into the List below:
List<? extends Comparable<String>> test = new ArrayList<String>();
String string = "A";
test.add(string); // why is this not ok? in fact "String" should be a subype of "? extends Comparable<String>
Can anyone please explain where the problem is? I know that there is no benefit in having a wildcard at this point because we can’t inherit from "String", but I want to understand what’s happening an why the compiler is complaining about it.
>Solution :
Consider this:
class X implements Comparable<String> {
@Override
public int compareTo(String o) {
return 0;
}
}
List<? extends Comparable<String>> test = new ArrayList<X>();
Does it make sense to add a String
to test
?