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 doubt in generics example from kathy sierra Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in generics example from kathy sierra" Watch "doubt in generics example from kathy sierra" New topic
Author

doubt in generics example from kathy sierra

kaushal rathore
Greenhorn

Joined: Jul 29, 2007
Posts: 2
Given a method declared as:

public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:

// INSERT DECLARATIONS HERE

output = process(input);

Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile?

The correct answers are:

B. ArrayList<Integer> input = null;
List<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;


Doubt:
we can assign List<E> type to List<? super E> type
but can we assign conversly List<? super E> to List<E> as we are doing in above example because the method "process" is returning List<? super E> and in correct answers we are assigninig this returning value "List<? super E>" to a "List<E> output" refrence?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy Kaushal,

seems to be your first or second posting here so

Welcome to the Ranch!



You were right in saying that we cannot assign a List<? super E> to a List<E>.
But we are not doing that in the example.
The return type is List<? super E>
and we return an List<E> in all three examples.
But this is
assigning a List<E> to a List<? super E> and not
assigning a List<? super E> to a List<E>.

And <? super E> also include E itself, but I'm sure you knew that.

Yours,
Bu.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Also see the errata:

K&B SCJP Errata

Thanks,


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in generics example from kathy sierra