| Author |
Why is List<? super Number> a subtype of List<? super Integer> ?
|
Jong Limb
Greenhorn
Joined: May 08, 2004
Posts: 12
|
|
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
Joined: Mar 05, 2008
Posts: 2778
|
|
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
Joined: Nov 04, 2009
Posts: 2066
|
|
|
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?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Chuck Stephanski
Greenhorn
Joined: Aug 25, 2010
Posts: 5
|
|
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.
|
 |
 |
|
|
subject: Why is List<? super Number> a subtype of List<? super Integer> ?
|
|
|