aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in generics........ 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 in generics........" Watch "doubt in generics........" New topic
Author

doubt in generics........

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
SOURCE:K&B chapter:7,p.no:634.

Here we can assign ArrayList<Integer> in to process method so E is Integer here�
We are returning List<Number> �
Actual return type is List<? Super Integer>.Number is super class of Integer then
Why it is giving compilation error�?


SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

The method will return <? super Integer>. Now it doesn't mean that it will return only Number. Integer's super types are Number, Serializable, Comparable and Object (through Number). So <? super Integer> could mean <Object> which cannot be stored in <Number>. This is why it gives compilation error...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Actual return type is List<? Super Integer>.Number is super class of Integer then
Why it is giving compilation error�?


Actually the compiler cant check whether the returned one is really a List<Number>. When List<? Super Integer> is used as return type, the method can return a List of any super type of Integer. So it can be List<Number> or List<Object> or some other super type of Integer.
So you can just assign the returned value to List<Number>!

Actually you can assign the returned value only to List<?> or List<? Super Integer>!

Hope this helps!
[ November 27, 2008: Message edited by: M Srilatha ]

Thanks,<br />Srilatha M
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
<? super Integer> means it can allow Integer (or) any super class of an Integer..


am i right?
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
I guess only Super type of Integer are allowed! But not sure.

[ November 27, 2008: Message edited by: M Srilatha ]
[ November 27, 2008: Message edited by: M Srilatha ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

<? super Integer> means Integer or it's super type are allowed. Also note that I have used the word type and not class as <? super Integer> means it can also accept super interface of Integer...
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
can anybody explain this?



answer is B,E,F.


M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
See the post Ch 7 Q 16 !

This question is explained there.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Ganesh the question that you have posted is wrong. I was wondering that how can B,E and F be the right answer. You posted this

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

while it is

public static <E extends Number> List<E> process(List<E> nums)
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
yes just now i have seen errata.....

thanku so much srilatha for your quote...

thanks to everyone for spending your valuable time.....


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