Welcome to the ranch! When you declare a collection like this:
List<? super B>
what you are saying is that the list will have elements of a type which the compiler doesn't know, except that the type will be B, or a supertype of B (in this case it could be B, A, or Object, since Object is a superclass of every other class.)
Since the compiler doesn't know if what you actually have is a List< B >, a List<A>, or a List<Object>, in order to preserve type safety, the compiler must prevent you from inserting elements that, in any of the three cases, might break the list. In this case:
- If List< B >, you can only add elements of type B or any of its subtypes.
- If List<A>, you can only add elements of type A or any of its subtypes.
- If List<Object>, you can add elements of type Object or any of its subtypes.
If you add an element of type A, and the list is actually List< B >, you break the list, because an element of type A is not an element of type B (since A is not a subtype of B.) That's why the code is not compiling.
To sum things up, when you have a type declared as List<? super B>, you can only add elements of type B (or of any of its subtypes.)
I hope that cleared things up.
All code in my posts, unless a source is explicitly mentioned, is my own.
Joined: Mar 28, 2009
Thanks for the reply and thanks for welcoming me to the forum.
It kind of help me but confuse me as well.
I modified the code and add more code to test what you said,
public static void m1(LinkedList<A> l)
l.add(new B()); //compiles ok
public static void m2(List<? extends Object> l)
public static void m3(List<Object> l)
public static void m4(List<A> l)
So 'super' needed to be used in a declration of collection.
in here using super we can add objects of 'A' and 'B' but not super class of 'A' like 'Object'
List<? super A> k=new ArrayList<A>();
k.add(new B()); //This is ok If extends declration of collection.cannot really add anything
List<? extends A> w=new ArrayList<A>();
w.add(new B()); cannot
w.add(new A()); cannot
w.add(new Object()); cannot Sorry program is bit long
Please use the code tags provided to surround your code examples, it makes things far easier to follow, thanks
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Joined: Mar 28, 2009
sorry , will do
Joined: Jul 23, 2008
Just as a heads up for next time, to save any extra typing, you can also edit your original post, instead of posting it again.
Joined: Dec 16, 2008
Jade Abe wrote:sorry , will do
These are the rules that you need to consider:
1. Generic type reference assignments:
- You can assign ArrayList<Integer> to List<Integer> (Polymorphism on the base type of the generic type, as in this case
from ArrayList to List is allowed.)
- You can not assign ArrayList<Integer> to ArrayList<Number> (Polymorphism on the element type of the generic type,
as in this case from Integer to Number is not allowed.) This is because if you assign an ArrayList<Integer> to an
ArrayList<Number>, it would be possible to add to the list elements of any subclass of Number other than Integer (like
Double for example) which would corrupt the ArrayList<Integer> object.
- You can assign to List<? extends A> a List<X>, where X is either A or any subtype of A.
- You can assign to List<? super A> a List<X>, where X is either A or any supertype of A.
- You can assign to List<?> List<X>, where X is any reference type.
2. Addition of elements to generic collections:
- You can only add elements of type A or of any subtypes of A to List<A>.
- You can't add anything to List<?> (you don't know what generic type the list actually is.)
- You can't add anything to List<? extends A> (again, you really don't know what generic type the list actually is.)
- To List<? super A> you can add elements of type A, or of any subclass of A. It's true that you don't know exactly
what generic type the list actually is, but you know the type must be either A or a supertype of A, so no matter which
of these potential types the list generic type is, you can add elements of type A or any subtype without breaking the
I think this covers all the doubts that you expressed in your program. Please, read through these rules a few times, then go over your code again, and if you still have any questions let me know.