Java Reference
In-Depth Information
interface in an
implements
clause. It should then implement the method or
methods that the interface requires.
class ClassName
implements
Measurable
{
public double
getMeasure()
{
Implementation
}
Additional methods and fields
}
390
391
A class can implement more than one interface type. Of course, the class must then
define all the methods that are required by all the interfaces it implements.
Let us modify the
BankAccount
class to implement the
Measurable
interface.
public class BankAccount
implements Measurable
{
public double getMeasure()
{
return balance;
}
. . .
}
Note that the class must declare the method as
public
, whereas the interface need
notȌall methods in an interface are public.
Similarly, it is an easy matter to modify the
Coin
class to implement the
Measurable
interface.
public class Coin
implements Measurable
{
public double getMeasure()
{
return value;
}
. . .
}
In summary, the
Measurable
interface expresses what all measurable objects have
in common. This commonality makes the
DataSet
class reusable. Objects of the
DataSet
class can be used to analyze collections of objects of any class that