I'm a bit confused about how Java generics handle inheritance / polymorphism.
It's fully possible to classcast Number to Integer but it seams not possible to classcast List<Number>
to List<Integer>
.
Why ?
Here is my java code sample :
Number n = new Integer(5);
Integer i = (Integer) n; // No compilation or runtime error ==> for me it's normal
Number n = new Long(5);
Integer i = (Integer) n; // No compilation error but have a runtime error (ClassCastException) ==> for me it's normal
List<Number> nList = new ArrayList<Number>();
nList.add(Integer.valueOf(5));
List<Integer> iList = (List <Integer>) nList; // Compilation error ==> WHYYYY ?!!
Thank's for your help.
EDIT :
This question is completely different from this one Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic? because my question is not why it's mandatory to write the class cast.
To be more specific, I call a function that returns a List<Change>
where Change
is an abstract super class so this list can contain ChangeItem
or ChangeList
or whatever subclass of Change
depending on my request (parameters).
I know my request makes this function returning only ChangeItem
so I want to convert it easily to return it from my function. I was thinking a class cast was the simplest way to do but it seems not possible because of compilation error.
Why it's possible to write and compil this :
Number n = new Long(5);
Integer i = (Integer) n;
While it's impossible to write and compil this :
List<Number> nList = new ArrayList<Number>();
nList.add(Integer.valueOf(5));
List<Integer> iList = (List <Integer>) nList;
I just want the same ClassCastException if nList contains other things than Integer.