Search⌘ K
AI Features

Solution: The Employee Payroll System

Explore how to design an employee payroll system by creating class hierarchies that use inheritance and polymorphism. Understand how to override methods to implement custom salary logic for different employee types like managers and interns, ensuring flexible and scalable application design.

We'll cover the following...
Java 25
class Employee {
protected String name;
protected double baseSalary;
public Employee(String name, double baseSalary) {
this.name = name;
this.baseSalary = baseSalary;
}
public double calculateSalary() {
return baseSalary;
}
}
class Manager extends Employee {
public Manager(String name, double baseSalary) {
super(name, baseSalary);
}
@Override
public double calculateSalary() {
return baseSalary * 1.2;
}
}
class Intern extends Employee {
public Intern(String name, double baseSalary) {
// We must satisfy the parent contract
super(name, baseSalary);
}
@Override
public double calculateSalary() {
// We completely replace the logic to return a fixed stipend
return 500.0;
}
}
public class Main {
public static void main(String[] args) {
Employee mgr = new Manager("Alice", 70000);
Employee intern = new Intern("Bob", 20000);
System.out.println("Manager Pay: " + mgr.calculateSalary());
System.out.println("Intern Pay: " + intern.calculateSalary());
}
}
...