Possible Duplicate:
java generics super keyword
I am not able to relate my knowledge with the below sample program. Please see the below sample program then my doubts are below that program.
import java.util.*;
class A { }
class B extends A { }
class C extends B { }
public class sampleprog {
public static void main(String[] args) {
List<? super A> list1 = new ArrayList<A>();
list1.add(new A());//valid. ok
list1.add(new B());//valid, why? it is not super to A?
list1.add(new C());//valid, why? it is not super to A?
List<? super B> list2 = new ArrayList<A>();
list2.add(new A());//not valid. why? it is also super to B!!!
list2.add(new B());
list2.add(new C());
List<? super C> list3 = new ArrayList<C>();
list3.add(new A());//not valid, why? It is super to A so should be valid!!!
list3.add(new B());//not valid, why? It is super to A so should be valid!!!
list3.add(new C());
}
}
My Doubts:
- As far as I know ? super T
means any class you can add that is super to T
but here output is different? Even subclass also added successfully that is totally confusing.
- output is not different with list initialization (List<? super C> list3 = new ArrayList<C>();
) . In this initialization, I assigned list of A
or B
, output was same!
Please clear my doubts.