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

                Figure 13.21 Enhanced version of Salaried class that includes tax deduction
a protected modifier on the salary variable in Salaried3, you can define the getPay method in
the SalariedAndCommissioned2 class like this: public double getPay()
13.10 The protected Access Modifier 543
 /*********************************************
*
Salaried3.java
Dean & Dean
This class represents salaried employees.
*********************************************/
*
*
*
  public class Salaried3 extends Employee3
 {
protected double salary;
//******************************************
public Salaried3(String name, double salary)
{
super(name);
this.salary = salary;
} // end constructor
//******************************************
public double getPay()
{
pay =Apsaalagryo; pay -= getFICA(pay);
return pay;
PDF Enhancer
double
This allows direct access from descendant classes.
  This calls protected method at top of subtree.
 }
// end class Salaried3
} // end getPay
   {
}
protected in Salaried3
    double pay = salary + COMMISSION_RATE
pay -= getFICA(pay);
 sales = 0.0;
return pay;
// end getPay
// reset for next pay period
So there you have it. Polymorphism enables you to put heterogeneous objects into generic arrays whose type is either a class the objects’ classes descend from or an interface the objects’ classes implement. Then you can cast array elements into subclass or interface types, so the array elements can make method calls that are specific to their subclass or interface. The JVM finds the method that best matches the calling
* sales;
protected in Employee3
 




















































   575   576   577   578   579