• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about super

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic