Steve
For this purpose, regard ? super B as including B, so call B a "supertype" of itself.Originally posted by Abhi Bhutani:
5. public static<T> List<? super T> backwards(List<T> input)
Same as 4, if i invoke backwards(b) how am i ever going to return a List of supertype of class B
Originally posted by Abhi Bhutani:
Hi Steve,
Ignore option A for the moment. Just think of option B.
If I invoked backwards(List<MiniTerrier>) then result will be:
B. public static<MiniTerrier> List<MiniTerrier> backwards(List<? extends MiniTerrier> input)
in other words now T will be replaced by MiniTerrier? isn't it ?
[edit]Disable smilies. CR[/edit]
[ September 28, 2008: Message edited by: Campbell Ritchie ]
Steve
Originally posted by Steve Luke:
Yes, in that specific case, we are trying to understand the generic case, so lets keep with Terrier since we know something about its children...
Let's expand our class family a bit.
Class Dog
Class Shepherd extends Dog
Class GermanShepherd extends Shepherd
Class AnatolianShepherd extends Shepherd
Class Terrier extends Dog
Class MiniTerrier extends Terrier
Class Yorkie extends Terrier
When you have a List<? extends Terrier> what objects can you put in the List?
When you have a List<Terrier> what objects can you put in the List?
When you have a List<? super Terrier> what objects can you put in the List?
Then:
When you have a List<? extends Terrier> what kind of Lists can you pass to the method?
When you have a List<Terrier> what kind of Lists can you pass to the method?
When you have a List<? super Terrier> what kind of Lists can you pass to the method?
Finally for each of the above, what types of Objects can you safely pull out of the List, so that you can modify it or put it in another list? (safely means without having to cast or use instanceof)
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|