This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
3. public static <T>List<? extends T> backwards(List<T> input) 4. public static <T> List<? super T> backwards (List<T>input)
I think both the options are correct, since in 3 we are returing a list of T or or one of it's child class. And in the 4, we are returning a list of T or one of it's parent class.
We are not calling that function from anywhere in that fragement of code. I think your confusion is like this "How can we return a reference of parent class where a child class reference is expected" for option 4.
And option 3 is correct anyway since we can return reference of child class where parent class reference is expected.
Swati Khanna
Greenhorn
Joined: Sep 19, 2007
Posts: 16
posted
0
My confusion is that in the method backwards we are returning List<T> and the return type of the method is List<? extends T> or List<? extends super> in option 3 and 4. So isn't it like are trying to assign a List<Object> to List<String> or vica versa. I hope I make sense.
First of all, i think List<? extends T> is different from List<Object>.
List<Object> can only be assigned List implementations with generic type Object where as List<? extends T> can take any implementation with generic type T or anything that extends T or implements T(in case of T being an interface).
Second, return type is List<? extends T> and you are returning List<T> List<? extends T> t = ArrayList<T>(); is possible I think.
Similarly the case with <? super T>. Hope this helps!!!
I think that you have a very interesting question... And I don't think that the last two implementations are very useful. Look at the classes Animal, Dog and Cat (extend Animals).
Now I input a List of Animals into the 3rd backwards method, and the return type is List<? extends Animal>. Now, what is my return type? A list of Animals? a list of Dogs? a list of Cats and Dogs? you can't tell, but you also can't put the outputted list in a List<Animal> (because it could be a List<Dog> )! You can only put the list in a List<? extends Animal> or an Object.
The same goes for the 4th backward method (is it a List<Object> or List<Animal>?). I think that it is more useful to use 2 specified generics instead of just one, like this:
With this code you can input a List<Animal> and get a List<Dog>, List<Cat> or List<Beagle> in return (it's probably not perfect but illustrates the use of multiple generics).
[ September 19, 2007: Message edited by: Xander Steinmann ] [ September 19, 2007: Message edited by: Xander Steinmann ]
Swati Khanna
Greenhorn
Joined: Sep 19, 2007
Posts: 16
posted
0
Hi Yogvinder,
So does 'nt this mean that you are trying to write List<String> t = ArrayList<Object>(); as String extends Object where T is Object.
I am still not clear.
Xander Steinmann
Greenhorn
Joined: Sep 18, 2007
Posts: 4
posted
0
So does 'nt this mean that you are trying to write List<String> t = ArrayList<Object>(); as String extends Object where T is Object. I am still not clear.
No, "List<? extends T> t" means that t is a list of Objects that extend T. If you use T=Object then it says: List<? extends Object> t = new List<Object>(); This doesn't mean that you can fill in just anything for <? extends Object> (like String). List t has as type "? extends Object", not String. But you CAN say: "List<? extends Object> t = new ArrayList<String>();"