COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

OOP - Polymorphism Pet Program Payroll Program Class Roster Program



Payroll Program with Polymorphism


Description

This program incorporates the OOP inheritance concept to design classes to represent different types of employees.

The client program incorporates the OOP Polymorphism concept to process a variety of subclass objects.



The superclass Employee defines the common features that apply to any types of employees.

Data members: employee id, first and last names

constructors

getters and setters

earnings method
(It is up to the subclasses how to implement it.)

toString method

The subclasses SalariedEmployee, HourlyEmployee, and CommissionEmployee inherit the superclass Employee's features and add additional features.

The subclass BasePlusCommissionEmployee inherits the superclass CommissionEmployee's features and adds additional features.

SalariedEmployee: fixed salary

HourlyEmployee: hours worked, and base wage

CommissionEmployee: sales and commission rate

BasePlusCommissionEmployee: base salary

All subclasses override the superclass' earnings method.

Basically, the Employee's earnings method simply returns 0. It is up to the subclasses how to implement it.

Associated Concepts:

OOP - Inheritance

Algorithm:

Class diagrams:



Client program:

The client program creates an ArrayList of superclass Employee and adds subclass objects to the ArrayList in random order.

It then invokes the earnings() method in the loop using the superclass references.

As we know, the actual subclass object's earnings() method is executed.

Source Code

Superclass Employee class



Subclass CommissionEmployee class



subclass BasePlusCommissionEmployee class



Client program

***Note: SalariedEmployee and HourlyEmployee source code were not implemented.
So the default constructors were used to create the objects of these two types.



Sample Run

***Note: For the objects of SalariedEmployee and HourlyEmployee, the default values were printed.



Video