Generics (? extends T)
Hi
Does
List<?extends A> lista =new ArrayList<B>();
make lista read-only?
I ask because given the following:
import java.util.List;
import java.util.ArrayList;
class A{}
class Bextends A{}
class Generics
{
publicstaticvoid main(String args[])
{
List<?extends A> lista =new ArrayList<B>();
List<B> listb =new ArrayList<B>();
lista.add(new A());// 1
lista.add(new B());// 2
listb.add(new B());// 3
}
}
only the add() in line 3 will compile.
Lines 1 and 2 cause a compiler error, viz:
Generics.java:15: cannot find symbol
symbol : method add(A)
location:interface java.util.List<capture#709 of ?extends A>
lista.add(new A());
^
Generics.java:16: cannot find symbol
symbol : method add(B)
location:interface java.util.List<capture#972 of ?extends A>
lista.add(new B());
^
2 errors
How can I use add() with a polymorphically-defined collection (i.e. using <? extends T>, or, is that I cannot? Seems odd that I can neither add an A nor a B to the ArrayList lista above.
Thanks.

