This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WildCards in Generics

 
Amieya Prabhaker
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amirr Rafique
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<? 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic