01 - OO Concepts - v1.5
01 - OO Concepts - v1.5
concepts
□ Functional approach
□ Object-oriented approach
□ Object-oriented concepts
■ Objects
■ Classes
■ Encapsulation
■ Inheritance
■ Polymorphism
■ Abstraction
OOAD
6
Functional/procedural approach
System
Function 1 Function 2
OOAD
7
Functional approach
□ Advantages
■ Easy to apply
■ Work well when data are simple
■ Help to reduce complexity
■ Obtain expected results
□ Disadvantages
■ Functions are separated from data
■ Structure of the system is defined based on the functions, therefore a
change of functions will cause difficulties in change of the structure
■ The system is weakly open
■ Difficult to re-use
■ An significant maintenant cost
OOAD
8
Object-oriented approaches
System Object 2
Object 3
Object 1
Object 4
OOAD
9
Object-oriented approaches
□ Advantages
■ Very close to the real world
■ Easy to reuse
■ Hide information (encapsulation)
■ Lower development cost (inheritance)
■ Suitable for complex systems
OOAD
10
Objects
identity
aPoint
aRectangle
x=0
length = 2 state
y=0
width = 4 move()
origin = aPoint behaviour
area()
OOAD
11
Objects
OOAD
12
Objects
□ Links
■ Between objects, there may be links
■ Example
studies at
Michael the university of Danang
■ Message types
□ constructor
□ destructor
□ getter
□ setter
□ others
OOAD
13
Classes
OOAD
14
Class
□ Relationship
■ There may be relationship between classes
■ A relationship between classes is the set of links between their objects
Studies at
Student University
□ Class/Object
■ An object is an instance of a class
■ A value is an instance of an attribute
■ A link between objects is an instance of the relationship between
classes
OOAD
15
Classes
point1
x=5
Point y=5
x : float move()
y : float
move()
point2
x=0
y=0
Rectangle move()
length : float
width : float
origin: Point aRectangle
area() length = 2
width = 4
origin = point2
area()
OOAD
16
Encapsulation
Class
attributes
methods
OOAD
17
Encapsulation
□ Advantages
■ Hide the information
■ Restrict access to the information from the exterior
■ Avoid the global changes in the whole system: the internal
implementation can be modified without affecting the external users
■ Facilitate the modularity
■ Easy to reuse
■ Easy to maintain
OOAD
18
Inheritance
□ Inheritance allows the reuse of the state and the behaviour of a class by
other classes
□ A class is derived from one or more classes by sharing attributes and
methods
□ Subclass inherits attributes and methods of parent-class
□ Generalisation / Specialisation
■ Generalisation: common attributes of sub-classes are used to
construct the parent-class
■ Specialisation: sub-classes are constructed from the parent-class by
adding other attributes that are unique to them
specialisation
Parent-class
generalisation
Sub-class
OOAD
19
Inheritance
Rectangle Lozenge
multiple inheritance
Square
OOAD
20
Inheritance
□ Advantages
■ Organisation of classes
□ classes are organised hierarchically
□ facilitation of the management of classes
■ Construction of classes
□ sub-classes are constructed from parent-classes
■ Reduction of development cost by avoiding to re-write the code
■ Allowing to apply easily the technique of polymorphism
Polygon
Rectangle Lozenge
multiple inheritance
Square
OOAD
21
Polymorphism
□ Polymorphism of methods
■ Different methods are capable of answering to a request
■ Methods having the same name are defined differently (different
behaviours) in different classes
■ Sub-classes inherit the specification of methods from parent-class and
these methods can be re-defined appropriately
■ Reducing the use of conditional statements (e.g., if-else, switch)
main
Account
credit
debit
execute the transaction
OOAD
22
Polymorphism: dynamic linking
OOAD
23
Abstraction: abstract class
□ An abstract class
■ indicates the common characteristics of the sub-classes
■ can’t have instances/objects
□ A concrete class
■ contains a complete characterization of real-world objects
■ is expected to have instances/objects
Ellipse Polygon
area() area()
perimeter() perimeter()
Circle
Parallelogram Triangle
area()
area() area()
perimeter()
perimeter() perimeter()
OOAD
24
Abstraction: abstract method
Ellipse Polygon
area() area()
perimeter() parimeter()
Circle
Parallelogram Triangle
area()
area() area()
perimeter()
perimeter() perimeter()
OOAD
25