aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections -super is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections -super is not working " Watch "Collections -super is not working " New topic
Author

Collections -super is not working

Jade Abe
Greenhorn

Joined: Mar 28, 2009
Posts: 4
Hi All,
Do anyone know why this is not compiling?

Cheers,
Jade


import java.util.*;

class A{}
class B extends A{}
public class Guess{


public static void main(String args[]){
LinkedList<A> listA=new LinkedList<A>();
LinkedList<B> listB=new LinkedList<B>();
LinkedList<Object> listO=new LinkedList<Object>();


}
public static void m1(List<? super B> l)
{
l.add(new A());

System.out.println("Working?");
}
}
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Jade,

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.
Jade Abe
Greenhorn

Joined: Mar 28, 2009
Posts: 4
Hi Ruban,
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,
import java.util.*;

class A{}
class B extends A{}
public class Guess{
LinkedList<A> l1=new LinkedList<A>();

public static void main(String args[]){
LinkedList<A> listA=new LinkedList<A>();
LinkedList<B> listB=new LinkedList<B>();
LinkedList<Object> listO=new LinkedList<Object>();


List<? super A> k=new ArrayList<A>();
k.add(new B()); //This is ok

List<? extends A> w=new ArrayList<A>();
w.add(new A()); //This is not ok


listB.add(new B());
m1(listB); //This is not ok


m1(listA);
m2(listB);
m2(listO);
m3(listO);
m4(listA);


}
public static void m1(LinkedList<A> l)
{
l.add(new B()); //compiles ok

System.out.println("DD");
}

public static void m2(List<? extends Object> l)
{
System.out.println("aa");
}
public static void m3(List<Object> l)
{
System.out.println("ww");
}

public static void m4(List<A> l)
{
System.out.println("err");
}
}

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

Cheers,
Jade

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Jade,

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()!
Jade Abe
Greenhorn

Joined: Mar 28, 2009
Posts: 4
sorry , will do




Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Nice one.

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.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Jade Abe wrote:sorry , will do





Hi Jade,

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
list.

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections -super is not working
 
Similar Threads
A good one on overloading... .
unmodifiableList(List list)
Generics
Generics
Generics wildcards