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 Generic declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic declarations "? super" Vs. "? extends"" Watch "Generic declarations "? super" Vs. "? extends"" New topic
Author

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

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Alexey Saenko
Ranch Hand

Joined: Aug 18, 2008
Posts: 30

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.


SCJP 6, SCBCD 5, OCEWSD 6
My blog: http://darkleden.wordpress.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

"? 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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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

Joined: Jul 25, 2008
Posts: 226
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
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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

Joined: Mar 07, 2010
Posts: 44
add is not allowed for ? extends but can be applied to ? extends super . C&B page 619.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You realize this topic is more than two years old?
Ryan Patrick
Greenhorn

Joined: Sep 09, 2011
Posts: 1
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:

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic declarations "? super" Vs. "? extends"