9.3 (Recommended: Using Composition Rather Than Inheritance) Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee (Fig. 9.11) of the CommissionEmployee—BasePlusCommissionEmployee hierarchy so  that it contains a reference to a ConmissionEmployee object, rather than inheriting from class CommissionEmployee. Retest BasePlusConmissionEmployee to demonstrate that it still provides the same functionality.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 3PE
icon
Related questions
Question

9.3 (Recommended: Using Composition Rather Than Inheritance) Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee (Fig. 9.11) of the CommissionEmployee—BasePlusCommissionEmployee hierarchy so  that it contains a reference to a ConmissionEmployee object, rather than inheriting from class CommissionEmployee. Retest BasePlusConmissionEmployee to demonstrate that it still provides the same functionality.

I
// Fig. 9.11: BasePlusCommission Employee.java
2 // BasePlus Commission Employee class inherits from Commission Employee
3 // and accesses the superclass's private data via inherited
4
// public methods.
5 public class BasePlusCommission Employee extends Commission Employee {
6 private double baseSalary; // base salary per week
7
Fig. 9.11 | BasePlus Commission Employee class inherits from Commission Employee and
accesses the superclass's private data via inherited public methods. (Part 1 of 2.)
Transcribed Image Text:I // Fig. 9.11: BasePlusCommission Employee.java 2 // BasePlus Commission Employee class inherits from Commission Employee 3 // and accesses the superclass's private data via inherited 4 // public methods. 5 public class BasePlusCommission Employee extends Commission Employee { 6 private double baseSalary; // base salary per week 7 Fig. 9.11 | BasePlus Commission Employee class inherits from Commission Employee and accesses the superclass's private data via inherited public methods. (Part 1 of 2.)
8
9
10
||
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
42
43
44
45 }
// six-argument constructor
public BasePlusCommission Employee (String firstName, String lastName,
String social SecurityNumber, double grossSales,
double commission Rate, double baseSalary) {
super (firstName, lastName, social SecurityNumber,
grossSales, commission Rate);
// if baseSalary is invalid throw exception
if (baseSalary < 0.0) {
throw new IllegalArgumentException ("Base salary must be >= 0.0");
}
this.baseSalary = baseSalary:
}
// set base salary
public void setBase Salary(double baseSalary) {
if (baseSalary < 0.0) {
throw new IllegalArgumentException ("Base salary must be >= 0.0");
}
this.baseSalary = baseSalary;
}
// return base salary
public double getBaseSalary() {return baseSalary;}
// calculate earnings
@Override
public double earnings() {return getBaseSalary() + super. earnings ((); }
}
// return String representation of BasePlusCommission Employee
@Override
public String toString() {
return String.format("%s %s %n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary());
Fig. 9.11 | BasePlusCommission Employee class inherits from Commission Employee and
accesses the superclass's private data via inherited public methods. (Part 2 of 2.)
Transcribed Image Text:8 9 10 || 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 42 43 44 45 } // six-argument constructor public BasePlusCommission Employee (String firstName, String lastName, String social SecurityNumber, double grossSales, double commission Rate, double baseSalary) { super (firstName, lastName, social SecurityNumber, grossSales, commission Rate); // if baseSalary is invalid throw exception if (baseSalary < 0.0) { throw new IllegalArgumentException ("Base salary must be >= 0.0"); } this.baseSalary = baseSalary: } // set base salary public void setBase Salary(double baseSalary) { if (baseSalary < 0.0) { throw new IllegalArgumentException ("Base salary must be >= 0.0"); } this.baseSalary = baseSalary; } // return base salary public double getBaseSalary() {return baseSalary;} // calculate earnings @Override public double earnings() {return getBaseSalary() + super. earnings ((); } } // return String representation of BasePlusCommission Employee @Override public String toString() { return String.format("%s %s %n%s: %.2f", "base-salaried", super.toString(), "base salary", getBaseSalary()); Fig. 9.11 | BasePlusCommission Employee class inherits from Commission Employee and accesses the superclass's private data via inherited public methods. (Part 2 of 2.)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr