From this Oracle tutorial,
Although
Integer
is a subtype ofNumber
,List<Integer>
is not a subtype ofList<Number>
and, in fact, these two types are not related.The common parent of
List<Number>
andList<Integer>
isList<?>
.
My question is about the second sentence. How can we say that List<?>
is the common parent of List<Number>
and List<Integer>
?
?
stands for an unknown type, which could be any reference type. Even if I say that ?
would be Object
here, Object
being the common parent of Integer
and Number
does NOT mean that List<Object>
becomes a common parent of List<Integer>
and List<Number>
.
List<?>
means that you don't know what its type is (and you don't care either). So anyList<>
is aList<?>
.List<Integer>
can be assigned toList<?>
freely i.e.List<?>
can be seen as a parent ofList<Integer>
. The same goes forList<Number>
. So in a way,List<?>
can be seen as a common parent.List<?>
class then think about contract thatList<?>
requires (we can get reference type, we can put onlynull
s, we can ask forsize()
). Then ask yourself ifList<String>
fully implements this contract, the answer is yes