aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt related to use of wild card character ( ? super E) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt related to use of wild card character ( ? super E) " Watch "Doubt related to use of wild card character ( ? super E) " New topic
Author

Doubt related to use of wild card character ( ? super E)

Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Hi! Everyone,

I have a doubt in getting through question no 16. from K&B (page 634)

Given a method as

public static <E extends Number> List<? super E> process(List<E> nums)

A programmer want to use this method like this
// INSERT DECLARATIONS HERE

output=process(input);

which pair of declarations could be placed at //INSERT DECLARATION HERE to allow the code to compile.

A. ArrayList <Integer> input=null
ArrayList <Integer> output=null

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

C. ArrayList <Integer> input=null
List <Number> output=null

D.List<Number> input=null
ArrayList <Integer> output=null

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

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

G. None of these

Correct answers are: B,E and F

I have doubt related to C

ArrayList <Integer> input=null
List <Number> output=null

As the return type in the method declaration states

public static <E extends Number> List<? super E> process(List<E> nums) that means we can have super class of the value for E as generic type of List as return value.

so if i have ArrayList <Integer> input ,so corresponding to Integer for place holder E why can't i have List <Number > output as return value.

Please clarify in detail as i am not able to get the reason.


Regards

Jolly
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

I have also doubt about this question because I think correct answer is option G
because all other option are not accepted by compiler when I consider each options given

I think following is correct for variable declaration type
1.List<?>
2.List<? super E> where E is type we pass in as argument to method
3.List of raw type

Option G is correct answer

Author of this book i.e. author of SCJP study guide
please clear doubt on this problem
[ June 08, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
Originally posted by Jolly tiw:
Hi! Everyone,

I have a doubt in getting through question no 16. from K&B (page 634)

Given a method as

public static <E extends Number> List<? super E> process(List<E> nums)



The question is not that though, its :

public static <E extends Number> List<E> process(List<E> nums)


SCJP 5.0 - Passed
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Buddy i came across this link,

do have a look

http://www.coderanch.com/t/252493/java-programmer-SCJP/certification/Tough-Generics-book
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt related to use of wild card character ( ? super E)