• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Alexey Saenko
Ranch Hand
Posts: 30
Chrome Eclipse IDE Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Soumya Ranjan Mohanty
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add is not allowed for ? extends but can be applied to ? extends super . C&B page 619.
 
Stephan van Hulst
Bartender
Pie
Posts: 4846
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You realize this topic is more than two years old?
 
Ryan Patrick
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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:

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic