Java Reference
In-Depth Information
bers between 0 and
N
. Because
Summation( )
specifies a type parameter that is bounded
by
Number
, a
Summation
object can be constructed using any numeric type, including
Integer
,
Float
, or
Double
. No matter what numeric type is used, its value is converted to
Integer
by calling
intValue( )
, and the summation is computed. Therefore, it is not neces-
sary for the class
Summation
to be generic; only a generic constructor is needed.
Generic Interfaces
As you saw in the
GenericMethodDemo
program presented earlier, an interface can be
generic. In that example, the standard interface
Comparable<T>
was used to ensure that
elements of two arrays could be compared. Of course, you can also define your own gener-
ic interface. Generic interfaces are specified just like generic classes. Here is an example.
It creates an interface called
Containment
, which can be implemented by classes that store
one or more values. It declares a method called
contains( )
that determines if a specified
value is contained by the invoking object.