Java Reference
In-Depth Information
The output is shown here:
In the program, notice these two calls to
absEqual( )
:
In the first call,
iOb
is an object of type
NumericFns<Integer>
and
dOb
is an object of
type
NumericFns<Double>
. However, through the use of a wildcard, it is possible for
iOb
to pass
dOb
in the call to
absEqual( )
. The same applies to the second call, in which an
object of type
NumericFns<Long>
is passed.
One last point: It is important to understand that the wildcard does not affect what type of
NumericFns
objects can be created. This is governed by the
extends
clause in the
Numer-
icFns
declaration. The wildcard simply matches any
valid
NumericFns
object.
Bounded Wildcards
Wildcard arguments can be bounded in much the same way that a type parameter can be
bounded. A bounded wildcard is especially important when you are creating a method that
is designed to operate only on objects that are subclasses of a specific superclass. To un-
derstand why, let's work through a simple example. Consider the following set of classes: