The objective of this module is to gain hands-on experience with the following topics:
- Overview of Inheritance
- Working with Inheritance
- Introduction to Polymorphism
- Method Overloading and Method Overriding
- Understand and Implement Inheritance in Java.
- Invoking superclass argument constructors.
Create subclasses of the Employee
class and add the necessary properties and constructors by invoking the superclass constructors.
-
Create Subclass
Developer
:- Extend from
Employee
class with a propertynoOfProjects
. - Create both no-arg and argument constructors, invoking the superclass constructors.
- Extend from
-
Create Subclass
Designer
:- Extend from
Employee
class with a propertynoOfWebsites
. - Create both no-arg and argument constructors, invoking the superclass constructors.
- Extend from
- Understand and implement method overriding.
- Implement runtime polymorphism.
Override calculateNetSalary()
and calculateNetSalaryAfterIncrement()
methods in the Employee
class and in Developer
and Designer
classes respectively.
-
Implement Methods in Employee Class:
float calculateNetSalary()
: Calculate the Net Salary after tax deduction.
float calculateNetSalary() { return grossSalary - federalTax - stateTax; }
float calculateNetSalaryAfterIncrement()
: Calculate the incremented Net Salary after the increment.
float calculateNetSalaryAfterIncrement() { float netSalary; netSalary = calculateNetSalary(); netSalary += (netSalary * incrementPercentage / 100); return netSalary; }
-
Update displayProfile() Method in Employee Class:
- Display the Net Salary and Net Salary after the increment.
-
Override Methods in Developer and Designer Classes:
- Override
calculateNetSalary()
,calculateNetSalaryAfterIncrement()
, anddisplayProfile()
methods with the appropriate logic.
- Override
-
Create Payroll Class:
- Implement
float calculateNetSalary(Employee employee)
to return thecalculateNetSalary()
method of theEmployee
class. - Implement
float calculateNetSalaryAfterIncrement(Employee employee)
to return thecalculateNetSalaryAfterIncrement()
method of theEmployee
class.
- Implement
-
Main Class Implementation:
- Create two
Developer
objects with necessary values. - Create two
Designer
objects with necessary values. - Create a
Payroll
object and invokecalculateNetSalary()
andcalculateNetSalaryAfterIncrement()
methods forDeveloper
andDesigner
objects. - Include SOP messages inside
Developer
andDesigner
class methods to observe which methods are invoked.
- Create two
- Understand and create overloaded methods.
Create overloaded methods for the displayProfile()
method of the Payroll
class with different input parameters.
- Create Overloaded Methods in Payroll Class:
void displayProfile(int empId)
: Implement with an SOP statement to indicate method invocation.void displayProfile(float fromSalaryRange, float toSalaryRange)
: Implement with an SOP statement to indicate method invocation.void displayProfile(String department)
: Implement with an SOP statement to indicate method invocation.
This README provides an overview of the exercises focusing on inheritance, polymorphism, and method overloading/overriding in Java. Follow the tasks to implement the required functionality and observe the expected outputs.