4

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 List<type> where type is either Number or super class of Number. Same logic holds true for List<? super Integer> also. So how can List<? super Number> be a sub type of List<? super Integer>?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
coderx
  • 509
  • 6
  • 22

1 Answers1

5

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

Joffrey
  • 32,348
  • 6
  • 68
  • 100