Question: A) Use Inheritance to reduce code duplication, as follows: 1- Create a class called Employee.java . This class will contain the code that is in
A) Use Inheritance to reduce code duplication, as follows:
1- Create a class called Employee.java. This class will contain the code that is in common between PartTimeEmployee.java and FullTimeEmployee.java classes
2- Add a constructor method to the Employee class, which receives parameters for all instance variables of the Employee class.
3- Add a toString() method to the Empolyee.java class, which prints all the instance variables of the class.
4- Modify PartTimeEmployee.java and FullTimeEmployee.java classes such that they inherit and use the common code from the Employee.java rather than duplicating them.
5- Modify the toString() methods of the new PartTimeEmployee.java and FullTimeEmployee.java classes to use inheritance.
6- getSalary() method is a good candidate to be implemented as an abstract method.
a. Explain why this method should be implemented as an abstract method.
b. Implement this method as an abstract method.
7- Run the EmployeeDemo.java to verify your new code runs correctly. Specifically, make sure that the toString() methods of the new PartTimeEmployee.java and FullTimeEmployee.java classes are working correctly.
(Note: You should not change the EmployeeDemo.java class to answer part A above)
B) Create a new class called EmployeeDemo2.java and in its main method implement the following:
1- Create two Employee variables called empPT and empFT.
2- Assign empPT to an instance of the new PartTimeEmployee class that you created in part A and assign empFT to an instance of the new FullTimeEmployee class that you also created in part A.
3- Print both empPT and empFT variables and specify whether the toString() method of the Employee class is called or the toString() methods of the PartTimeEmployee and FullTimeEmployee classes are called.
a. Explain why the above has happened.
b. In the Object Oriented paradigm, what this phenomena is called?
4- Call empPT.getHours() in EmployeeDemo2.java.
a. What is the result? Explain why it is happening.
b. If there is an issue, just explain how it can be fixed (writing code to fix the issue is not necessary).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
