aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Tough Generics question from S&B 5.0 book 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 "Tough Generics question from S&B 5.0 book" Watch "Tough Generics question from S&B 5.0 book" New topic
Author

Tough Generics question from S&B 5.0 book

Andrew Shumway
Greenhorn

Joined: Jan 05, 2006
Posts: 18
I posted this a couple of days ago but didn't get any responses. I've tried to make this explaination a bit clearer in case it was difficult to follow before.

Here is the question as presented in chapter 7 of the new SCJP book by Sierra and Bates



I have two problems with the answer. First, I don't understand why C is wrong. My take on <? super E> is that the returned List can by of any super type of E. Since E would be <Integer> then <Number> should work.

My second problem is that in an effort to play with these constructs I made a little program. I can't even get the "correct" versions to work.

Here is the code



Cases A and F give this compiler error

Type mismatch: cannot convert from List<capture-of ? super Integer> to List<Integer>


Case C gives this compiler error

Type mismatch: cannot convert from List<capture-of ? super Integer> to List<Number>


Case E gives this compiler error

Type mismatch: cannot convert from List<capture-of ? super Number> to List<Number>



So as near as I can tell, all of these fail for essentially the same reason but I don't know WHY?

Anyone kind enough to point out where I've run amuck will be most appreciated.

Thanks
Barry Bassi
Ranch Hand

Joined: Dec 20, 2005
Posts: 49
It's a very interesting question!
You should contact directly Bert Bates, but I think he's deservedly going into hibernation after the SJCP5 Study Guide publication

B.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, MySQL 5 Developer, ZCE, EUCIP Core<br /> <br />OCA 9i (in progress..)<br />SCJD UrlyBird (in progress..)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Using JDK 1.5.0_02, this is the compiler error for option C.

Test.java:12: <E>process(java.util.List<E> in Test cannot be applied to (java.util.ArrayList<Integer>
output = process(input);
^
1 error
Frederico Benevides
Greenhorn

Joined: Jan 15, 2006
Posts: 25
public static <E extends Number> List<? super E> process(List<E> list)

What's the meaning? My problem is to understando this (? super E).

What I understand is that List will need to have anything that extends from number, but and the meaning of (? super E) what will it be?

thank you.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
As it happens, this is a question that I wrote. And somehow I managed to mangle it just before submitting it, and the error went undetected until now. The original, tested, correct version of this question used the following declaration for process():

Note the return type is List<E>, not List<? super E>. With this version, the answers given are correct, and the explanations should make more sense.

I'm not sure what happened here - I remember experimenting a bit with the form of the question, to see if there was a way the question could also involve using super. But I couldn't find a way to do this that made sense, so I decided to go back to the original version of the question. Except somehow I accidentally left behind the <? super E> from an alternate version of the question. Oops.

So anyway, just replace List<? super E> with List<E>, and the question works as it was originally intended. And we'll get this listed in the errata shortly.


"I'm not back." - Bill Harding, Twister
Hamid Virani
Ranch Hand

Joined: Jul 26, 2005
Posts: 41
But, if we dont change the method signature, i.e. keep it as


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.

As I understand since E extends Number, lets assume that E is Integer. So ? super E implies that output list should be List<Number> or List<Integer>, since ? super E implies that it should either be of type E or a super class of E.
Therefore,
List<Integer> input = null;
List<Number> output = null;

output = process(input);
should work
or even for
List<Integer> output = null;
should work.

But, it is not working

Why is this happening and how should it work?
Can anyone tell me if my understanding is correct or I am missing something?
[ January 30, 2006: Message edited by: Hamid Virani ]
Andrew Shumway
Greenhorn

Joined: Jan 05, 2006
Posts: 18


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.
[ January 30, 2006: Message edited by: Hamid Virani ][/qb]<hr></blockquote>

Using the Generics class above the following declarations work



Because Number both extends Number and is super of Number



Like the above I can see that the input list will work because Number extends Number. However I can't see List<? super Integer> qualifies when the process method will return List<? super Number> However, I don't get a compile error. I'm guessng that since all super classes of Number are also super classes of Integer it's ok.



This also works for the same reason as the first.


--andrew
[ February 02, 2006: Message edited by: Andrew Shumway ]
elPete Martin
Greenhorn

Joined: Apr 27, 2006
Posts: 5
with the aswere B I get

Type mismatch: cannot convert from List<capture-of ? super Integer> to List<Integer>

what about you??
Ken Loh
Ranch Hand

Joined: Feb 16, 2005
Posts: 190
Thanks, I've checked all of your suggestions and have found them working.

Originally posted by Andrew Shumway:


Then, what version of List should be given to make it work. I tried, but I could not get anything to work.
[ January 30, 2006: Message edited by: Hamid Virani ]<hr></blockquote>

Using the Generics class above the following declarations work



Because Number both extends Number and is super of Number



Like the above I can see that the input list will work because Number extends Number. However I can't see List<? super Integer> qualifies when the process method will return List<? super Number> However, I don't get a compile error. I'm guessng that since all super classes of Number are also super classes of Integer it's ok.



This also works for the same reason as the first.


--andrew

[ February 02, 2006: Message edited by: Andrew Shumway ][/QB]
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233

by Jim Yingst

...correct version of this question used the following declaration for process():

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


FYI, I check my book (ISBN 0-07-225360-6) and it has the correct version.

Richard
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tough Generics question from S&B 5.0 book