File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in generics........

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • 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�?
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
M Srilatha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody explain this?



answer is B,E,F.


 
M Srilatha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the post Ch 7 Q 16 !

This question is explained there.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.....


 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic