Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about super

 
anjali ray
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Ng
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic