Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A) Use Inheritance to reduce code duplication, as follows: 1- Create a class cal

ID: 3868113 • Letter: A

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 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).

Explanation / Answer

Employee.java

public abstract class Employee {

//Declaring instance variables

private int id;

private String name;

//Parameterized constructor

public Employee(int id, String name) {

super();

this.id = id;

this.name = name;

}

//Abstract Method

public abstract double getSalary();

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

return " id=" + id + ", name=" + name;

}

}

_____________________

PartTimeEmployee.java

public class PartTimeEmployee extends Employee {


//Declaring instance variables
private int hours;
private int payPerHour;

//Parameterized constructor
public PartTimeEmployee(int id, String name, int hours, int payPerHour) {
super(id, name);
this.hours = hours;
this.payPerHour = payPerHour;
}

//Setters and getters
public int getHours() {
return hours;
}

public void setHours(int hours) {
this.hours = hours;
}

public int getPayPerHour() {
return payPerHour;
}

public void setPayPerHour(int payPerHour) {
this.payPerHour = payPerHour;
}

//We are implementing the super class Abstract method
@Override
public double getSalary() {

return payPerHour * hours;
}


//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "PartTimeEmployee [" + super.toString() + " salary=" + getSalary() + "]";
}


}

_____________________

FullTimeEmployee.java

public class FullTimeEmployee extends Employee {

//Declaring instance variables
private double salary;

//Parameterized constructor
public FullTimeEmployee(int id, String name, double salary) {
super(id, name);
this.salary = salary;
}


//We are implementing the super class Abstract method
@Override
public double getSalary() {

return salary;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "FullTimeEmployee [" + super.toString() + " salary=" + salary + "]";
}


}

________________________

EmployeeDemo2.java

public class EmployeeDemo2 {

public static void main(String[] args) {

//Declaring Employee Class Variables
Employee empPT, empFT;

//Creating an Instance of PartTimeEmployee Class
empPT = new PartTimeEmployee(111, "Rahul", 40, 12);

//Creating an Instance of FullTimeEmployee Class
empFT = new FullTimeEmployee(222, "John", 50000);

//Calling the toString() method
System.out.println(empPT.toString());
System.out.println(empFT.toString());


// System.out.println(((PartTimeEmployee) empPT).getHours());

}

}

_____________________

Output:

PartTimeEmployee [ id=111, name=Rahul salary=480.0]
FullTimeEmployee [ id=222, name=John salary=50000.0]

_____________________

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.

Here we declared getSalary() method as abstract because the implementation of getSalary() method is different in Both PartTimeEmployee class and FullTimeEmployee Class.

And also every employee must have salary that's y we declared getSalary() method in Employee class

______________________

3-      Print both empPT and empFT variables and specify whether the toString() method of theEmployee 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?

Here the toString() method of PartTimeEmployee and FullTimeEmployee will be called.These two classes are extending from the Super class Employee through the concept of Inheritance.

___________________

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).

Ans)

Here we are trying to call the getHours() method with Super class (Employee) reference .So it will search for getHours() method in Employee class.But we dont have getHours() method in Employee class.That's y why we will get error.

In order to solve this problem we have to type cast

System.out.println(((PartTimeEmployee)empPT).getHours());

Now it will search for getHours() method in the PartTimeEmployee Class.Then we wont get any error

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote