Reputation: 23187
I've been for ages trying to figure out what's on earth the difference between a List<? extends A>
and a List<A>
.
What can I do, what couldn't I do...?
Upvotes: 0
Views: 51
Reputation: 48404
A List<? extends A>
reference will allow its value to be a List
of A
or any child / implementing class.
For instance, with parent class A
and child B
, the following idiom is valid:
List<? extends A> list = new ArrayList<B>();
... whereas List<A> list = new ArrayList<B>();
wouldn't compile.
However
A bounded type parameter with wildcards (the <? extends A>
idiom) comes at a cost.
You cannot add
elements to the List
, only remove elements, iterate it or query its contents.
That is because the specific type of elements in your list will be unknown at runtime, hence adding any element is unsafe.
Upvotes: 2
Reputation: 59095
If you have class B extends A
then
You can do:
List<? extends A> myList = new ArrayList<B>();
because List<? extends A>
is a list whose generic type is some subtype of A
.
Therefore you cannot safely put anything into List<? extends A>
because you don't know how specific its generic type actually is. You only know that the items it contains must be A
, so you can take A
out of it.
You cannot do:
List<A> myList = new ArrayList<B>();
because List<A>
is a list whose generic type is A
.
Therefore if you have a List<A>
you know that its generic type is exactly A
, so you can put any instance of A
into it.
Upvotes: 1