0% found this document useful (0 votes)
25 views2 pages

OOP Concepts: Classes & Inheritance Guide

The document describes a payroll system for a company with four types of employees: salaried employees, hourly employees, commission employees, and employees who earn a base salary plus commissions. It outlines a class hierarchy with an Employee base class and subclasses for each employee type. The Employee class stores basic employee data and an earnings method. Each subclass calculates earnings differently and overrides the earnings method accordingly - salaried uses a weekly salary, hourly uses wage and hours, commission uses sales and commission rate, and base+commission adds a percentage of base salary to commission earnings. The system uses polymorphism so a single earnings calculation can work for any employee type.

Uploaded by

Dibora Tsegaye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

OOP Concepts: Classes & Inheritance Guide

The document describes a payroll system for a company with four types of employees: salaried employees, hourly employees, commission employees, and employees who earn a base salary plus commissions. It outlines a class hierarchy with an Employee base class and subclasses for each employee type. The Employee class stores basic employee data and an earnings method. Each subclass calculates earnings differently and overrides the earnings method accordingly - salaried uses a weekly salary, hourly uses wage and hours, commission uses sales and commission rate, and base+commission adds a percentage of base salary to commission earnings. The system uses polymorphism so a single earnings calculation can work for any employee type.

Uploaded by

Dibora Tsegaye
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Worksheet -1 CSE2202 (Object Oriented Programming)

Part I: Workout (for all questions give description of each line and report it well.)

1) Create a class called Rectangle. The class has two member variables called width and length
and 5 member methods called getData(), setData(), calcArea(), calcPerimeter() and
calcDiagonal(). The methods are intended to perform the following operations:
a) getData(): To get the values of width and height from the user.
b) setData(): To assign the obtained values of width and height to the class member
variables.
c) calcArea(): To calculate the area of the rectangle and display the result.
d) calcPerimeter(): To calculate the perimeter of the rectangle and display the result.
e) calcDiagonal(): To calculate the diagonal of the rectangle and display the result.

2) Rewrite the first question using the following instructions.

a) Constructor method

b) Constructor overloading method

c) Method overloading

d) Nested methods

e) Object parameterized method

3) Modify question 2(a) to prove the use or function of this keyword.


4) Give your own example for cascading constructor.
5) Create class sum which add two integer numbers. Use constructor method sum() to
instantiate instance variables and getsum() method which must return object. NB: - method
getsum() return object not parameter.

1
Worksheet 2- Inheritance and Polymorphism

1. A company named XYZ wants to implement a Java application that performs its payroll calculations
polymorphically. The company pays its employees on a weekly basis. The company has four types of
employees: salaried employees, who are paid a fixed salary regardless of the number of hours
worked; hourly employees, who are paid by the hour and receive overtime pay; commission em-
ployees, who are paid a percentage of their sales; and salaried-commission employees by adding
10% of their salaries.
Use class Employee to represent a “generic” employee. The classes that extend Employee
are SalariedEmployee, CommissionEmployee and HourlyEmployee.
Class BasePlusCommissionEmployee – which extends CommissionEmployee – represents the last
employee type.
An earnings method certainly applies generically to all employees. But each employee’s earnings
calculation depends on the employee’s class.
Each class has the following attributes and methods:

Class Employee  Parameterized constructor


Attributes:  setWage(Double w)
 firstName  getWage ()
 fatherName  setHours(Double h)
 employeeID  getHours ()
 earnings()
Methods:
 Parameterized constructor
Class CommissionEmployee
 setFirstName(String fn)
Attributes:
 getFirstName()
 grossSales
 setLastName(String ln)
 commissionRate
 getLastName()
 setEmployeeID(String eid)
Methods:
 getEmployeeID()
 Parameterized constructor
 earnings()
 setcommissionRate (Double cr)
Class SalariedEmployee
 getcommissionRate ()
Attributes:
 setGrossSales (Double gs)
 weeklySalary
 getGrossSales ()
Methods:
 earnings()
 Parameterized constructor
 setWeeklySalary(Double ws) Class BasePlusCommissionEmployee
 getWeeklySalary () Attributes:
 earnings()  baseSalary
Class HourlyEmployee
Attributes: Methods:
 wage  Parameterized constructor
 hours  setBaseSalary (Double bs)
Methods:  getBaseSalary ()
 earnings()

Common questions

Powered by AI

Encapsulation in the payroll system is maintained through the use of private attributes and public methods, ensuring that an employee’s sensitive data is protected and accessed only through defined interfaces. Methods such as 'setEarnings' or 'getEmployeeID' provide controlled access to modify or retrieve data, thereby governing how data can be altered. This limits exposure of internal state, allowing the system to prevent unauthorized modification or access. The impact on system security is significant as it reduces risks such as data corruption and unauthorized financial manipulations by restricting how data can be accessed and modified .

Polymorphism in payroll calculations is implemented through the use of a superclass 'Employee' and multiple subclasses (SalariedEmployee, HourlyEmployee, CommissionEmployee, and BasePlusCommissionEmployee) that extend the behavior of the superclass. Each subclass overrides the 'earnings' method to provide specific logic for salary calculation based on the type of employee. This allows the system to process a collection of employees and calculate their earnings polymorphically, meaning the system will invoke the correct 'earnings' method at runtime without needing to know the specific type of employee in advance .

Method overriding is crucial in the payroll system for implementing specific earnings calculations for different employee types. Each subclass of Employee, like SalariedEmployee or HourlyEmployee, overrides the 'earnings' method to define its unique calculation logic: SalariedEmployee simply returns a fixed weekly salary, while HourlyEmployee computes pay based on hours worked and wage, accounting for overtime. This design allows each type of employee to maintain its distinct logic while sharing a common interface, facilitating polymorphism in processing employee pay without needing detailed, type-specific checks in the code .

Inheritance plays a crucial role in facilitating polymorphism by allowing subclasses to inherit basic attributes and behaviors from a superclass while adapting or extending them. In the payroll system, the Employee superclass defines common properties and methods, such as personal information and the earnings interface. Subclasses like SalariedEmployee or HourlyEmployee then customize the 'earnings' method based on specific rules, leveraging inherited structures to enable the same operation (like calculating earnings) to behave differently depending on the class of the object being processed. This setup makes the system flexible and scalable by allowing new employee types to be added with minimal changes .

Nested methods can enhance the functionality of a Rectangle class by encapsulating functionality that is used only by a single method, thus keeping related logic closely tied together and potentially improving local reasoning about complex operations. However, their use can lead to drawbacks in terms of code readability and maintenance, as they may increase the complexity of method bodies and make the overall logic harder to read and manage. This can outweigh their advantages, especially if abusive or inappropriate nesting obscures business logic or introduces coupling that is difficult to refactor .

The 'this' keyword is used in object-oriented programming to refer to the current instance of the class. In the context of modifying the Rectangle class constructor, 'this' can be used to distinguish between class attributes and parameters when their names are identical, ensuring the class attributes are correctly assigned the input parameter values. For example, in a constructor like 'this.width = width', 'this' specifies that the width on the left-hand side refers to the member variable, while the right-hand side refers to the parameter .

Constructor overloading allows the Rectangle class to initialize objects with different sets of initial data, such as default values or specified width and length. Method overloading in the Rectangle class can provide multiple versions of the calcArea() method (e.g., for non-rectangular shapes or three-dimensional objects). This flexibility in method application enables more versatile and reusable code, enhancing the object-oriented principles of extensibility and reusability .

The cascading constructor pattern, also known as constructor chaining, improves efficiency by allowing multiple constructors to be linked together. This pattern typically starts with the use of 'this()' to call another constructor from within a constructor. By doing so, shared initialization logic is centralized, reducing code redundancy and improving maintainability. For instance, in the case of initializing a Rectangle object, a cascading constructor can handle default and custom instantiation scenarios without duplicating code. This leads to cleaner, more intuitive code that is easier to debug and extend .

An example scenario of using object parameterized methods could involve a 'PaymentProcessor' class that requires a 'Transaction' object to complete its 'processPayment()' method. In such a setup, the method could receive an initialized 'Transaction' object that contains all necessary details, such as amount, source, and destination accounts. The advantage of this approach lies in its ability to encapsulate behavior related to the transaction within the 'Transaction' class itself, promoting code reusability and clarity. It ensures that payment processing logic is directly tied to a relevant object, reduces parameter list sizes, and leverages well-defined object behaviors, thus enhancing maintainability and scalability .

Parameterized constructors enhance the functionality and flexibility of employee classes by allowing initialization with specific data required for operation. For example, a parameterized constructor in the SalariedEmployee class can initialize attributes like weeklySalary directly upon object creation, ensuring the object is fully configured at the time of instantiation. This approach simplifies object creation, reduces the risk of uninitialized or improperly configured objects, and enables developers to create instances with varying initial states easily, tailored to the application's needs .

You might also like