• 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

Generic wildcard lower bound (bug or erratum?)

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In his book, Java 2 v5.0 (TIGER) New Features, Herbert Schildt explains on page 44 that both upper and lower bounds can be established for generic wildcard arguments.

An upper bound is established using <? extends superclass>. Schildt explains that this "is an inclusive clause, because the class forming the upper bound (that is, specified by superclass) is also within bounds." This makes sense because an object that extends a superclass actually is an instance that superclass.

Similarly, a lower bound is established using <? super subclass>. And Schildt explains that "only classes that are superclasses of subclass are acceptable. This is an exclusive clause, because it will not match the class specified by subclass." This also seems to make sense, because a superclass is not an instance of the subclass.

However, the following code seems to contradict the lower bound being exclusive. Here, I've defined a simple hierarchy of classes: A, B, and C. Then I defined a generic class Gen with type parameter T. Then I defined two methods that take wildcard instances of Gen: One establishes an upper bound of B as Gen's type parameter, while the other establishes a lower bound of B as Gen's type parameter. The upper bound works as expected (as do other test instances I've left out of this example). The problem is that the lower bound version seems inclusive -- not exclusive.

Is this a bug in Java? Is this an erratum in the text? Or am I missing something?

(Edited with revised code for clarity.)
[ February 01, 2005: Message edited by: marc weber ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like this is an error in Schildt's book. From The Generics Tutorial, p. 18: "The syntax ? super T denotes an unknown type that is a supertype of T. ... Or T itself. Remember, the supertype relation is reflexive."
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
... "Remember, the supertype relation is reflexive."


Any class is a subclass of itself, and (thus) any class is a superclass of itself.

I think this concept can become troublesome when we consider that any subclass object is also an instance of any of its superclass types. But superclass objects are not instances of their subclass types except in the special case of being instances of the same class. This special case is often dismissed as academic, but I think the above situation concerning lower bounds demonstrates otherwise.
[ February 02, 2005: Message edited by: marc weber ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any class is a subclass of itself, and (thus) any class is a superclass of itself.

No actually, the JLS doesn't define it this way. Supertype and subtype are defined (in JLS3 beta 4.10) to be transitive and reflexive. Superclass and subclass relations are defined to be transitive but not reflexive. Basically, this means a class is a supertype of itself, and a subtype of itself, but it's not a superclass or subclass of itself.

This defiance of common sense has been brougt to you by the JLS 3rd edition beta. Ugh. I was also disappointed that the JLS3 did not bother to explain what was meant by "transitive and reflexive closure" by simply stating what the rules are, the way they did for subclass and superclass. Relying on such pretentious gobbledygook in lieu of a straightforward definition only serves to obfuscate things, IMO.

Anyway though, I don't see what instanceof has to do with it. instanceof is a relationship which is closely related to the subtype relationship, but isn't the same thing. There's no real parallel on the supertype side, and I can't see any real reason why there should be. We could create some term for aesthetic symmetry I suppose - but what use would such a term have?

This special case is often dismissed as academic, but I think the above situation concerning lower bounds demonstrates otherwise.

Ummm... how? As I've said, I see several problems with the current JLS presentation of this subject. But I don't really see what instanceof has to do with it - it's sort of off in left field, from my perspective. Care to elaborate?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Superclass and subclass relations are defined to be transitive but not reflexive. Basically, this means a class is a supertype of itself, and a subtype of itself, but it's not a superclass or subclass of itself.

Hmmm... I see I'll have to research this further. I'm having trouble grasping that.

instanceof is a relationship which is closely related to the subtype relationship, but isn't the same thing. There's no real parallel on the supertype side, and I can't see any real reason why there should be.

I think this is actually what I was trying to say: That mistakenly trying to "visualize" the reflexive property in terms of "instanceof" might be the reason it ends up being confusing. But I see that 1) my terminolgy was probably inaccurate; and 2) I'm apparently still in the murky depths myself.

(Off to do more research...) :roll:
 
Bras cause cancer. And tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic