Back to portfolio

Najae Hall CSC223 Java

Employee Payroll Model

An employee model that applies inheritance and polymorphism to salary calculations.

Download source ZIP

Technical notes

  • The abstract Employee class stores shared employee information and declares calculateSalary.
  • FullTimeEmployee calculates base salary plus bonus. PartTimeEmployee calculates hours worked multiplied by the hourly rate.
  • EmployeeTestDrive loops through an Employee array. Each object uses its own salary calculation through runtime polymorphism.

Compile and run

With JDK 17 or newer installed, open a terminal in the extracted project folder.

javac -d out src/Module1/*.java
java -cp out Module1.EmployeeTestDrive

The demo calculates salaries for four sample employees using a base salary plus a bonus, or hours worked multiplied by the hourly rate.

Source code

src/Module1/Employee.java
/**
 *  Create a program that models different types of employees using inheritance and polymorhispm.
 * 
 * 1.Parent Class: Employee - abstract class (cannot be instantiated)
 * 	Fields: String name 
 * 			int id
 * 			double baseSalary
 *	Constructor to initialize all fields
 *	Methods:
 *		doubble calculateSalary() - returns baseSalary
 * 	Constructor to initialize all fields
 * Method: 
 * 	double calculateSalary() - returns baseSalary
 * 	void displayInfo() - prints employee details
 * 
 * 2.	Subclass: FullTimeEmployee
 * 
 * Additional Field:
 * 		double bonus 
 * Override:
 * 		calculateSalary() - baseSalary + bonus
 * 
 * 3. Subclass: PartTimeEmmployee
 * Additional field:
 * 	int hoursWorked
 * 	double hourlyRate
 * 
 * Override:
 * 		calculateSalary() - hoursWorked * hourlyRate
 */

package Module1;


public abstract class Employee {
		
		//Attributes
		 String name;
		 int id;
		 double baseSalary;
		 
		 //Constructor
		 public Employee (int id,String name, double baseSalary) {
			 this.id = id;
			 this.name = name;
			 this.baseSalary = baseSalary;
			 
		 }
		 //Abstract method (polymorphism)
		public abstract double calculateSalary();
		 
		 //Common method
		 public void displayInfo() {
			 System.out.println(
					 "ID:" +id +
					 ",Name:" + name +
					 ", Salary: " + calculateSalary());
		 
		 
		
	}

}
src/Module1/FullTimeEmployee.java
/**
 * 
 */
package Module1;

/**
 * 
 */
public class FullTimeEmployee extends Employee {

	double bonus;
	
	//constructor
	public FullTimeEmployee(int id,String name, double baseSalary,double bonus) {
		super(id, name, baseSalary);
		this.bonus= bonus;
		
	}
	
	@Override
	public double calculateSalary() {
		return baseSalary + bonus;
	
	}

}

src/Module1/PartTimeEmployee.java
/**
 * 
 */
package Module1;


public class PartTimeEmployee extends Employee {

	
	int hoursWorked;
	double hourlyRate;
	
	//constructor
	public PartTimeEmployee(int id,String name, int hoursWorked, double hourlyRate) {
		super(id, name, 0); //baseSalary not used for part- time
		this.hoursWorked = hoursWorked;
		this.hourlyRate = hourlyRate;
		
	}
	
	@Override 
	public double calculateSalary () {
		 return hoursWorked * hourlyRate ;
	
	}

}
src/Module1/EmployeeTestDrive.java
/**
 * 
 */
package Module1;

/**
 * 
 */
public class EmployeeTestDrive {

	
	public static void main(String[] args) {
		//Polymorphism: parent refernce, child objects
		Employee[] employees = new Employee [4];
		
		
		employees[0] = new FullTimeEmployee(101,"Alice",50000,500);
		employees[1] = new PartTimeEmployee(102,"Bob",80,150);
		employees[2] = new FullTimeEmployee(103,"John" , 600 , 150);
		employees[3] = new PartTimeEmployee(104, "Annie", 40, 60);
		
		//Display employee details
		for(Employee emp : employees) {
			emp.displayInfo();
			}
		

	}

	}