In this chapter, you studied an inheritance hierarchy in which classBasePlusCommissionEmployee inherited from class CommissionEmployee. However, not

Question:

In this chapter, you studied an inheritance hierarchy in which classBasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, you’ll create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, socialSecurityNumber, getFirstName, getLastName, getSocialSecurityNumber and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. Class CommissionEmployee’s constructor should invoke class Employee’s constructor, and CommissionEmployee’s toString method should invoke Employee’s to-String method. Once you’ve completed these modifications, run the CommissionEmployeeTest and BasePlusCommissionEmployeeTest apps using these new classes to ensure that the apps still display the same results for a CommissionEmployee object and BasePlusCommissionEmployee object, respectively.

Class CommissionEmployee
Class CommissionEmployee (Fig. 9.10) declares instance variables firstName, lastName,socialSecurityNumber, grossSales and commissionRate as private (lines 5–9) andprovides public methods getFirstName, getLastName, getSocialSecurityNumber, setGrossSales, getGrossSales, setCommissionRate, getCommissionRate, earnings andtoString for manipulating these values. Methods earnings (lines 70–72) and toString(lines 75–82) use the class’s get methods to obtain the values of its instance variables. If wedecide to change the names of the instance variables, the earnings and toString declarations will not require modification—only the bodies of the get and set methods that directlymanipulate the instance variables will need to change. These changes occur solely withinthe superclass—no changes to the subclass are needed. Localizing the effects of changes like this is a good software engineering practice.

Figure 9.10:

I // Fig. 9.10: Commission Employee.java 2 // Commission Employee class uses methods to manipulate its //

45678 6 7 8 9 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

55 56 57 58 5678 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 } // set

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  answer-question

Java How To Program Late Objects Version

ISBN: 9780136123712

8th Edition

Authors: Paul Deitel, Deitel & Associates

Question Posted: