Is there a problem with the lower border ("super")?

Possible duplicate:
java generics super keyword

I cannot relate my knowledge to the program below. See below an example program, then my doubts below this 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 ? super T means any class you can add is super in T , but is the output different here? Even a subclass has also been successfully added, which is completely confusing. - the output does not differ from the initialization of the list ( List<? super C> list3 = new ArrayList<C>(); ). In this initialization, I assigned list A or B , the output was the same!

Please clarify my doubts.

+4
source share
4 answers

"? super T" means any class you can add that must be an instance of T or its subclass.

So List<? super C> list3 = new ArrayList<C>(); List<? super C> list3 = new ArrayList<C>(); in list3 you can add an instance of class C or its subclass instances ( SomeClass extends C ).

A list of parent objects can have its own, child, and child elements of a child, but a list created for a child class cannot have a parent. I hope you understand now.

+1
source

Should we use ? super ? super in a generic declaration, a reference may point to the same generic collection type or super generic collection type. Thus, everything that passes the IS-A test is valid in this case.

+1
source

List<? super B> List<? super B> means that

 List<? super B> list1 = new ArrayList<A>(); List<? super B> list1 = new ArrayList<B>(); 

allowed. ArrayList<A> is a super ArrayList<B> , right? Can we add B and C both ArrayList<A> and ArrayList<B >? Yes we can. Can we add A ? Only up to ArrayList<A>. But List<? super B> List<? super B> can also point to an ArrayList<B> . Therefore, we cannot add A to List<? super B> List<? super B> .

+1
source

Comment on the comment @ us2012.

  • For List<? super A> List<? super A>

    types A, B and C are all subtypes of A, so you can add them.

  • For List<? super B> List<? super B>

    the type can be A, B. Both B and C are subtypes of A or B. Think about the difference between List<? super B> list2 = new ArrayList<A/B>() List<? super B> list2 = new ArrayList<A/B>()

  • For List<? super C> List<? super C>

    the type can be A, B, C. Thus, only C is a subtype of A, B or C.

For example, Machine β†’ Car β†’ SportsCar . for List<? super Machine> List<? super Machine> , regardless of car, car and sports car, they are all machines, so you can add them to a safe place.

0
source

All Articles