Professional Documents
Culture Documents
Object Oriented Concepts: Data Versus Operations
Object Oriented Concepts: Data Versus Operations
1
9/21/2010
Procedural Programming
• Procedural programming is verb-oriented:
– decomposition around operations
– operation are divided into smaller operations
• Programming Languages:
– C
– Pascal
– Fortran, etc.
2
9/21/2010
Drawbacks
• data is given a second-class status when compared with
operations
• difficult to relate to the real world – there are no functions in
real world
• difficult to create new data types – reduces extensibility
• programs are difficult to debug – little restriction to data access
• programs are hard to understand – many variables have global
scope
• programs are hard to reuse – data/functions are mutually
dependent
• little support for developing and comprehending really large
programs
• top-down development approach tends to produce monolithic
programs
Object-Oriented Programming
• Programming defined in terms:
– objects (nouns) and
– relationships between objects
• Object-Oriented programming languages:
– SmallTalk
– C++
– C#
– Java
3
9/21/2010
What is OO?
Definition: Object orientation is about viewing
and modelling the world (or any system) as a
set of interacting and interrelated objects.
• An approach characterized by the following
features:
– views the universe as consisting of interacting
objects
– describes and builds systems consisting of
representation of objects
What is an Object?
• An Object is an abstraction of an Entity from
some information processing point of view
• any abstraction that models a single thing
• a representation of a specific entity in the real
world
• may be tangible (physical entity) or intangible
• Object is a bundle of variables (attributes/state)
and methods (operations/ behavior)
• examples: specific citizen, agency, job, location,
order, your dog .....
4
9/21/2010
Example: Obiects
Object Definition
Two aspects: information and behavior
• Information/Attributes:
– has a unique identity
– has a description of its structure or the information
that is used to create it
– has a state representing its current condition, e.g.
values of some of its features
• Behavior/Operation:
– what can the object do?
– what can be done to it?
5
9/21/2010
What is a Class?
• Rules that define objects
• a definition or template that describes how to build
an accurate representation of a specific type of
objects
• A class is a blueprint that defines the variables and
methods common to all objects of a certain kind.
• Example: ‘your dog’ is a object of the class Dog, car,
agency.
• An object is called an instance of the Class
• Objects are instantiated (created) using class
definitions as templates.
6
9/21/2010
Example: Classes
• Printer
– Information
– Behaviour
Attributes
Definition
• Attribute is a named property of a class that
describes a range of values that instances of the
class may hold for that property.
• An attribute has a type and defines the type of its
instances.
• Only the object itself should be able to change
the values of its attributes.
• The given set of values of the attributes defines
the state of the object.
7
9/21/2010
Example: Attributes
Example: Operations
8
9/21/2010
Operations
Definition
• Operation is the implementation of a service
that can be requested from any object of the
class.
An operation could be:
• Question - does not change the values of the
object
• Command - may change the values of the
object
Object-Oriented Principles
• Four main concepts:
– Abstraction
– Encapsulation
– Inheritance /Hierarchy
– Polymorphism
9
9/21/2010
Abstraction
• Abstraction is a technique that extract
essential details about an item, or a group of
items, while ignoring the inessential details.
• Abstraction allows us to manage complexity
by concentrating on essential characteristics
that makes an entity different from others.
• Removing unnecessary details or things that
are of no relevance for the problem we want
to tackle.
Customer
Salesman
Encapsulation
10
9/21/2010
Encapsulation 1
• Encapsulation is a programming language
feature.
• Encapsulation is the concept of grouping of
data members and member functions.
• Encapsulation means drawing a boundary
around something. It means being able to talk
about the inside and the outside of it.
• When we declare a class we are doing
encapsulation ?
Encapsulation 2
11
9/21/2010
Encapsulation 3
Encapsulation 4
12
9/21/2010
Encapsulation 5
Encapsulation 6
13
9/21/2010
Encapsulation 7
Encapsulation 8
14
9/21/2010
Information Hiding
• Information Hiding is a design principle.
• Inaccessible certain details which would not
affect other parts of the system.
• Design decision should be hidden from the
rest of the system to prevent unintended
coupling.
• Hiding details of implementation
Information Hiding 1
This is more about the methods. Now, you are
trying to hide e.g. make private the
implementation of methods, to hide the design
decisions. For example, you API should return
Interface Object reference but not Class Object
reference and then another client API wouldn't
know how you API is implemented. For example,
if you are getting cash in ATM you should just
type your pin a get your money and you don't
really know how they are transferred to you from
you balance.
15
9/21/2010
Example of Encapsulation
with/without Information hiding
class NoEncapsulationOrInformationHiding {
public ArrayList widths = new ArrayList();
}
class EncapsulationWithoutInformationHiding {
private ArrayList widths = new ArrayList();
public ArrayList getWidths(){
return widths;
}
}
Example of Encapsulation
with/without Information hiding
class InformationHidingWithoutEncapsulation {
public List widths = new ArrayList();
}
class EncapsulationAndInformationHiding{
private ArrayList widths = new ArrayList();
public List getWidths(){
return widths;
}
}
16
9/21/2010
17
9/21/2010
Data Hiding
• Data hiding is a feature provided by the
abstraction for hiding the data from the class.
• Data Hiding is a concept of making all the data
(fields) privete e.g. not accessible from the
other objects, classes, APIs in the system.
• Only class/object/API have to know about the
data and how to operate them.
18
9/21/2010
Inheritance
• Is an ordering of
abstractions into a tree like
structure
• a class obtains variables
and methods from another
class
• the former is called
subclass, the latter super-
class
• a sub-class provides a
specialized behavior with
respect to its super-class
• inheritance facilitates code
reuse and avoids
duplication of data
Inheritance 1
19
9/21/2010
Inheritance 2
Inheritance 3
20
9/21/2010
Inheritance 4
Polymorphism
• Polymorphism = many different (poly) forms of objects
that share a common interface respond differently
when a method of that interface is invoked.
• Polymorphism is enabled by inheritance:
– a super-class defines an interface that all sub-classes must
follow
– it is up to the sub-classes how this interface is
implemented; a subclass may override methods of its
super-class
• Therefore, objects from the classes related by
inheritance may receive the same requests but
respond to such requests in their own ways.
21
9/21/2010
Modularity
• Modularity deals with the process of breaking
up complex systems into small, self contained
pieces that can be managed easily.
• Order Processing System
– Order Entry
– Order Fulfillment
– Billing
22