File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic methods question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic methods question" Watch "Generic methods question" New topic
Author

Generic methods question

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi all,

Can anyone please explain why D. is not valid.


Which two, inserted independently at line 15, allow the file to compile?

A. public static void takeCars(List<?> list) { }
B. public static void takeCars(List<Object> list) { }
C. public static void takeCars(List<? extends Car> list) { }
D. public static void takeCars(List<T extends Object> list) { }
E. public static void takeCars(List<? extends Object> list) { }

Source: Sun Practice exams.
Ans : A,E

[ April 26, 2007: Message edited by: megha joshi ]
[ April 26, 2007: Message edited by: megha joshi ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You don't list the extends clause like that.

If you modify d to be this, it will compile.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Its because T has not been declared a type variable for the method. If the method signature was:



there would be no compilation error.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Garrett Rowe:
Its because T has not been declared a type variable for the method. If the method signature was:



there would be no compilation error.

Sorry, that won't work either. Thats what I get for trying to post before I tested. Keith's answer is the correct one.

This will also work

[ April 26, 2007: Message edited by: Garrett Rowe ]
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Thanks for explaining everyone...

So to sum up..

1) If <?> or <? extends something> or <? super something> is used in method's args its not declaring the type and hence its okay.
2) If anything other than ? or like <T> or <T extends something> is used then in method args then , it is a type declaration and must be done before the return type of the method or in the class type declaration.Just <T> should be written in the method args in this case...for eg.

public <T extends Integer> void methodA(<T >
or
public <T> void methodA(<T >

Let me know if I am mistaken..
Thanks again!!
[ April 26, 2007: Message edited by: megha joshi ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Exactly,

Let me add...


Always wrong syntax!!!


Regards,
cmbhatt


cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Keith wrote:
If you modify d to be this, it will compile.




isn't that the same as


?
Will compile, by the way.

Bu.


all events occur in real time
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Sure, it's the same.

I was just modifying the way they had it in the original statement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic methods question
 
Similar Threads
Generics doubt
Question about instanceof
Implementing
Generics
Generics Question: