• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with List assignment using Generics

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see he code below:



Why is List<Gen1> list4 = (List<Gen1> ) list1; above, a compilation error?
[ November 21, 2008: Message edited by: Satya Maheshwari ]
 
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
see this example to see why compiler disallows this


[ November 21, 2008: Message edited by: Ankit Garg ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<Gen1> list4 = (List<Gen1> ; ) list1; //Error. WHY??


List<? extends Gen2> list1 = null;

You may be thinking that since Gen2 is a subclass of Gen1 it should compile, if so. See below code and think why it won't compile.

List<Object> list = new ArrayList<String>();
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us the author of this code!

Thanks!
[ November 21, 2008: Message edited by: Bert Bates ]
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above code why won't the following compile?
List<Gen1> list5 = (List<Gen1> ) list2; //Error
List<? super Gen2> list2
because list2 is bounded by Gen2 and its super classes. Gen1 is super for Gen2 and you should be able to assign list2 to list5.
Kindly explain.
Thanks,
Meera
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us the author of this code!


I myself wrote this code, though I think using this anywhere would be a crime The sole purpose is to teach myself generics.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by meera kanekal:
In the above code why won't the following compile?
List<Gen1> list5 = (List<Gen1> ) list2; //Error
List<? super Gen2> list2
because list2 is bounded by Gen2 and its super classes. Gen1 is super for Gen2 and you should be able to assign list2 to list5.
Kindly explain.
Thanks,
Meera


List2 contents are anything which is a superclass of Gen2. If Gen2 were 'Green Apple' and 'Gen1' were Apple, List2 contents would be any kind of 'Fruit', which does not mean that List2 contents are 'Apples' only. Hence its an error. I hope I am not too cryptic here
 
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
@meera Let's take an example to see why the compiler doesn't allow this



So as you can see if the assignment was allowed, then you will have a ClassCastException at your hand. This is the whole thing that we try to avoid by using Generics...
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic