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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K & B Mock exam Generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "K & B Mock exam Generics doubt" Watch "K & B Mock exam Generics doubt" New topic
Author

K & B Mock exam Generics doubt

Swati Khanna
Greenhorn

Joined: Sep 19, 2007
Posts: 16
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
hi swati,

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
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.
Yogvinder Singh Ghotra
Ranch Hand

Joined: Sep 10, 2007
Posts: 38
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!!!
Xander Steinmann
Greenhorn

Joined: Sep 18, 2007
Posts: 4
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
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
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>();"
Yogvinder Singh Ghotra
Ranch Hand

Joined: Sep 10, 2007
Posts: 38
Hi.. Swati..

Please go through the tutorial
http://java.boot.by/scjp-tiger/ch06s04.html

Here in java ranch, You can rad the thread
http://www.coderanch.com/t/265228/java-programmer-SCJP/certification/Generic-methods-wildcards

Hope this will help. If not, do tell me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K & B Mock exam Generics doubt
 
Similar Threads
Help me understand
Generics
Doubt
generics
Genrecis and Wildcard