Create an abstract class “Employee” with abstract methods “calculateSalary()” and “displayEmployeeDetails()”. Implement two subclasses “Manager” and “Worker” which extend “Employee” and implement the abstract methods. Create a “SalesPerson” class which extends “Manager” and overrides the necessary methods. Create objects of all classes and test their behavior.
Solution :
abstract class Employee {
protected String name;
protected int age;
protected String gender;
public Employee(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public abstract double calculateSalary();
public abstract void displayEmployeeDetails();
}
class Manager extends Employee {
protected double baseSalary;
protected double bonus;
public Manager(String name, int age, String gender, double baseSalary, double bonus) {
super(name, age, gender);
this.baseSalary = baseSalary;
this.bonus = bonus;
}
public double calculateSalary() {
return baseSalary + bonus;
}
public void displayEmployeeDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
System.out.println("Base Salary: " + baseSalary);
System.out.println("Bonus: " + bonus);
System.out.println("Total Salary: " + calculateSalary());
}
}
class Worker extends Employee {
protected double hourlyRate;
protected int hoursWorked;
public Worker(String name, int age, String gender, double hourlyRate, int hoursWorked) {
super(name, age, gender);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
public double calculateSalary() {
return hourlyRate * hoursWorked;
}
public void displayEmployeeDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
System.out.println("Hourly Rate: " + hourlyRate);
System.out.println("Hours Worked: " + hoursWorked);
System.out.println("Total Salary: " + calculateSalary());
}
}
class SalesPerson extends Manager {
protected double commissionRate;
public SalesPerson(String name, int age, String gender, double baseSalary, double bonus, double commissionRate) {
super(name, age, gender, baseSalary, bonus);
this.commissionRate = commissionRate;
}
public double calculateSalary() {
return super.calculateSalary() + (super.calculateSalary() * commissionRate);
}
public void displayEmployeeDetails() {
super.displayEmployeeDetails();
System.out.println("Commission Rate: " + commissionRate);
System.out.println("Total Salary (including commission): " + calculateSalary());
}
}
class AbstractExample {
public static void main(String[] args) {
Employee manager = new Manager("John", 40, "Male", 5000, 1000);
Employee worker = new Worker("Mary", 25, "Female", 20, 160);
Employee salesPerson = new SalesPerson("Bob", 45, "Male", 6000, 1500, 0.05);
manager.displayEmployeeDetails();
System.out.println();
worker.displayEmployeeDetails();
System.out.println();
salesPerson.displayEmployeeDetails();
}
}
Save the above Program with “AbstractExample .Java” then compile it with the following command :
javac AbstractExample.java //to compile the code java AbstractExample //to run the code
Output is :
Name: John Age: 40 Gender: Male Base Salary: 5000.0 Bonus: 1000.0 Total Salary: 6000.0 Name: Mary Age: 25 Gender: Female Hourly Rate: 20.0 Hours Worked: 160 Total Salary: 3200.0 Name: Bob Age: 45 Gender: Male Base Salary: 6000.0 Bonus: 1500.0 Total Salary: 7500.0 Commission Rate: 0.05 Total Salary (including commission): 7875.0
Want to practice more problems involving Abstract Classes? Click here.