Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics question

 
adolfo bulfoni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys, I have some doubts on a question I took from one of the two practice tests (test a) of SCJP6 cd.

//INSERT CODE HERE
List<T> output = new LinkedList<T>();
for(T t: input)
output.add(0,t);
return output;
}



and here are the 4 correct answers:

public static <T> List bw(List <T> input){
public static <T> List bw(List <? extends T> input){
public static <T> List<? extends T> bw (List <T> input){
public static <T> List<? super T> bw (List <T> input){

i dont understand why the 3rd and the 4th are correct answers.. the output list is a list of <T> so why is it accepted as return value a list of <? super T> or a list of <? extends T> ?
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<? extends T> says T or a subclass of T;
<? super T> says T or a parent of T.

In the first case T definitely is <? extends T> and so is correct.
In the second case T definitely is <? super T> and so is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic