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

                Where is the getPay method? It’s a polymorphic method that apperars in all classes that di- rectly and indirectly extend the Employee class—Commissioned, Salaried, Hourly, and SalariedAndCommissioned. But hey! This set of classes together with the Employee class itself is the Employee subtree. So instead of repeating the definition of the FICA calculation in all classes that have a getPay method, it’s more logical and more efficient to put this common calculation in the subtree’s root class Employee and make it protected.
To avoid trampling on previous versions of the program, we use new class names in our new FICA enhanced Payroll program. See Figure 13.19. It shows the program’s UML diagram with the new class names—Payroll5, Employee3, Commissioned2, Salaried3, Hourly3, and SalariedAndCommissioned2.
13.10 The protected Access Modifier 541
  Employee3
Commissioned2 Salaried3 Hourly3
SalariedAndCommissioned2
Apago PDF Enhancer
Payroll5
              Figure 13.19 Abbreviated class diagram for an enhanced Payroll program
Figure 13.20 shows the definition of Employee3, which includes this additional common helper method, getFICA. Employee3 also includes some named constants used in the FICA calculation. The details of this calculation are not relevant to the present discussion, so to save space, we implement it in a fairly cryptic form using the conditional operator. What this little getFICA method does is a reasonable representation of what actually happens to people’s paychecks. So if you’re curious, you might want to ex- pand the cryptic code into a more readable form. (An end-of-chapter exercise asks you to do this.)
Each of the polymorphic getPay methods includes a call to this new getFICA method. The code for this call is essentially the same in each of the getPay methods, so we’ll show it just once, in the Salaried3 class in Figure 13.21.
For the most part, the SalariedAndCommissioned2 class that extends the Salaried3 class is like what’s shown in Figure 13.16, with appropriate changes in the version numbers at the ends of the class names. However, in the getPay method we cannot use super.getPay() to access salary in the Salaried3 class, because the FICA tax makes the value returned by Salaried3’s getPay method different from the value of salary.
With the FICA tax, there must be another way to access salary. Although Salaried3 could in- clude a getSalary accessor method, the code would be simpler if salary were public. But would you want everybody’s salary to be public? The most appropriate thing to do here is to elevate the ac- cessibility of the salary variable in the Salaried3 class from private to protected. This gives descendant classes direct access to the salary variable, but it does not expose it as much as a public modifierwould.BecausetheSalariedAndCommissioned2classextends Salaried3,ifthereis






















































































   573   574   575   576   577