Class BasePlusCommissionEmployee
(Fig. G.20) extends class CommissionEmployee
(line 4) and therefore is an indirect subclass of class Employee
. Class BasePlusCommissionEmployee
has a constructor (lines 9–14) that takes as arguments a first name, a last name, a social security number, a sales amount, a commission rate and a base salary. It then passes all of these except the base salary to the CommissionEmployee
constructor (line 12) to initialize the inherited members. BasePlusCommissionEmployee
also contains a set method (lines 17–24) to assign a new value to instance variable baseSalary
and a get method (lines 27–30) to return baseSalary
’s value. Method earnings
(lines 33–37) calculates a BasePlusCommissionEmployee
’s earnings. Line 36 in method earnings
calls superclass CommissionEmployee
’s earnings
method to calculate the commission-based portion of the employee’s earnings—this is another nice example of code reuse. BasePlusCommissionEmployee
’s toString
method (lines 40–46) creates a String
representation of a BasePlusCommissionEmployee
that contains "base-salaried"
, followed by the String
obtained by invoking superclass CommissionEmployee
’s toString
method (another example of code reuse), then the base salary. The result is a String
beginning with "basesalaried commission employee"
followed by the rest of the BasePlusCommissionEmployee
’s information. Recall that CommissionEmployee
’s toString
obtains the employee’s first name, last name and social security number by invoking the toString
method of its superclass (i.e., Employee
)—yet another example of code reuse. BasePlusCommissionEmployee
’s toString
initiates a chain of method calls that span all three levels of the Employee
hierarchy.
1 // Fig. G.20: BasePlusCommissionEmployee.java
2 // BasePlusCommissionEmployee class extends CommissionEmployee.
3
4 public class BasePlusCommissionEmployee extends CommissionEmployee
5 {
6 private double baseSalary; // base salary per week
7
8 // six-argument constructor
9 public BasePlusCommissionEmployee( String first, String last,
10 String ssn, double sales, double rate, double salary )
11 {
12 super ( first, last, ssn, sales, rate );
13 setBaseSalary( salary ); // validate and store base salary
14 } // end six-argument BasePlusCommissionEmployee constructor
15
16 // set base salary
17 public void setBaseSalary( double salary )
18 {
19 if ( salary >= 0.0 )
20 baseSalary = salary;
21 else
22 throw new IllegalArgumentException(
23 "Base salary must be >= 0.0" );
24 } // end method setBaseSalary
25
26 // return base salary
27 public double getBaseSalary()
28 {
29 return baseSalary;
30 } // end method getBaseSalary
31
32 // calculate earnings; override method earnings in CommissionEmployee
33 @Override
34 public double earnings()
35 {
36 return getBaseSalary() + super.earnings();
37 } // end method earnings
38
39 // return String representation of BasePlusCommissionEmployee object
40 @Override
41 public String toString()
42 {
43 return String.format( "%s %s; %s: $%,.2f",
44 "base-salaried", super.toString(),
45 "base salary", getBaseSalary() );
46 } // end method toString
47 } // end class BasePlusCommissionEmployee