Java Reference
In-Depth Information
2. The class must implement all the method headings listed in the defi nitions of the
interfaces.
For example, to implement the Ordered interface, a class definition must contain
the phrase implements Ordered at the start of the class definition, as shown in the
following:
public class OrderedHourlyEmployee
extends HourlyEmployee implements Ordered
{
The class must also implement the two methods precedes and follows . The full
definition of OrderedHourlyEmployee is given in Display 13.2 .
Display 13.2
Implementation of an Interface
1 public class OrderedHourlyEmployee
2 extends HourlyEmployee implements Ordered
3 {
4 public boolean precedes(Object other)
5 {
6 if (other == null )
7 return false ;
8 else if (!(other instanceof OrderedHourlyEmployee))
9 return false ;
10 else
11 {
12 OrderedHourlyEmployee otherOrderedHourlyEmployee =
13 (OrderedHourlyEmployee)other;
14
Although getClass works bet er than
instanceof for dei ning equals ,
instanceof works bet er in this case.
However, either will do for the points being
made here.
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 }
 
 
Search WWH ::




Custom Search