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 ]