Search⌘ K
AI Features

Solution: The Employee Payroll System

Explore how to apply inheritance and polymorphism in Java by creating a payroll system with Manager and Intern classes. Learn to extend base classes, call parent constructors, and override methods for specific payroll calculations.

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());
}
}
...