Page 571 - Introduction to Programming with Java: A Problem Solving Approach
P. 571

                13.9 Interfaces 537
 /************************************************************
*
SalariedAndCommissioned.java
Dean & Dean
This class represents salaried and commissioned employees.
************************************************************/
*
*
*
public class SalariedAndCommissioned
extends Salaried2 implements Commission
{
private double sales;
//*********************************************************
{
public SalariedAndCommissioned(String name, double salary)
super(name, salary);
} // end constructor
//*********************************************************
public void addSales(double sales) ⎫ {⎪
⎬ this.sales += sales; ⎪
   } // end addSales ⎭ //*********************************************************
Apago PDF Enhancer
  }
// end class SalariedAndCommissioned
public double getPay() ⎫ {⎪
double pay = ⎪
 super.getPay() + COMMISSION_RATE * sales;
⎬ ⎪ ⎪ ⎭
sales = 0.0;
return pay;
// reset for next pay period
} // end getPay
The interface supplies this constant value.
The interface requires this method definition.
 This method overrides
the method defined in the parent class.
Figure 13.16 Class defining salary-and-commission employees in enhanced Payroll program
Figure 13.16 shows the code for a SalariedAndCommissioned class. This class extends the Salaried2 class. The Salaried2 class is like the Salaried class in Figure 13.11, except for one difference: whereas the Salaried class extends Employees, the Salaried2 class extends Employee2. The SalariedAndCommissioned class describes a class of employees that earn a salary and a commission. The Salaried2’s class defines a getPay method, so the compiler does not insist that the SalariedAndCommissioned class also define a getPay method, but logically we need to over- ride Salaried2 getPay method. Notice how the overriding method uses the super prefix to call the method it overrides. This additional getPay method definition increases the total number of polymorphic getPay methods to four.
The SalariedAndCommissioned class also implements the Commission interface. This pro- vides direct access to the COMMISSION_RATE named constant, which the getPay method uses to do its job. Because it implements the Commission interface, the SalariedAndCommissioned class must define all methods declared in that interface, and yes, it does define the addSales method.


























































   569   570   571   572   573