You are on page 1of 4

import java.util.

ArrayList;

abstract class Employee {

private String name;

private String entitlements;

public Employee(String name, String entitlements) {

this.name = name;

this.entitlements = entitlements;

public String getName() {

return name;

public String getEntitlements() {

return entitlements;

public abstract double calculateSalary();

class OfficeLaptopEmployee extends Employee {


private double salary;

public OfficeLaptopEmployee(String name, String entitlements, double salary) {

super(name, entitlements);

this.salary = salary;

@Override

public double calculateSalary() {

return salary;

class PersonalCarEmployee extends Employee {

private double salary;

public PersonalCarEmployee(String name, String entitlements, double salary) {

super(name, entitlements);

this.salary = salary;

@Override

public double calculateSalary() {


return salary;

public class Main {

public static void main(String[] args) {

ArrayList<Employee> employees = new ArrayList<>();

employees.add(new OfficeLaptopEmployee("John", "Office Laptop", 5000));

employees.add(new PersonalCarEmployee("Jane", "Personal Car", 6000));

for (Employee employee : employees) {

System.out.println("Name: " + employee.getName());

System.out.println("Entitlements: " + employee.getEntitlements());

System.out.println("Salary: " + employee.calculateSalary());

System.out.println();

Explanation

This Java code defines a simple program that models employees and calculates their salaries.
Here's a breakdown of the code:
1. Employee is an abstract class representing an employee. It has two private fields, name
and entitlements, and an abstract method calculateSalary() that should be implemented
by its subclasses.

2. OfficeLaptopEmployee and PersonalCarEmployee are two concrete subclasses of


Employee. They both have an additional private field, salary, which represents the salary
of the respective employees. They provide an implementation for the calculateSalary()
method, returning the salary specified when creating an instance of the class.

3. In the Main class's main method, an ArrayList named employees is created to store
instances of Employee.

4. Two Employee objects are created using the OfficeLaptopEmployee and


PersonalCarEmployee classes, with specific names, entitlements, and salaries.

5. These employee objects are added to the employees ArrayList.

6. A for-each loop is used to iterate over the employees ArrayList.

7. Within the loop, it prints out the following information for each employee:

 Name of the employee obtained using getName().

 Entitlements of the employee obtained using getEntitlements().

 Salary of the employee obtained using calculateSalary().

8. After printing the information for one employee, a newline character is added to separate
the information for the next employee.

In summary, this code defines a basic employee management system with an abstract base class
(Employee) and two concrete subclasses (OfficeLaptopEmployee and
PersonalCarEmployee). It demonstrates polymorphism by storing these employees in a list and
calculating and displaying their salaries.

You might also like