• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generic declarations "? super" Vs. "? extends"

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


I have a doubt with the generic declarations "? super" and "? extends"


"? super Integer" means any class above the Integer class is permitted

"? extends Integer" means any subclass of Integer is permitted

I know that you cannot use "? extends" for access to a collection such as add or delete

But what about "? super"? I tried some code and it allows access to the collection.



If I change the generic declaration to Set<? extends String> s1

it will not compile

Please can anyone explain further?


Thanks
 
Ranch Hand
Posts: 30
Eclipse IDE Spring Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Santiago,
"? extends String" means that collection is applicable for all subclasses of String, but as String is a final class it cannot has any subclasses.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"? super Integer" means any class above the Integer class is permitted



Not quite. It means that the collection works with an unknown type -- that happens to be super class of Integer (bounded by). It doesn't mean any type is allowed, but the type that is allowed is one either an Integer or one of it's super classes.


"? extends Integer" means any subclass of Integer is permitted



Not quite. It means that the collection works with an unknown type -- that happens to be subclass of Integer (bounded). It doesn't mean any type is allowed, but the type that is allowed is one either an Integer or one of it's derived classes.


Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know that you cannot use "? extends" for access to a collection such as add or delete




If I change the generic declaration to Set<? extends String> s1

it will not compile




I am not sure what you are asking... but don't one quote answers the other quote?

Henry
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Sorry for any confusion.

Basically I was under the impression that if your generic declaration has either "? super" or "? extends" then you cannot add or delete from the collection. Now, this holds true for "? extends" as it can only be used for access. However with "? super" you can add or delete from the collection.

So my real question is why CAN we add or delete from the collection if the generic delcaration is "? super"


Thanks
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So my real question is why CAN we add or delete from the collection if the generic delcaration is "? super"



example: ? super someclass

Basically, you don't know what type the collection takes, but it is one of the super classes of someclass.

Inheritance says that a subclass IS-A super class type, and of all class types up the inheritance tree. So, a someclass IS-A someclass, and IS-A all the classes which are super class of someclass.

So... if you add or delete someclass (or one of the subclass of someclass), you guarantee that the class is the type of the collection, even though you don't know what type it is.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically I was under the impression that if your generic declaration has either "? super" or "? extends" then you cannot add or delete from the collection. Now, this holds true for "? extends" as it can only be used for access. However with "? super" you can add or delete from the collection.



BTW, IMO, it is better to understand why you can do something, and the limitations of what can be done, than to just memorized that .... add or delete is not allowed if you use some declaration.

Henry
[ November 19, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add is not allowed for ? extends but can be applied to ? extends super . C&B page 619.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You realize this topic is more than two years old?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the reason <? extends String> fails, while <? super String> is successful, is purely a matter of scope. A specific generic type method signature... ...cannot be accessed when the initialization of the generic instance was created with wildcards...

For Example:

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic