Because Say Animal Arraylist can containg dog,cat objects so you can pass an animal and then in the method can return something that extends Animal. say cat.... which on returning to the main method can be used by the cat reference variable without the cast..

Ankit,
Sorry this was my first post so didn't know what exactly to do. Actually this question is from Master Exam that comes with SCJP 5 by Kathy Sierra/Bert Bates..

Nitish
Thanks for your reply. I thought the same as you. But when I tried to compile it, It did not work for me. Line 1 gave me problems.
This is what I have tried, Please tell me If I did something wrong here

D has a more broader prospect thats all. In A you can only pass T= Animal,nothing else.You cannot pass Dog ,Cat, Wolf which are sub classes of Animal. If you try to pass a sub class of T then compiler will complain.

In D you can pass anything which is a subclass of T(Dog, cat,wolf) n the compiler will not complain. The only thing need to take care is you cannot add any thing to the list if you pass a sub class i.e in <? extends T> because then the compiler will complain.
If we want to add then <? super T> should be used but then here also T should be a subclass and the list that is passed should be <supertype of T>

michael muris
Greenhorn

Joined: Jul 26, 2009
Posts: 2

posted Jul 26, 2009 12:43:18

0

@Nitish: The difference between D and A is in the declared type of the return value. In both cases I believe I can add anything that implements T to the returned array.
Can you provide an example of something that is possible with the D answer but not with the A answer?

michael muris wrote:@Nitish: The difference between D and A is in the declared type of the return value. In both cases I believe I can add anything that implements T to the returned array.
Can you provide an example of something that is possible with the D answer but not with the A answer?

I am not sure if this is the correct way to think about it -- there is really no reason to return a bounded type, if it is possible to specify exactly what you want to return.

But what if you can't specify exactly what you are going to return? What if, given a type T, the best you can do is return some type that is of something that extends T?

But see in A.....you have an Animal List.....Animal class is extended by Dog and Cat. So in A you can add and return only animal. And yes in the called method it should be taken in by the <Animal> variable only.

In D you can pass an Animal List and in the method can add anything which is a Dog or a Cat and you can return Dog or Cat also.Cause it can return anything that extends Animal. But in the called method it should be taken in by the <Animal> variable only because you really cannot be sure which subtype is going to come out when you say <? extends Animal>.... Atleast the compiler wont be sure.

If one generically declares return type <T> to a method, then that is exactly what must be returned.
I was under the impression that polymorphic covariant returns must be explicitly declared in generics.
This means that if you wish to return a covariant ('is a' ) object of the declared <T> one
must add <? extends T> (upper bounds) declaration. In an example, the following should apply (in my interpretation).

However, where I become foggy is in the parameter passing and manipulation area in regards to bounds. Here is my current
understanding, anyone feel free to jump in ans correct me at any time!

be a well encapsulated person, don't expose your privates, unless you public void getWife()!

in this yes normally with void return type you can add or remove. Also when the list you are passing as an Argument is a super of T. That is Animal is the list you are passing and accepting parameter is List<? super Dog> input. o k understood.

But if you accept Animal i.e <? super Dog> within the method T => Dog, there is no way you can add it to the List<T> (List<Dog>) and then return as List<T>

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352

posted Jul 28, 2009 04:00:00

0

..Yes, the returning for me I must read up on. Ignoring the return for now however, if you follow the lower bounds argument here:
replacing Type T with Dog, and assuming Dog extends (or implements) Animal, and of course, implicitly Object:

So in each of these cases the compiler reads ok the reference types in this list can be a Dog or higher, so
at a minimum (lower bound) the list will be a Dog, and that's fine as that's the Type declared, so feel free to add / remove
from the List.

However the return type of the method, is a different matter, I would suppose if you were to return a List it must be a list of Dogs, here I am foggy, however in this case I am simply talking of the argument type, NOT the return type.