Skip to main content
Recap: Object Oriented Approach
 Four major elements:
 Abstraction
 Encapsulation
 Modularity
 Hierarchy
 Three minor elements:
 Typing
 Concurrency
 Persistence
Abstraction
Car
Jeep
Motor
Bike
LemonTruck
Apple
Banana
Mango
Chiku
Maruti
Wagon-R
Bajaj
Scooter
Abstraction
 Dahl, Dijkstra, and Hoare suggest that “abstraction
arises from a recognition of similarities
between certain objects, situations, or processes in
the real world, and the decision to concentrate
upon these similarities and to ignore for the time
being the differences” [42].
Abstraction
 An abstraction denotes the essential
characteristics of an object that distinguish it from
all other kinds of objects and thus provide crisply
defined conceptual boundaries, relative to the
perspective of the viewer.
Abstraction: Perspective of User
Parent
Child Name
Class
Roll No
Mobile No
Employee
Name
Id No
Mobile No
Tax Payer
Name
Pan No
Annual Return
Bank Customer
Name
Pan No
Acct No
Acct Type
Loyalty Program
Member
Name
Card No
Points
Abstraction
 An abstraction focuses on the outside view of an
object and so serves to separate an object’s
essential behavior from its implementation.
 Deciding on the right set of abstractions for a given
domain is the central problem in object-oriented
design.
Abstraction
 Abstraction or separation of behavior / implementation
can be achieved by applying:
 Principle of least commitment: the interface of an
object provides its essential behavior, and nothing
more
 Principle of least astonishment: an abstraction
captures the entire behavior of some object, no more
and no less, and offers no surprises or side effects that
go beyond the scope of the abstraction
Abstract Data Type (ADT)
 An ADT is a mathematical model of a data
structure that specifies the type of data stored, the
operations supported on them and the type of
parameters of the operations.
 Specifies what each operation does, but not how it
does it
Encapsulation
 Encapsulation hides the details of the
implementation of an object
 Typically, the structure of an object is hidden, as well as
the implementation of its methods.
 Encapsulation is the process of compartmentalizing the
elements of an abstraction that constitute its structure and
behavior;
 Encapsulation serves to separate the contractual interface of
an abstraction and its implementation.
Encapsulation
Encapsulation
 Encapsulation is most often achieved through information
hiding (not just data hiding), which is the process of hiding
all the secrets of an object that do not contribute to its
essential characteristics
Data/Information Hiding
Access
House
X ModifyX () Modify X
Private data – hidden from others
Function to safeguard the private data
Data Hiding
 "... the purpose of hiding is to make inaccessible
certain details that should not affect other parts of a
system.“ [Ross et al, 1975]
Data Hiding
 Data must be hidden/ private
 Read access through read() functions
 Write access through write() functions
 For each data,
 Allow both read and write
 Allow read only
 Allow write only
 No access
Encapsulation / Data Hiding
void modifyX (int newVal) {
if (newVal > 100) or (newVal < 0) {
return error;
}
else
X = newVal
}
void readX() {
return X;
}
Abstraction & Encapsulation
 Complementary concepts:
 The abstraction of an object should precede the decisions about its
implementation.
 Once an implementation is selected, it should be treated as a secret
of the abstraction and hidden from most clients i.e. encapsulated
 “For abstraction to work, implementations must be encapsulated
 Abstraction:
 focuses on the observable behavior of an object
 Encapsulation:
 focuses on the implementation that gives rise to this behavior
Anatomy of a Class
PRIVATE PUBLIC
Data
Private
Functions
Read/ Write
Functions
Constructors/
Destructors
ADT Functions
Pop(), Insert ()
Public Interface
Class
• Realization of an ADT
• State /fields /data
members
• Behavior /methods
/member functions
• Public Interface: signatures
(names, return types, argument
types) of a class’s public member
functions, only part of the class
that can be accessed by a user of
the class
Class
• It defines the data being stored and the operations
supported by the objects that are instances of the
class
• Every class must have two parts:
• an interface – what? – Abstraction
• an implementation – how? – Encapsulation
Abstraction Encapsulation
 External Interface
 The interface of a class
captures only its outside
view, encompassing
abstraction of the
behavior common to all
instances of the class.
 Internal Implementation
 The implementation of a
class comprises the
representation of the
abstraction as well as the
mechanisms that achieve
the desired behavior.
Class, Abstraction & Encapsulation
Abstraction Encapsulation
 Exposes Generic /
Generalized Features
 The interface of a class is
the one place where we
assert all of the
assumptions that a client
may make about any
instances of the class
 Hides implementation
details
 The implementation
encapsulates details
about which no client
may make assumptions.
Class, Abstraction & Encapsulation
In C++ a Class embodies both abstraction and encapsulation
Please read: http://www.tonymarston.co.uk/php-mysql/abstraction.txt
Point Product
class Point
{
public:
Point (double xval, double yval);
void move(double dx, double dy);
double get_x() const;
double get_y() const;
private:
double x;
double y;
};
class Product
{
public:
Product();
void read();
bool is_better_than(Product b) const;
void print() const;
private:
string name;
double price;
int score;
};
Examples
Objects
 Object
 Instantiation of a class
 Initialization of Objects
 Constructors
 Called automatically every time an object is created, ensures proper initialization
 Overcome the problem of improper initialization in procedural languages
 Resource De-allocation
 Destructors
 Ensures de-allocation of resources before the object dies, or goes out of scope
 Overcome memory leaks etc. in procedural languages
Objects
 Life-cycle of an Object
 Born Healthy
 Properly initialized by use of constructors
 Lives Safely
 Using read/write functions, ensure data integrity
 Dies Cleanly
 Using destructors
References
 [Lafore] Chapter 1
 [Booch et al] Chapters 1 & 2
 [Deital & Deital] Chapter 3
 [Horstmann & Budd] Chapter 5
 http://www.tonymarston.co.uk/php-
mysql/abstraction.txt