Java Reference
In-Depth Information
2. The class must implement
all
the method headings listed in the definitions of the
interfaces.
For example, to implement the
interface, a class definition must contain the
Ordered
phrase
at the start of the class definition, as shown in the following:
implements
Ordered
public class
OrderedHourlyEmployee
extends
HourlyEmployee
implements
Ordered
{
The class must also implement the two methods
and
. The full defi-
precedes
follows
nition of
is given in Display 13.2.
OrderedHourlyEmployee
Display 13.2
Implementation of an Interface
1
public class
OrderedHourlyEmployee
2
extends
HourlyEmployee
implements
Ordered
3{
4
Although
getClass
works better than
instanceof
for defining
equals
,
instanceof
works better here. However,
either will do for the points being made
here.
public boolean
precedes(Object other)
5
{
6
if
(other ==
null
)
7
return false
;
8
else if
(!(other
instanceof
HourlyEmployee))
9
return false
;
10
else
11
{
12
OrderedHourlyEmployee otherOrderedHourlyEmployee =
13
(OrderedHourlyEmployee)other;
14
return
(getPay() < otherOrderedHourlyEmployee.getPay());
15
}
16
}
17
public boolean
follows(Object other)
18
{
19
if
(other ==
null
)
20
return false
;
21
else if
(!(other
instanceof
OrderedHourlyEmployee))
22
return false
;
23
else
24
{
25
OrderedHourlyEmployee otherOrderedHourlyEmployee =
26
(OrderedHourlyEmployee)other;
27
return
(otherOrderedHourlyEmployee.precedes(
this
));
28
}
29
}
30
}