SAD - Ch1 - Introduction To Object-Oriented Concepts
SAD - Ch1 - Introduction To Object-Oriented Concepts
SAD - Ch1 - Introduction To Object-Oriented Concepts
Function 1 Function 2
• 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
4
Object-oriented approaches
Object 3
Object 1
Object 4
5
Object-oriented approaches
• Advantages
• Very close to the real world
• Easy to reuse
• Hide information (encapsulation)
• Lower development cost (inheritance)
• Suitable for complex systems
6
Objects
area()
7
Objects
8
Objects
• Links
• Between objects, there may be links
• Example
studies at
Michael the university of Danang
9
Classes
10
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
11
Classes
12
Encapsulation
attributes
methods
13
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
14
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
generalisation
Parent-class
Sub-class
15
Inheritance
Polygon
Rectangle Lozenge
multiple inheritance
Square
• What is the difficulty of multiple inheritance?
16
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
17
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)
• Procedural approach versus Object-oriented approach Account
credit
debit
main
18
Polymorphism: dynamic linking
• The method to be executed by an object depends on the class of the object: dynamic linking
• The dynamic linking is necessary when
• A variable refers to an object whose class of membership is part of an inheritance tree
• Several methods exist for the same message (name) in the inheritance tree (polymorphism)
int calculateCost(Account accounts)
{
int s = 0;
Account for (int i = 0; i < accounts.length; i++)
calculateCosts()
s = s + accounts[i]->calculateCosts();
calculateInterests() return s;
}
void main()
CurrentAccount SavingAccount {
calculateCosts() calculateCosts() Account accounts = new Account[2];
calculateInterests() calculateInterests() accounts[0] = new CurrentAccount();
accounts[1] = new SavingAccount();
int s = calculateCost(accounts);
…
}
19
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
Figure
area() Abstract class
perimeter()
Ellipse Polygon
area() area()
perimeter() perimeter()
Ellipse Polygon
area() area()
perimeter() parimeter()