It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes WildCards in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "WildCards in Generics" Watch "WildCards in Generics" New topic
Author

WildCards in Generics

Amieya Prabhaker
Ranch Hand

Joined: Apr 23, 2006
Posts: 45
Can someone explain the deal with wildcards in generics?

Pg.594 SCJP5 Sierra & Bates has an example with comment:
//adding is sometimes OK with super.
I'm somewhat confused with the word "sometimes"??

Also the deal with the ability to add (/NOT add) anything which has a ? wildcard type.
1.) <? extends ABCD>
2.) <? super ABCD>
3.) <?>

Thanks guys in anticipation.
Shawn Kuenzler
Ranch Hand

Joined: Apr 16, 2006
Posts: 73
I *think* it's only legal to add to a generic collection with the wildcard if it's the return type.

public <T> List<? extends Number> someMethod(ArrayList<T> list){}

I think this method can add to the List before it returns it.

But when you have say:

List list = new ArrayList<? extends Number>();

you can't add to this collection.

Don't have 1.5 here at work so my syntax may be wrong. Can someone confirm or deny if logic is correct?

Also, the K & B book did state the following:

1.) <? extends ABCD> -- can't modify
2.) <? super ABCD> -- can modify
3.) <?> -- can't modify
[ May 22, 2006: Message edited by: Shawn Kuenzler ]

SCJP 1.5
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
<? extends ABCD>

List <? extends ABCD> can refer to any list of ABCD subclass

<? super ABCD>

List <? super ABCD> can refer to list of any super class of ABCD

<?>


List <?> can refer to List of any object.


"Know where to find the solution and how to use it - that's the secret of success."
 
jQuery in Action, 2nd edition
 
subject: WildCards in Generics
 
Similar Threads
Generics <?>
Generics
generics
Generics
Generics