Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

9.16 (Recommended Project: Combining Composition and Inheritance 2 ) In this chapter, we created the CommissionEmployeeBasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types

9.16 (Recommended Project: Combining Composition and Inheritance2) In this chapter, we created the CommissionEmployeeBasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object.

2. Our thanks to Brian Goetz, Oracles Java Language Architect, for suggesting the class architecture we use in this exercise.

A CompensationModel would provide an earnings method. Subclasses of CompensationModel would contain the details of a particular Employees compensation:

CommissionCompensationModelFor Employees CompensationModel subclass would contain grossSales and commissionRate instance variables, and would define earnings to return grossSales * commissionRate.

BasePlusCommissionCompensationModelFor Employees who are paid a base salary and commission, this CompensationModel subclass would contain instance variables grossSales, commissionRate and baseSalary and would define earnings to return baseSalary + grossSales * commissionRate.

Class Employees earnings method would simply call the composed CompensationModels earningsmethod and return its result.

This approach is more flexible than our original hierarchy. For example, consider an Employee who gets promoted. With the approach described here, you can simply change that Employees CompensationModel by assigning the composed CompensationModel reference an appropriate subclass object. With the CommissionEmployeeBasePlusCommissionEmployee hierarchy, youd need to change the employees type by creating a new object of the appropriate class and moving data from the old object into the new one.

Implement the Employee class and CompensationModel hierarchy discussed in this exercise. In addition to the firstName, lastName, socialSecurityNumber and CompensationModel instance variables, class Employee should provide:

A constructor that receives three Strings and a CompensationModel to initialize the instance variables.

A set method that allows the client code to change an Employees CompensationModel.

An earnings method that calls the CompensationModels earnings method and returns its result.

When you invoke method earnings via the superclass CompensationModel reference to a subclass object (of type CommissionCompensationModel or BasePlusCommissionCompensationModel), you might expect superclass CompensationModels earnings method to execute. What actually happens? The subclass objects earnings method executes. This is called polymorphic behavior, which we explore in Chapter 10.

In your test application, create two Employee objectsone with a CommissionCompensation-Model and one with a BasePlusCommissionCompensationModelthen display each Employees earnings. Next, change each Employees CompensationModel dynamically and redisplay each Employees earnings. In Chapter 10 s exercises, well examine how to implement CompensationModel as an interface rather than a class.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions