w3resource

Java: Abstract Employee class with Manager and Programmer subclasses

Java Abstract Class: Exercise-5 with Solution

Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role.

Sample Solution:

Java Code:

//Employee.java
abstract class Employee {
  protected String name;
  protected double baseSalary;

  public Employee(String name, double baseSalary) {
    this.name = name;
    this.baseSalary = baseSalary;
  }

  public abstract double calculateSalary();

  public abstract void displayInfo();
}

//Manager.java
class Manager extends Employee {
  private double bonus;

  public Manager(String name, double baseSalary, double bonus) {
    super(name, baseSalary);
    this.bonus = bonus;
  }

  @Override
  public double calculateSalary() {
    return baseSalary + bonus;
  }

  @Override
  public void displayInfo() {
    System.out.println("Manager Name: " + name);
    System.out.println("Base Salary: $" + baseSalary);
    System.out.println("Bonus: $" + bonus);
    System.out.println("Total Salary: $" + calculateSalary());
  }
}
//Programmer.java
class Programmer extends Employee {
  private int overtimeHours;
  private double hourlyRate;

  public Programmer(String name, double baseSalary, int overtimeHours, double hourlyRate) {
    super(name, baseSalary);
    this.overtimeHours = overtimeHours;
    this.hourlyRate = hourlyRate;
  }

  @Override
  public double calculateSalary() {
    return baseSalary + (overtimeHours * hourlyRate);
  }

  @Override
  public void displayInfo() {
    System.out.println("Programmer Name: " + name);
    System.out.println("Base Salary: $" + baseSalary);
    System.out.println("Overtime Hours: " + overtimeHours);
    System.out.println("Hourly Rate: $" + hourlyRate);
    System.out.println("Total Salary: $" + calculateSalary());
  }
}
//Main.java
public class Main {
  public static void main(String[] args) {
    Employee manager = new Manager("Corona Cadogan", 6000, 1000);
    Employee programmer = new Programmer("Antal Nuka", 5000, 20, 25.0);

    manager.displayInfo();
    System.out.println("---------------------");
    programmer.displayInfo();
  }
}

Sample Output:

Manager Name: Corona Cadogan
Base Salary: $6000.0
Bonus: $1000.0
Total Salary: $7000.0
---------------------
Programmer Name: Antal Nuka
Base Salary: $5000.0
Overtime Hours: 20
Hourly Rate: $25.0
Total Salary: $5500.0

Explanation:

In the above exercise -

  • The abstract class "Employee" has two abstract methods: calculateSalary() and displayInfo(). The subclasses Manager and Programmer extend the Employee class and provide their own implementations of abstract methods.
  • The "Manager" class calculates the total salary by adding the bonus to the base salary, while the "Programmer" class calculates the total salary based on the number of overtime hours and the hourly rate.
  • In the main method, we create instances of Manager and Programmer, and then call the displayInfo() method on each object to display the information specific to each role.

Flowchart:

Flowchart: Employee Java
Flowchart: Manager Java
Flowchart: Programmer Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Abstract Animal Class with Lion, Tiger, and Deer Subclasses.
Next: Abstract Shape3D Class with Sphere and Cube Subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.