coderx coderx - 3 months ago 45
Java Question

Confusion in generic List<> hierarchy

Wildcards and Subtyping - Oracle Documentation

This document has a diagram shows the relationships between several List classes declared with both upper and lower bounded wildcards. The relationship as depicted in the picture below:

Generic list hierarchy

In the right side hierarchy,
List<? super Number>
is sub type to
List<? super Integer>
. Isn't it confusing?

As far as I interpret,
List<? super Number>
can be represented by any
is either
or super class of
. Same logic holds true for
List<? super Integer>
also. So how can
List<? super Number>
be a sub type of
List<? super Integer>


This is because List<? super Number> can only hold Number and its super classes, while List<? super Integer> can hold the same + integers.

You can think about it this way: if the item type is more restrictive, then a list of such items is lower in the inheritance hierarchy. You can put a List<? super Number> in a variable of type List<? super Integer>, but not the other way around.

Look at how the item types include each other, this might help you:

enter image description here