Program in JAVA (Employee Hierarchy) In this chapter, you studied an inheritance
ID: 3916968 • Letter: P
Question
Program in JAVA
(Employee Hierarchy)
In this chapter, you studied an inheritance hierarchy in which class BasePlusCommissionEmployee 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 toString 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.
Explanation / Answer
I can only help you with writing the Employee class and a little skeleton code of the CommsisionEmployee class because its specifications are not given.
Employee.java
package myPackage;
public class Employee {
private String firstName;
private String lastName;
private long socialSecurityNumber;
public Employee(String firstName, String lastName, long socialSecurityNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getSocialSecurityNumber() {
return socialSecurityNumber;
}
public void setSocialSecurityNumber(long socialSecurityNumber) {
this.socialSecurityNumber = socialSecurityNumber;
}
@Override
public String toString() {
return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", socialSecurityNumber="
+ socialSecurityNumber + "]";
}
}
CommissionEmployee.java
package myPackage;
public class CommissionEmployee extends Employee {
public CommissionEmployee(String firstName, String lastName, long socialSecurityNumber) {
super(firstName, lastName, socialSecurityNumber);
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return super.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.