It's not a secret anymore!*
The moose likes Java in General and the fly likes Generics Wildcard question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics Wildcard question" Watch "Generics Wildcard question" New topic
Author

Generics Wildcard question

Femi Byte
Ranch Hand

Joined: Jul 09, 2004
Posts: 40
I am wondering whether the statements below are true



Upon looking at Angelika Langer's generics FAQ, she states in the following section:

Is List<Object> a supertype of List<String>?

No, different instantiations of the same generic type for different concrete type arguments have no type relationship.


So, my assumption would be that the answer would be false for each of the above statements.

But later down in the same section of the FAQ, there is this statement:

Compatibility between instantiations of the same generic type exist only among wildcard instantiations and concrete instantiations that belong to the family of instantiations that the wildcard instantiation denotes.


which indicates to me that there is a super-sub type relationship for wildcards.
So which is correct for the examples above?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

List<? extends A> is the supertype of all lists of types that are subtypes of A. Therefore, List<? extends A> is the supertype of List<A>, as well as List<C>, if C extends A.

List<? super A> is the supertype of all lists of types that are supertypes of A. Therefore, List<? super A> is the supertype of List<A>, as well as List<Object>.

Wildcards always indicate supertypes.

[edited 10.000 times because the forum interprets my type arguments as bold text ]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2995
    
    9
Regarding the two quotes you posted, there is no contradiction. The first quote applies when both types are concrete, with no wildcards. List<Object> is neither a supertype nor subtype of List<String>. But the second quote applies where at least one of the types does use a wildcard.
Femi Byte
Ranch Hand

Joined: Jul 09, 2004
Posts: 40
.
Femi Byte
Ranch Hand

Joined: Jul 09, 2004
Posts: 40
Stephan van Hulst wrote:List<? extends A> is the supertype of all lists of types that are subtypes of A. Therefore, List<? extends A> is the supertype of List<A>, as well as List<C>, if C extends A.

List<? super A> is the supertype of all lists of types that are supertypes of A. Therefore, List<? super A> is the supertype of List<A>, as well as List<Object>.

Wildcards always indicate supertypes.

[edited 10.000 times because the forum interprets my type arguments as bold text ]


So you're saying that List<? extends A> is a supertype, not subtype of List<A>?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Yep!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Think of the IS-A test:

List<A> IS-A List<? extends A> ? - yes
List<? extends A> IS-A List<A> ? - no
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

Here is an example:

You can see that I can assign a List<Number> to both List<? extends Number> and List<? super Number> because they are both supertypes of List<Number>. In order to do the reverse, I need to cast.
Note that this code will compile with warnings, but it's perfectly valid.
Femi Byte
Ranch Hand

Joined: Jul 09, 2004
Posts: 40
Your responses below clarify things quite a bit. Thanks for your responses.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Wildcard question
 
Similar Threads
Generics question
Why this compilation error(Generics)
Generics
Generics
generics