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 Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic
Author

Generics

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is from Master Exam.


}


Choose the correct statement to be inserted.

A.public static <T> List<T> backwards(List<T>input)
B.public static <T> List<T> backwards(List<? extends T>input)
C.public static <T> List<T> backwards(List<? super T>input)
D. public static <T> List<? extends T> backwards(List<T>input)
E. public static <T> List<? super T> backwards(List<T>input)
F. public static List<? extends T> backwards(List<T>input)
G. public static List<? super T> backwards(List<T>input)


The correct answer is A,B,D,E.

I understand F,G are wrong because <T> is not defined.

A.public static <T> List<T> backwards(List<T>input)
This is correct because it returns <T> .

D. public static <T> List<? extends T> backwards(List<T>input)
E. public static <T> List<? super T> backwards(List<T>input)
D,E also seems correct as the return type is <? super T> and <? extends T >

B.public static <T> List<T> backwards(List<? extends T>input)
But i thought B is wrong as it is trying to add to a list which extends from T and you cannot add to such a list


C.public static <T> List<T> backwards(List<? super T>input)
And the explanation for C is given that ( which I thought should be for B is:

C is wrong because if the input can contain any supertype of C (like Object) those elements cannot be added to an output list of type T.

Can some please help me understand this.
I am not even clear with my own explanation.Still hazy with that.
[ September 19, 2008: Message edited by: Nabila Mohammad ]

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
sannuth kashikar
Greenhorn

Joined: Sep 16, 2008
Posts: 14
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 ]

scjp5 90%
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Hi All,

I am having the same issue as stated in this question.... I don't understand the reasoning behind options B and C.

I have a simple question to ask, which I think I am not clear on. When we have the below method, Option C:


Does this mean that return type List<T> output and the argument List<? super T> input
have the same place holder type?
Just to clarify my question: If <T> is Integer then output is List<Integer> and input will be List<Number> ???
OR both will be same: output is List<Integer> and input will be List<Integer>

Also for Option B:


If output is List<Number> is input List<Integer>
OR if <T> is Number output is List<Integer> is input List<Integer> ??


Thanks.
Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
Hi All,

Please reply to query below... I know some of you maybe thinking what a silly question, sorry...
but this will really help me clear up the generics concepts! Thanks.
Albert Kam
Ranch Hand

Joined: Oct 18, 2007
Posts: 58
Hello ..

I'm still learning generics too. I want to share my current experiment n conclusion so far. Any corrections and thoughts would be very appreciated !

Here's my experiment code :


And here's my temporary conclusions ..

Conclusion for the List<? super MyType>

What you can reference :
- Any lists without the specific parameterized type
- Any lists with any specific parameterized type which is equal or above the declared MyType

What you can add :
- Only the MyType objects

What you can get :
- Only Object type (just like the legacy lists), which means you'll have to downcast to make it a specific type

Cheers,
Albert Kam


Be nice !
Byju Joy
Ranch Hand

Joined: Sep 06, 2005
Posts: 84
I agree with Albert
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Nabila Mohammad wrote:
B.public static <T> List<T> backwards(List<? extends T> input)
But i thought B is wrong as it is trying to add to a list which extends from T and you cannot add to such a list


How did you came to such a conclusion. The code in the method doesn't try to add anything to the input list



Here what you are doing is, you have created a new list, then iterate over the input list i.e. get elements from it and add them into the output list. This is perfectly legal here. The list input will have elements which are a sub-type of T. So whatever it is, it will fit into a T reference. So that's why the loop will work. It is like doing something like this



Thus we are sure that elements in the input list are of type T or sub-type of T. Thus in the method (of your original code), we create a new list of type T and add all the elements of the input list into the new list and return it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics