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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education