Suppose the compiler let you do that and that you now have a List of Numbers in list1. Now lets say you want to add a Number to the list, let's say.... new Double(338.67). There's no one stopping you, so congratulations, you now have put a Double in a List that was originally supposed to consist of Integers.
You can fix the compiler error by using "? extends Number" instead of "Number" but remember that you still can't add anything to the list.
Here's my take on this (from what I understand from Angelika Langer's Generics FAQ):
The reason List<? extends Number> cannot be assigned to List<Number> is that List<? extends Number> represents a whole family of types, and you cannot assign a family to one specific member of that family.
How about asigning List<? extends Integer> to List<Integer>. There's no danger of adding a Double to a List<Integer>, since Integer is final. Hence, there is no family of types that can possibly be added to List<? extends Integer> as it can be to List<? extends Number>.
But this still doesen't work out. Why so?
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Joined: Aug 03, 2002
How about assigning List<? extends Integer> to List<Integer>?
As you noticed this is not allowed either. List<Integer> is a subtype of List<? extends Integer> (The Java Programming Language, 4th Edition p. 258) and the assignment of a reference to a supertype to a reference of a subtype is not allowed.
You can cast a List<? extends Integer> reference to a List<Integer> reference - but you then get an "unchecked" warning.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Wild card ? extends Number in return of a method ?