java - Why is this such use of generic and wildcard not allowed? - Stack Overflow
List<? super Apple>
means a List
you can add an Apple
to (and since Jonathan
is an Apple
, you can put Jonathan
s into a List
of that type as well).
It can be List<Apple>
, List<Fruit>
or List<Object>
, but not List<Jonathan>
, since you cannot put arbitrary Apple
s into List<Jonathan>
. As you can see, in this case ?
can be an Apple
or any of its superclasses.
List<? extends Apple>
means a List
you can get an Apple
from. It can be List<Apple>
or List<Jonathan>
, but not List<Fruit>
, since List<Fruit>
is not guaranteed to contain only Apple
s.
This explanation is known as "producer - extends
, consumer - super
" rule: if parameter acts as a consumer of elements, it should be declared with super
, and vice versa.
Read full article from java - Why is this such use of generic and wildcard not allowed? - Stack Overflow
No comments:
Post a Comment