but then how exactly List<? extends Number>
is a sub type of List<? extends Number>
?
They are the same type! Did you mean:
but then how exactly List<? extends Integer>
is a sub type of List<? extends Number>
?
List<T>
means "a list of T
s", whereas List<? extends T>
means "a list of some unknown type, but I know it's a subclass of T
". In other words, what List<Number>
is is "set in stone", whereas List<? extends Number>
can be one of many things: List<Integer>
, List<Double>
, List<Number>
and so on. We don't know.
If you have understood this nuance, you shouldn't find the fact that you can't add Integer
s to a List<? extends Number>
surprising. Why? Because the List<? extends Number>
could very well be a List<Double>
. You can't add Integer
s to List<Double>
, can you?
Now the answer to the question should be clear, List<? extends Integer>
could be a list of any subclass of Integer
, whereas List<? extends Number>
could be a list of any subclass of Number
. Since, Integer
extends Number
, any list of a subclass of Integer
must also be a list of a subclass of Number
.
But note that a list of Integer
is not a list of Number
, since you can't add Double
s to a list of Integer
, but you can to a list of Number
.
How List<Number>
is sub type of List<? extends Number>
?
This is kind of "by definition" of the <? extends T>
syntax. There is no reason not to allow <T>
itself. And I do agree extends
is misleading here.
Also note that this is not really "inheritance". All the types involved here are really just the same JVM type List
, which is also an interface. Inheritance happens between classes. Your wording in the question body - "subtype" - is more appropriate.