• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic methods question

 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't list the extends clause like that.

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

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its because T has not been declared a type variable for the method. If the method signature was:



there would be no compilation error.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly,

Let me add...


Always wrong syntax!!!


Regards,
cmbhatt
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, it's the same.

I was just modifying the way they had it in the original statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic