Search⌘ K
AI Features

Solution: The Employee Payroll System

Explore how to implement an employee payroll system by using inheritance to create Manager and Intern classes from Employee. Learn to override methods for specific salary logic like bonuses and fixed pay, enhancing flexible and maintainable code 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());
}
}
...