You are on page 1of 22

9/21/2010

Object Oriented Concepts

Data versus Operations


Data (nouns) versus operations (verbs):
• Nouns
– Data1
–…
– dataN
• Verbs
– operation1
–…
– operationN

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.

Example: Procedural Program


program AddNums {
Integer a;
Integer b;
a = 100;
b = 200;
midPoint(a, b);
procedure midPoint(Integer a, Integer b) {
while(a < b) {
println(“a is " + a); a = a+1;
Println(“b is " + b); b = b-1;
}
}
}

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

Example: Object Definition


• information:
– serial number
– model
– speed
– memory
– Status
• behaviour:
– print file
– stop printing
– remove file from queue

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

• Enclose data inside an object.


• Along with data, include operations on this data.
• Data cannot be accessed from outside except through
the operations.
• Provides data security and facilitates code reuse.
• Operations provide the interface - internal state can
change without affecting the user as long as the
interface does not change.

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

Encapsulation is not Information


Hiding
Enclosing in a capsule. That is enclosing the related
operations and data related to an object into that
object. If encapsulation was “the same thing as
information hiding,” then one might make the
argument that “everything that was encapsulated was
also hidden.” This is obviously not true. For example,
even though information may be encapsulated within
record structures and arrays, this information is usually
not hidden and is available for use. Encapsulation is
just getting a set of operations and data together which
belong together and putting them in a capsule.

Encapsulation & Information hiding


are synonymous 1
Object-Oriented Analysis and Design with Applications by
Grady Booch, Ivar Jacobson, and James Rumbaugh, p. 51:
"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; typically, the structure of an
object is hidden, as well as the implementation of its
methods.
"No part of a complex system should depend on the internal
details of any other part“ .
Whereas abstraction "helps people to think about what they
are doing," encapsulation "allows program changes to be
reliably made with limited effort“ ."

17
9/21/2010

Encapsulation & Information hiding


are synonymous 2
Wikipedia,
http://en.wikipedia.org/wiki/Encapsulation
"In computer science, encapsulation is the hiding of
the internal mechanisms and data structures of a
software component behind a defined interface,
in such a way that users of the component (other
pieces of software) only need to know what the
component does, and cannot make themselves
dependent on the details of how it does it."

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

You might also like