Essentially List<? extends String> List<? extends String> is a list of an unknown type, all that is known is that this type extends String. It does not matter that the variable contains an instance of List<String> .
Now String is final, so there is no subclass ... so consider:
List<? extends A> list = new ArrayList<A>();
where A is a class with a subclass of B When using a variable of type List<? extends A> List<? extends A> may be an instance of List<B> . We cannot add A to it, because there is a possibility that A not B
But do you know that will be right? Well, the compiler is not , and it does not expect logical connections between an unknown type and what you created, because in the general case you can change this variable at runtime.
A similar example exists in a textbook optimistically called More Fun with Wildcards , where the method accepts (Set<T>, T) , and an invalid call is used using (Set<?>, String) , although the variable Set<?> Contains an instance of Set<String> . The problem is here, despite the addition of extends .
source share