You are on page 1of 55

TMF2243

Object Oriented Software Development

Lecture 05a:
Object-Oriented
Concept

Ref: R.S. Pressman, 2001, A. Bahrami, 1999, A. Eliens, 2000, G. Booch,


1994

1
Objectives
 In this lecture you will learn, the
fundamental concepts of object-orientation.
i. Classes and objects
ii. Attributes
iii. Operations, methods and services
iv. Messages
v. Relationships : Inheritance, Association &
Aggregation
vi. Encapsulation, and polymorphism.

R.S. Pressman, 2001, A. Bahrami, 1999, A. Eliens, 2000, G. Booch, 1994


2
Introduction
 OO first proposed in 1960s, take
almost 20 years for object
technologies to become widely
used. Throughout 1990s, OOSE
become the paradigm of choice
for many software product
builders (why?) (R.S Pressman,
2001),
 We live in the world of objects.
These objects exist in nature,
human-made entities, in business,
and in products that we use.
 They can be categorized,
described, organized, combined,
manipulated, and created. 3
What is an object?
 An abstraction (representation) of something
(entity) in a problem domain reflecting the
capabilities of the system to keep information
about it (attributes), interact with it (provides
services or has methods) (S. Bennett,
et.al.,1999)

4
It Knows things (attributes)
I am an Employee.
I know my name,
IC number and
my address.

I am a Fish. I am a Car.
I know my date of I know my color,
arrival and manufacturer, cost,
expiration. owner and model.

5
It does things (methods)

I know how to
compute
my payroll.

I know how
to cook myself.
I know how to
stop.

6
Class Diagram

7
 Object is whatever an application wants to talk
about.
 For example, Parts and assemblies might
be objects in material applications.

 Account object in bank operations

 Exercise: Name attributes and operations of


object “Book”?
8
 In an object-oriented system,
everything is an object:
numbers, arrays, records,
fields, files, forms, an invoice,
etc.

 An Object is anything, real or


abstract, about which we store
data and methods that
manipulate the data.

9
 Conceptually, each object is
responsible for itself

A window object is
responsible for things like
opening, sizing, and closing
itself.

A chart object is responsible


for things like maintaining its
data and labels, and even for
drawing itself.
10
Two Basic Questions
 When developing an OO application,
two basic questions always arise.

 What objects does the application


need?
 What functionality should those objects
have? domain

System ABC

Object- Object-x

Object- Object-

11
Object-

Traditional Approach

 The traditional approach to software


development tends toward writing a lot of
code to do all the things that have to be
done.

12
OO Approach

 OO approach is more like creating a lot of


helpers that take on an active role, a spirit,
that form a community whose interactions
become the application.

Object- Object-x

Object- Object-

Object-

13
Object’s Attributes

 Attribute represents properties attached


to an object (Jacobson, 1994).

 Object has state, behaviour and a


unique identity.

 In the Car object example


the car’s attributes are:
 color,
manufacturer, cost, owner,
model, etc.
14
Object’s methods

 Methods define an object’s behavior


and specify the way in which an object’s
data are manipulated.

 In the Car example


the car’s methods are:
 drive, lock, tow, carry passenger

15
Object (taken from G. Booch, 1994)

16
Roles and Responsibilities
(Grady Booch, 2007)

OOSD 17
Class

 G. Booch, 1994, defines class as a set of


object that share common structure
(instance variables) and common
behaviour.

 Objects are grouped in classes.

 A single object is an instance of class.

 The role of a class is to define the


attributes and methods (the state and
behavior) of its instances.
18
 The class car, for example, defines the
property color.

 Each individual car (object) will have a


value for this property,
such as "maroon," "yellow" or "white."

19
Employee Class

Salesman
Manager Accountant

John object Jane object Mark object


Object-Oriented Systems
Development Bahrami
© Irwin/ McGraw-Hill
A Class is an Object Template,
or an Object Factory

B o e in g A i r p l a n e O b j e c ts
( B o e in g in s ta n c e s )

17-
22
Oct-22
Class (Taken from G. Booch, 1994)

23
Relationships

 3 types
 Inheritance

 Association

 Aggregation

24
Inheritance - Class hierarchy

 An object-oriented system organizes


classes into superclass-subclass hierarchy.

 At the top of the hierarchy are the most


general classes and at the bottom are the
most specific.

25
 A subclass inherits all of the properties
and methods (procedures) defined in its
superclass.
Motor Vehicle

Bus Truck Car

17-
26
Oct-22
Inheritance

 Inheritance is a relationship between


classes where one class is the parent class
of another (derived) class.

 Inheritance allows classes to share and


reuse behaviors and attributes.

 The real advantage of inheritance is that


we can build upon what we already have
and, reuse what we already have.

27
V ehicle

C ar
I know how to stop
stop m ethod is reusable
F ord

M ustang Taurus Thunderbird

I don’t know how to stop

stop (m yM ustang)
28
Multiple inheritance

 OO systems permit a class to inherit from


more than one superclass.

 This kind of inheritance is referred to as


multiple inheritance.

29
 For example utility vehicle inherits from
Car and Truck classes.
MotorVehicle

Truck Car Bus

UtilityVehicle
30
(taken from G. Booch, 1994)

31
Associations

 Association is defined as a directed binary


relation between objects (Jacobson et
al., 1995). It is normally the associating
object that acts upon and knows of the
associated object.
 For example a pilot can fly planes.

Pilot can fly flown by Planes

32
Aggregation
 Aggregation is defined as a special form of
association that specifies a whole-part
relationship between the aggregate (the whole)
and the component part (the part) (Jacobson,
1999)

33
 For example, the class family (the whole)
is made out of class children and class
parent (the parts), as shown in the Figure
below.
Family

Parent Children

34
Encapsulation and Information Hiding

 Information hiding is the principle of hiding the


internal data and procedures of an object and
providing an interface (protocol) to each object
in such a way as to reveal as little as possible
about its inner working.

Permissible operations

Messages
Yolk (data)
Data
white (process)
Private Protocol shell (interface)
Public Protocol Figure Objects represented by an egg [Avison]

35
 C++ has a general encapsulation protection
mechanism:
 public - may be accessed from any other classes
 private - accessible only from within the class itself
 protected - can be accessed only from subclasses
 Encapsulation in Python:
https://pythonspot.com/encapsulation/
 Often, an object is said to encapsulate the data.
This is to ensure that no object can operate directly
on another object’s data.
36
Examples

 A car engine is an example of


encapsulation, although engine may differ
in implementation, but the interface
between the driver and the car is through
the common protocol.

 TV contains many complex components,


but you do not need to know about them
to use it.
37
Encapsulation (taken from G. Booch,
1994)

38
Message
 Message is defined as a specification of a
communication between objects that conveys
information with the expectation that an
activity will happen (Jacobson et al., 1999).

39
Example

 Objects perform operations in response to


messages.
 For example, object ‘Student’ may
send message registerCourse() to
object ‘Course’. Object course will
triggered operation registerCourse() in
response to the message.

Course
Student registerCourse
+registerCourse()
+displayDetails()
40
Method & message
(taken from G. Booch, 1994)

41
A Case study – A payroll program
 Consider a payroll program that processes
employee records at a small manufacturing
firm. This company has three types of
employees:

 1. Managers: Receive a regular salary.


 2. Office Workers: Receive an hourly
wage and
are eligible for overtime after 40
hours.
 3.Production Workers: Are paid
according to a
piece rate. 42
Structured Approach
FOR EVERY EMPLOYEE DO
BEGIN
IF employee = manager THEN
CALL computeManagerSalary
IF employee = office worker THEN
CALL
computeOfficeWorkerSalary
IF employee = production worker
THEN CALL
computeProductionWorkerSalary
END 43
Add employee types?

 What if we add two new type of employees?


 Temporary office workers ineligible for
overtime,
 Junior production workers who receive an
hourly wage plus a lower piece rate.

44
FOR EVERY EMPLOYEE DO
BEGIN
IF employee = manager THEN
CALL computeManagerSalary
IF employee = office worker THEN
CALL computeOfficeWorker_salary
IF employee = production worker THEN
CALL computeProductionWorker_salary
IF employee = temporary office worker THEN
CALL computeTemporaryOfficeWorkerSalary
IF employee = junior production worker THEN
CALL computeJuniorProductionWorkerSalary
END 17-
45
Oct-22
An OO Approach
 What objects does the application need?
 The goal of OO analysis is to identify objects
and classes that support the problem
domain and system's requirements.
 Some general candidate classes are:
 Persons

 Places

 Things

46
 What are some of the application’s
classes?
 Employee

 Manager

 Office Workers
 Production Workers

47
Class hierarchy

 Identify class hierarchy.


 Identify commonality among the classes.
 Draw the general-specific class hierarchy.

48
Employee
name
address
salary
SS#

OfficeWorker Manager ProductionWorker

dataEntry dataEntry dataEntry


ComputePayroll ComputePayroll ComputePayroll
printReport printReport printReport
49
An OO Approach

FOR EVERY EMPLOYEE DO


BEGIN
employee computePayroll
END

50
If a new class of employee
were added E m p lo y e e
nam e
a d d re ss
sa la ry
SS#

O ffic e W o rk e r M anager P ro d u c tio n W o rk e r

d a ta E n try d a ta E n try d a ta E n try


C o m p u te P a y ro ll C o m p u te P a y ro ll C o m p u te P a y ro ll
p rin tR e p o rt p rin tR e p o rt p rin tR e p o rt

T e m p o ra ry O ffic e W o rk er J u n io rP ro d u c tio n W o rk e r

C o m p u te P a y ro ll C o m p u te P a y ro ll
51
Polymorphism

 Polymorphism means that the same


operation may behave differently on
different classes.
 Example: computePayroll

52
polymorphism example
 For example, consider method
draw graph in objects line graph,
histogram and pie chart.

 When you (other object) send ProduceGraph();


Graph Object

message ‘produce graph’ to each of Draw Graph


these graph objects, it will invoke
method ‘draw graph’.

 This will produce different graph


(result)

53
Objects and Persistence

 Objects have a lifetime.


 An object can persist beyond application
session boundaries, during which the
object is stored in a file or a database, in
some file or database form.

54
Summary

 In this lecture we have looked at the


object basic concepts:
 classes,object, attributes, methods,
message, encapsulation, inheritance,
polymorphism association and
aggregation
 Comparison of Structured Approach and
OO Approach in modeling a simple case
study.
55
Questions?

?
56

You might also like