• 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 with List assignment using Generics

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9708
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
see this example to see why compiler disallows this


[ November 21, 2008: Message edited by: Ankit Garg ]
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>();
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us the author of this code!

Thanks!
[ November 21, 2008: Message edited by: Bert Bates ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
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
@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...
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic