0% found this document useful (0 votes)
23 views

Abstraction Handout

The document defines an abstract Employee class with a calculateSalary() method. It then creates Contractor and FullTimeEmployee subclasses that override calculateSalary() differently based on their salary structures. Contractor salary is calculated by multiplying payment per hour by working hours. FullTimeEmployee salary multiplies payment per hour by a constant of 8 hours. The AbstractionDemo class tests the subclasses by creating instances and printing their calculated salaries.

Uploaded by

Sidra Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
23 views

Abstraction Handout

The document defines an abstract Employee class with a calculateSalary() method. It then creates Contractor and FullTimeEmployee subclasses that override calculateSalary() differently based on their salary structures. Contractor salary is calculated by multiplying payment per hour by working hours. FullTimeEmployee salary multiplies payment per hour by a constant of 8 hours. The AbstractionDemo class tests the subclasses by creating instances and printing their calculated salaries.

Uploaded by

Sidra Nasir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 3

Employee, Contractor, and FullTimeEmployee

In this example, we create an abstract Employee class and which contains the


abstract calculateSalary() method. Let the subclasses extend the Employee class and
implement a calculateSalary() method.

Let's create Contractor and FullTimeEmployee classes as we know that the salary


structure for a contractor and full-time employees are different so let these classes
override and implement a calculateSalary() method.

Let's write source code by looking into the above class diagram.
Step 1: Let's first create the superclass Employee. Note the usage of abstract keyword
in this class definition. This marks the class to be abstract, which means it cannot be
instantiated directly. We define a method called calculateSalary() as an abstract method.
This way you leave the implementation of this method to the inheritors of
the Employee class.
public abstract class Employee {

private String name;


private int paymentPerHour;

public Employee(String name, int paymentPerHour) {


this.name = name;
this.paymentPerHour = paymentPerHour;
}

public abstract int calculateSalary();


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPaymentPerHour() {
return paymentPerHour;
}
public void setPaymentPerHour(int paymentPerHour) {
this.paymentPerHour = paymentPerHour;
}
}

Step 2: The Contractor class inherits all properties from its parent


abstract Employee class but has to provide its own implementation
to calculateSalary() method. In this case, we multiply the value of payment per hour with
given working hours.

public class Contractor extends Employee {

private int workingHours;


public Contractor(String name, int paymentPerHour, int workingHours) {
super(name, paymentPerHour);
this.workingHours = workingHours;
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * workingHours;
}
}
Step 3: The FullTimeEmployee also has its own implementation
of calculateSalary() method. In this case, we just multiply by a constant value of 8 hours.

public class FullTimeEmployee extends Employee {


public FullTimeEmployee(String name, int paymentPerHour) {
super(name, paymentPerHour);
}
@Override
public int calculateSalary() {
return getPaymentPerHour() * 8;
}
}

Step 4: Let's create an AbstractionDemo class to test implementation


of Abstraction with the below code:

public class AbstractionDemo {

public static void main(String[] args) {

Employee contractor = new Contractor("contractor", 10, 10);


Employee fullTimeEmployee = new FullTimeEmployee("full time employee", 8);
System.out.println(contractor.calculateSalary());
System.out.println(fullTimeEmployee.calculateSalary());
}
}

You might also like