Labs in Software Engineering
Labs in Software Engineering
Fall
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
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
Earning is calculated by
adding the salary to the
bonus and subtracting the
deductions from it
12
Salaried Employee Class
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.
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