Here is what I know:
Double
is a subtype of Number
and List
is not a subtype of List
All your items are correct.
Double
is a subtype ofNumber
andList
is not a subtype ofList
.
List
is not a subtype ofList
because you can addCat
toList
but you can't do that withList
.
That's correct. Generics aren't covariant (but arrays are!). Here's some follow up reading: Why are arrays covariant but generics are invariant?
List extends Number>
means this list can store variables of typeNumber
and variables of subtype ofNumber
.List
means this list can store variables of typeDouble
.
This is true, but there's an important difference between List
and List extends Number>
. You can think of List extends Number>
as a list of a specific Number
-subtype (that is one of List
, List
, List
, ...) and a List
as a list that can potentially contain a mix of Double
, Integer
, ...
As for your final question:
Is
List
a subtype ofList extends Number>
...
Yes, you can have for instance
List doubles = new ArrayList<>();
List extends Number> numbers = doubles;
... and why?
This is just the way subtyping is defined.
As for the motivation, suppose you have a method that accepts a list of numbers. If you let the parameter have the type List
you won't be able to pass a List
to it. (Your second item in your question explains why!) Instead, you can let the parameter have type List extends Number>
. Since List
is a subtype of List extends Number>
it will work out.