File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about super 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 "question about super" Watch "question about super" New topic
Author

question about super

anjali ray
Greenhorn

Joined: Mar 12, 2005
Posts: 28

The above code does not compile.
I was wondering why I am not able to add to the list?
It does have a super and I thought I could add String or a super class of String and Object being superclass of all, I should be able to add Object.
Thanks for any help
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Originally posted by anjali ray:

The above code does not compile.
I was wondering why I am not able to add to the list?
It does have a super and I thought I could add String or a super class of String and Object being superclass of all, I should be able to add Object.
Thanks for any help


As far as my limited understanding goes, I think that:

wildcard(? notation) is used similarly as Object[].

We know that Collection can not be used polymorphically like:

Object[] arr = new Integer[10];//This is ok with array

But not:

ArrayList<Object> arrList = new ArrayList<Integer>();//Can't do this!

To overcome this. ? is used

ArrayList<?> arrList = new ArrayList<Integer>();//OK!

Now ? behaves like Object class which is supertype of all classes

? can have bounds: extends and super

These 2 work similarly except:

? extends T: the right handside can be a collection T or subtypes of T. However, you can't add anything to the collection.

? super T: the right handside can be a Collection of T or supertypes. You can add T and subtypes of T elements to the collection.

Back to this question: you can pass a Collection of String or supertypes of Strings but you can't add a supertype of String element to the collection. Hence .add(new Object()) produces compile-error
George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi Anjali,
The following is your code:




List<? super String>: The use of super indicates that you can add into the collection. But you can only add the type on the right side (in this case String) or any of its subtypes. You can never ever put a supertype in a subtype collection. This is about adding into the collection.

But when it comes to invoking it, you can pass the type on the right side (in this case String) or any of its supertypes (ofcourse including Object).

This is my understanding.


Regards,<br />George Gates
Steve Ng
Greenhorn

Joined: Aug 10, 2008
Posts: 17
Generics is hard to understand but I like it.

Well, I think that what matters is the reference type(lefthand side). I believe this code won't compile:

List<? super FileNotFoundException> ls1 = new LinkedList<IOException>();
ls1.add(new IOException());

Because the compiler will compare <? super FileNotFoundException> to new IOException(). It realizes that IOException isn't safe to be inside because it's not subtype of FileNotFoundException. So the conclusion is only subtype of FileNotFoundException and itself can be added to the collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about super