Java Reference
In-Depth Information
where
superclass
is the name of the class that serves as the upper bound. Remember, this
is an inclusive clause because the class forming the upper bound (specified by
superclass
)
is also within bounds.
You can also specify a lower bound for a wildcard by adding a
super
clause to a wildcard
declaration. Here is its general form:
<? super
subclass
>
In this case, only classes that are superclasses of
subclass
are acceptable arguments. This is
an inclusive clause.
Generic Methods
As the preceding examples have shown, methods inside a generic class can make use of a
class' type parameter and are, therefore, automatically generic relative to the type paramet-
er. However, it is possible to declare a generic method that uses one or more type paramet-
ers of its own. Furthermore, it is possible to create a generic method that is enclosed within
a nongeneric class.
The following program declares a nongeneric class called
GenericMethodDemo
and a
static generic method within that class called
arraysEqual( )
. This method determines if
two arrays contain the same elements, in the same order. It can be used to compare any two
arrays as long as the arrays are of the same or compatible types and the array elements are,
themselves, comparable.