0% found this document useful (0 votes)
5 views

Labs in Software Engineering

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Labs in Software Engineering

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

2024-2025

Fall

Labs In Software Engineering


Revision on Object Oriented
Programming
Dr. Mahmoud Bassiouni
[email protected]
Revision on OOP Concepts
• Class is the blueprint for an object. It specifies the fields and methods a particular
type of objects has. One or more objects may be created from a class.
• Objects are instances of a class.
• Constructor is a method that is automatically called when an object is created.
• Methods Overloading is when two or more methods in a class have the same
name but their parameters lists are different.
• Methods Overriding is when a subclass provides a specific implementation of a
method that is already provided in one of its superclasses.
• Inheritance allows a new class to extend an existing class. The new class inherits
the members of the
• class it extends.
• Polymorphism means that a superclass reference variable can reference objects
of a subclass.
• Interfaces specify the behavior for a class without implementing it.
• toString() method is a default t method with every class. However it does not of
much use unless you override it and write your own version that does
2 what you
Revision on OOP Concepts

3
Example

• This example shows a class diagram. It explains the classes existing and the
relationships between them. This relationship can be inheritance, association,
composition, aggregation and others.
• It shows a main class or a base class called (Employee). It has three derived
classes or subclasses.
• The first subclass is called SalariedEmployee and this employee takes a fixed
salary.
• The second subclass is called CommissionEmployee and this employee takes a
commission only such as (sales employee).
• The third subclass is called HourlyEmployee and this employee is paid
4
by hour or
the amount of hours.
Full Class Diagram

5
Example

• The class Employee is the base class and it can be the abstract class. Not all the
base classes can be an abstract class. It is abstract because it contains methods
prototype.
• The main attributes are the Name, SSN, address. The methods included are the
setName(), GetName(), SetSSN(), GetSNN(), and Earning().
• It can be seen that the method Earning is abstract. Why? Because its calculation
differs from one type of employee to another.
• It is important to note that any class inherits the employee class must implement
the Earning abstract method in a way that is related to the type of the subclass.
6
Employee Class

After adding the abstract method we need to make the class abstract
7
Employee Class

The Employee class became abstract


and an object cannot be created from it
and to use this class we must inherit it.

8
Example
• The interface is the same as the abstract class. The
main major point in the interface is that it is like a
contract.
• The interface shows some attributes and features
that should available to the classes using it.
• The interface is called displayable which mean we
need to make the subclass have the ability to display
the data inside them.
• Each subclass should have a method called
displayAllDetails(). It will print the values of all
attributes and a method called display Earning() that
will display the salary of each employee.
• All for each employee we may need to print some
attributes such as CompanyName and the Fax.
• Then, all the subclass should inherit the superclass
which is the Employee and implement the interface
called Displayable
9
Displayable Interface

Attributes are by
default final

Methods are by
default abstract

10
Salaried Employee Class

11
Salaried Employee Class

This called implemented


the abstract method in the
superclass called earnings

Earning is calculated by
adding the salary to the
bonus and subtracting the
deductions from it

12
Salaried Employee Class

Return the attributes of the superclass


Return the attributes of the salaried employee class

Call the earning function to


display the total earnings

13
Hourly Employee Class

14
Hourly Employee Class

15
Commission Employee Class

16
Commission Employee Class

17
Example
• The class heriarcy has two levels. The
CommissionEmployee took the inheritance from the
Employee and the BasePlusCommissionEmployee
took the inheritance from the commission Employee.
• BasePlusCommissionEmployee will get all the
attributes and functions in the CommissionEmployee
in addition to it has a base salary. Therefore, it will
have a new method called Set Base.

18
Base Plus Commission Employee

19
Department Class

20
Department Class

21
Department Class

22
Main Class

23
Note
Employee E = new SalariedEmployee();:
•Here, E is a reference of type Employee (assuming SalariedEmployee is a subclass of Employee).
•You can only use methods and fields defined in the Employee class on E, even though the actual
object is of type SalariedEmployee.
•This is an example of polymorphism, where a subclass object is referred to by a superclass
reference. This provides flexibility in handling different types of employees (such as
HourlyEmployee or ContractEmployee), as they can all be referenced by an Employee variable.

SalariedEmployee SE = new SalariedEmployee();


•Here, SE is a reference of type SalariedEmployee.
•You can use both SalariedEmployee and Employee methods and fields on SE.
•This is more restrictive than the first approach because it specifically binds the reference to the
SalariedEmployee type.

Summary:
•Employee E = new SalariedEmployee();: You get the benefits of polymorphism but are limited to
Employee-level functionality.
•SalariedEmployee SE = new SalariedEmployee();: You have full access to
SalariedEmployee-specific functionality but lose the flexibility of referencing it as a general Employee.
24

You might also like