• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in generics........

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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�?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<? super Integer> means it can allow Integer (or) any super class of an Integer..


am i right?
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<? 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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody explain this?



answer is B,E,F.


 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See the post Ch 7 Q 16 !

This question is explained there.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes just now i have seen errata.....

thanku so much srilatha for your quote...

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


reply
    Bookmark Topic Watch Topic
  • New Topic