Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics Wildcard question

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 13870
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Master Rancher
Posts: 4190
57
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
Femi Byte
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 13870
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 13870
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your responses below clarify things quite a bit. Thanks for your responses.
 
rubbery bacon. crispy tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic