hi,
Option B is same as option A, since the argument which is passed to the method will be of type T.
import java.util.*;
public class BackLister
{
// INSERT STATEMENT HERE
//A.public static <T> List<T> backwards(List<T>input)
public static <T> List<T> backwards(List<? extends T>input)
{
List<T> output=new LinkedList<T>();
for(T t:input)
output.add(0,t);
return output;
}
public static void main(
String[] args) {
List<String> lst1=new ArraryList<String>( ); //#1
List<Object> lst2=new ArraryList<Object>( ); //#2
//more codes
System.out.println(backwards(lst1));
System.out.println(backwards(lst2));
}
}
when 1 and 2 passed to the method , T would either be of type String or of type Object.So option B is correct.
If we consider 2 parameters
public static <T> List<T> backwards(List<T>input,List<T> input1)
{ .... }
and call to backwads(lst1,lst2); would produce compiler error because T should be of same type.
? extends T denotes an unknown type that is subtype of T
public static <T> List<T> backwards(List<? extends T>input,List<T> input1)
{ .... }
call to backwads(lst1,lst2); would be successful because String is a subtype of Object.
? super T denotes a unkown that is supertype of T.In option C the supertype of T cannot be assigned to T in the for loop hence fails to compile.
option D,E returns subtype and supetype hence valid
[ September 19, 2008: Message edited by: sannuth kashikar ]