Polymorphism
Java Project
Alvi KALIA Kevin KALOSHI
1. Introduction
Polymorphism in Java
Polymorphism in Java allows objects of different types to be treated as objects of a common
type. It provides a way for classes to offer a common interface while still allowing their specific
behaviors to be implemented. In simple terms, it allows different classes to be used
interchangeably through a shared interface.
2. About the Project
Purpose
The Employee Payroll System project showcases the principles of Object-Oriented
Programming (OOP) in Java. It aims to demonstrate the use of inheritance, encapsulation, and
polymorphism to model and manage different types of employees in an organization.
Key Features
Inheritance: Creating a hierarchy of employee classes based on a common base class.
Polymorphism: Implementing different behaviors for each employee type while using a
common interface.
Encapsulation: Protecting data by using private variables and providing controlled
access through getter methods.
3. The Code
4. Classes and Methods
Employee Class
Represents the base class for all employee types.
Contains basic information such as first name, last name, and social security number.
Method: earnings() returns a default value.
SalariedEmployee Class
Inherits from Employee.
Represents employees with a fixed weekly salary.
Method: earnings() returns the weekly salary.
HourlyEmployee Class
Inherits from Employee.
Represents employees paid on an hourly basis.
Method: earnings() calculates earnings based on hours worked and hourly wage.
CommissionEmployee Class
Inherits from Employee.
Represents employees earning a commission on gross sales.
Method: earnings() calculates earnings based on gross sales and commission rate.
BasePlusCommissionEmployee Class
Inherits from CommissionEmployee.
Represents employees with a base salary in addition to commission earnings.
Method: earnings() calculates earnings including the base salary.
PayrollSystem Class
Contains the main method for executing the payroll system.
Creates instances of various employee types and displays their information and
earnings.
5. Execution and Output
Upon execution, the program creates a list of different employee types and displays their
information, showcasing the polymorphic behavior. The output demonstrates that despite
using a common Employee interface, each employee type calculates earnings differently,
reflecting the diversity of roles within the organization.
6. Conclusion
The Employee Payroll System successfully illustrates the effectiveness of OOP principles in
Java. Through inheritance, encapsulation, and polymorphism, the project provides a clear and
extensible model for managing various employee types. The use of a common interface allows
for flexibility and code reusability, essential aspects of effective software design.