w3resource

Java Polymorphism - Employee Class with Manager and Programmer Subclasses for Salary Calculation

Java Polymorphism: Exercise-4 with Solution

Write a Java program to create a class Employee with a method called calculateSalary(). Create two subclasses Manager and Programmer. In each subclass, override the calculateSalary() method to calculate and return the salary based on their specific roles.

In the given exercise, here is a simple diagram illustrating polymorphism implementation:

Polymorphism: Employee Class with Manager and Programmer Subclasses for Salary Calculation

In the above diagram, the Employee class is the base class, and it has two subclasses, Manager and Programmer. The Employee class has private instance variables name and role, along with getter methods for retrieving their values. It also has a method calculateSalary() which returns a double.

The diagram demonstrates the inheritance relationship between the classes and how the calculateSalary() method is polymorphically overridden in the subclasses.

Sample Solution:

Java Code:

// Employee.java
// Base class Employee
class Employee {
    private String name;
    private String role;

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

    public String getName() {
        return name;
    }

    public String getRole() {
        return role;
    }

    public double calculateSalary() {
        return 0.0;
    }
}

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

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

    @Override
    public double calculateSalary() {
        return baseSalary + bonus;
    }
}
// Programmer.java
// Subclass Programmer
class Programmer extends Employee {
    private double baseSalary;
    private double overtimePay;

    public Programmer(String name, double baseSalary, double overtimePay) {
        super(name, "Programmer");
        this.baseSalary = baseSalary;
        this.overtimePay = overtimePay;
    }

    @Override
    public double calculateSalary() {
        return baseSalary + overtimePay;
    }
}
// Main.java
// Main class
public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Manager("Lilo Heidi", 7500.0, 1500.0);
        Employee emp2 = new Programmer("Margrit Cathrin", 5000.0, 600.0);

        System.out.println("Manager: " + emp1.getName() + "\nRole: " + emp1.getRole() + "\nSalary: $" + emp1.calculateSalary());
        System.out.println("\nProgrammer: " + emp2.getName() + "\nRole: " + emp2.getRole() + "\nSalary: $" + emp2.calculateSalary());
    }
}

Sample Output:

Manager: Lilo Heidi
Role: Manager
Salary: $9000.0

Programmer: Margrit Cathrin
Role: Programmer
Salary: $5600.0

Flowchart:

Flowchart: Base class Employee
Flowchart: Subclass Manager
Flowchart: Subclass Programmer
Flowchart: Main class

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Employee Class with Manager and Programmer Subclasses for Salary Calculation.
Next: Sports Class with Football, Basketball, and Rugby Subclasses for Playing Statements.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

Java: Tips of the Day

How do I remove repeated elements from ArrayList?

If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:

Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);

Of course, this destroys the ordering of the elements in the ArrayList.

Ref: https://bit.ly/3bYIjNC

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook