File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with List assignment using Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with List assignment using Generics" Watch "Doubt with List assignment using Generics" New topic
Author

Doubt with List assignment using Generics

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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 ]

Thanks and Regards
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

see this example to see why compiler disallows this


[ November 21, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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>();


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Please tell us the author of this code!

Thanks!
[ November 21, 2008: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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

Joined: Jan 01, 2007
Posts: 368
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

Joined: Jan 01, 2007
Posts: 368
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

@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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with List assignment using Generics