Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is List<? super Number> a subtype of List<? super Integer> ?

 
Jong Limb
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the exact quote from The Java Programming Language:

In contrast to the non-wildcard versions, parameterized types that include a bounded wildcard are
related in the way you might have expected. For example, List<?extends Integer> is a subtype
of List<?extends Number>, which is itself a subtype of List<? >. Similarly, List<?super
Number> is a subtype of List<?super Integer>.


I understand List<? extends Integer> being a subtype of List<? extends Number>, but I don't understand why List<? super Number> is a subtype of List<? super Integer>. Can someone explain this to me?
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A: List<? super Number> - could be a List<Number>, List<Object>, List<Comparable<Integer>>, or List<Serializable>

B: List<? super Integer> - could be a List<Integer>, List<Number>, List<Object>, List<Comparable<Integer>>, or List<Serializable>

All A are B. Not all B are A. Therefore, A is a subtype of B.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by super type and sub type? A super type variable can hold sub type object? OK? In your later case, why don't you think in that way?
 
Chuck Stephanski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
supertype = parent type
subtype = child type

the sub type is MORE restrictive, correct?

Integer is a subtype of Number and Integer is more restrictive. You can assign an Integer to a Number, but you cannot assign a Number to an Integer.

Likewise, you can assign a List<Integer> to a List<? super Integer> but you cannot assign a List<Integer> to a List<? super Number>. The more restrictive type is the subtype.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic