• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 5122
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Those are the largest trousers in the world! Especially when next to this ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic