We have:
class A{}
class B extends A{}
class C extends B{}
class D extends C{}
we can define Lists like:
List<? super B> lsb1 = new ArrayList<Object>();
//List<? super B> lsb2 = new ArrayList<Integer>();//Integer, we expect this
List<? super B> lsb3 = new ArrayList<A>();
List<? super B> lsb4 = new ArrayList<B>();
//List<? super B> lsb5 = new ArrayList<C>();//not compile
//List<? super B> lsb6 = new ArrayList<D>();//not compile
now we crate some objects:
Object o = new Object();
Integer i = new Integer(3);
A a = new A();
B b = new B();
C c = new C();
D d = new D();
I will try to add this objects to List:
List<? super B> lsb = new ArrayList<A>();
lsb.add(o);//not compile
lsb.add(i);//not compile
lsb.add(a);//not compile
lsb.add(b);
lsb.add(c);
lsb.add(d);
Question:
Why when I define reference for List<? super B>
can I use new ArrayList<>();
which can have elements type of B, A, Object (I expected this), but when I add elements to this list can I add only objects with type B, C, D?