aspose file tools*
The moose likes Java in General and the fly likes Why is List<? super Number> a subtype of List<? super Integer> ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is List<? super Number> a subtype of List<? super Integer> ?" Watch "Why is List<? super Number> a subtype of List<? super Integer> ?" New topic
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: 2969
    
    9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is List<? super Number> a subtype of List<? super Integer> ?
 
Similar Threads
Wildcard
Generics/wildcards in variable declaration
Need generic clarification
Generics - return types
Why generic wildcard could bypass the type safety?