Java Reference
In-Depth Information
Display 8.6 Employee Class and Its Derived Classes (part 2 of 2)
1 public class HourlyEmployee extends Employee
2 {
3
private double wageRate;
4
private double hours; //for the month
5 /**
6 Returns the pay for the month .
7 */
8 public double getPay()
9 {
10
return wageRate * hours;
11 }
12 public boolean equals(Object otherObject)
13 {
14 if (otherObject == null )
15 return false ;
16 else if (getClass() != otherObject.getClass( ))
17 return false ;
18 else
19 {
20 HourlyEmployee otherHourlyEmployee =
21 (HourlyEmployee)otherObject;
22 return ( super .equals(otherHourlyEmployee)
23 && (wageRate == otherHourlyEmployee.wageRate)
24 && (hours == otherHourlyEmployee.hours));
25 }
26 }
All constructor and other method definitions are exactly the same as in Display 7.3.
27 }
If we add this abstract method getPay to the class Employee , then we are free to add
the method samePay to the class Employee .
An abstract method can be thought of as the interface part of a method with the
implementation details omitted. Because a private method is normally only a helping
method and so not part of the interface for a programmer using the class, it follows that
it does not make sense to have a private abstract method. Java enforces this reasoning.
In Java, an abstract method cannot be private. Normally an abstract method is public
but protected, and package (default) access is allowed.
An abstract method serves a purpose, even though it is not given a full definition. It
serves as a placeholder for a method that must be defined in all (nonabstract) derived
classes. Note that in Display 8.7 , the method samePay includes invocations of the
method getPay . If the abstract method getPay were omitted, this invocation of
getPay would be illegal.
abstract
cannot be
private
 
 
Search WWH ::




Custom Search