Java Reference
In-Depth Information
Unlike a class, an interface type provides no implementation.
An interface type is similar to a class, but there are several important differences:
ȗ
All methods in an interface type are abstract; that is, they have a name,
parameters, and a return type, but they don't have an implementation.
ȗ
All methods in an interface type are automatically public.
ȗ
An interface type does not have instance fields.
Now we can use the interface type
Measurable
to declare the variables
x
and
maximum
.
public class DataSet
{
. . .
public void add(
Measurable
x)
{
sum = sum + x
.getMeasure()
;
if (count == 0 || maximum
.getMeasure()
<
x
.getMeasure()
)
maximum = x;
count++;
}
public
Measurable
getMaximum()
{
return maximum;
}
private double sum;
private
Measurable
maximum;
private int count;
}
Use the
implements
keyword to indicate that a class implements an interface
type.
This
DataSet
class is usable for analyzing objects of any class that implements the
Measurable
interface. A class implements an interface type if it declares the