Concepts: Object-Oriented Programming
Concepts: Object-Oriented Programming
Object-Oriented Programming
Outline
History of programming languages Procedural programming vs. Objectoriented programming Basics concepts of OOP Readings:
Core Java 2, Section 4.1
Basic concepts
Basic concepts
Characteristics of structural / procedural programming paradigm: Use of looping structures: for, while, repeat, do-while Programs as a series of functions/procedures Program source code focus on representing algorithms: how to do things.
Basic concepts
void someProcess() { ... date.day = someDay; date.month = someMonth; date.day = someYear; ...
What if
someDay, someMonth, someYear
Object-oriented programming
class Date { public: void setDate(int newDay, int newMonth, int newYear); int getDay() { return day; } ... private: Data come together with related processing code int day; Allow for guarantee of data consistency and constrainst int month; Easier maintenance int year; }; void Date::setDate(int newDay, int newMonth, int newYear) { //check validity of newDay, newMonth, newYear ... //set new values ... }
i h c Cng ngh . HQG H N i Basic concepts 6
What is OOP?
STUDENT * studentNo - name - birthday + enroll() ... CLASS * courseID - lecturer - instructor + getStudents() + setLecturer() ...
enrols in
Object-Oriented Programming
Map your problem in the real world Define things (objects) which can either do something or have something done to them Create a type (class) for these objects so that you dont have to redo all the work in defining an objects properties and behavior
Basic concepts
Adaptability: software that can evolve over time in response to changing conditions in its environment.
Web browsers and Internet search engines typically involve large programs that are used for many years.
Reusability: the same code should be usable as a component of different systems in various applications.
Save time and money
i h c Cng ngh . HQG H N i Basic concepts 8
Important OO concepts
Abstraction Objects & Class
Object state and behavior Object identity Messages
encapsulation "P.I.E triangle abstraction
inheritance
polymorphism
Encapsulation
Information/implementation hiding
Inheritance Polymorphism
Basic concepts
Abstraction
Abstraction: to distill a complicated system down to its most fundamental parts and describe these parts in a simple, precise language.
naming the parts explaining their functionality
Examples:
Design of data
i h c Cng ngh . HQG H N i
Abstraction
Five basic characteristics of Smalltalk, the first successful object-oriented language
Everything is an object. A program is a bunch of objects telling each other what to do by sending messages. Each object has its own memory made up of other objects. Every object has a type. All objects of a particular type can receive the same messages.
i h c Cng ngh . HQG H N i Basic concepts 11
Objects
Three components:
State:
Given by objects internal data
class Date { public: void setDate(); int getDay(); ... private: int day; int month; int year; }; Date today = new Date(); today.setDate(2, 2, 2010);
Behaviour:
Produced by objects methods
Identity:
Objects in any of its states are unique entities (with unique addresses in memory)
Different objects could have same values for data (same state)
Basic concepts
12
Messages
today.setDate(2, 2, 2010);
A means for object A to request object B to perform one method of Bs. A message consists of: Handle of the destination object host (today) Name of the method to perform (setDate) Other necessary information arguments (2, 2, 2010) In fact, a message is a function call with the host object as the implicit argument (method invocation) However, the concept of messages has great significance to OOP
Data become active Contrast to the old view of programming that
All actions are centrally controlled Data are passive, procedures manipulate data
Basic concepts
13
Object classes
class Date { public: void setDate(); ... private: int day; ... }; Date today = new Date();
Classes
today.setDate(2, 2, 2010);
are the templates to create objects (instantiate). Each object has the same structure and behaviour as the class from which it was created
Basic concepts
14
Inheritance
is-a relations The general classes can be specialized to more specific classes Reuse of interfaces & implementation Mechanism to allow derived classes to possess attributes and operations of base class, as if they were defined at the derived class We can design generic services before specialising them
i h c Cng ngh . HQG H N i Basic concepts 16
Polymophism
Polymorphism:
Ability to assume different forms or shapes. To exist in more than one form
Object polymorphism:
Objects of different derived classes can be treated as if they are of the same class their common base class Objects of different classes understand the same message in different ways
example: on receiving message draw(), Rectangle and Triangle objects perform different methods
Basic concepts
17
OOP languages
Some OOP features can be implemented in C or other procedural programming languages, but not enforced by these languages OOP languages: OOP concepts are embeded in and enforced by the languages. OOP languages vary in degrees of object-oriented
Pure: Smalltalk, Eiffel, Ruby, JADE.. Original OO plus some procedural features: Python, Java (very high), C++ (mixed), C#.. OO features as extension: VB.NET, Fortran 2003, PHP, Perl..
Basic concepts
18
OO in programming languages
Note: OOP does not give good design by default. Example: two pieces of code below are in effect the same. The OOP piece is even worse.
class Date { public: int getDay { return day;} ... int setDay(int newDay) { day = newDay; } ... private: int day; int month; int year; };
Basic concepts
19