| 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: 2341
|
|
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: 1295
|
|
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: 1295
|
|
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: 2341
|
|
Sure, it's the same. I was just modifying the way they had it in the original statement.
|
 |
 |
|
|
subject: Generic methods question
|
|
|