• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Collections

 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help understand this question. It is a question at the end of chapter in Kathie Sierra book.

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are trying to learn, think what the type of input can be (i.e: things can be assigned to -> List<E>) and what the type of the output should be (i.e: to what we can assign a return type of List<? super E>) given <E extends Number> satisfies.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Giffy,
Welcome to JavaRanch!!!
I think this question is asked before and well discussed............
In fact many questions we ask are mostly already asked..........You should get it if you search this same forum
I'll see if I can get it and post the link if possible..........
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for the reply. I found the similar post.

Here is a piece that I would like to know how is it happening? Please help me here.

Nikos’ Java blog

Both result in Incompatible types compile error.

If we take q1 as <Number> then q2 can be <Number> too. So will not the assignment be a valid one.

In other case when q1 is <Integer> then q2 can also be <integer>.

I am little lost here so I may be sounding that way.

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giffy Geraldo wrote:If we take q1 as <Number> then q2 can be <Number> too. So will not the assignment be a valid one.


But will that be always true? If q1 actually refers to an object of generic type LinkedList<Float> (which is perfectly all right) is it possible to assign that object to a reference of type Queue<? super Integer> (which permits only Integer or any super type) ? That's wrong right?

You can think about the other query which can by thought in the same way. Hope you get it
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should read this...
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vijitha for the science. Thanks Ankit for the link.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And maybe this could also help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic