Anda di halaman 1dari 31

CS F213

Object Oriented Programming


BITS Pilani
Hyderabad Campus

Prof.R.Gururaj
CS&IS Dept.

Classes
and
Objects

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Ch.2 of R1.
The Complete Reference- Java, 7th Edition, Herbert
Schildt, Tata McGraw Hill Publishing.
Ch.3 of R2. Object Oriented Analysis and Design with
Applications, Grady Booch, Addison Wesley, 2nd Edition.
And also refer to Class notes.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Objects
The ability to recognize the physical objects is a skill
humans possess.
We understand that an object is a tangible entity that
exhibits some well-defined behavior.
An object can be any of the following:
1. A tangible and/or visible thing.
2. Something towards which our thought or action is
directed.
3. Something that may be understood intellectually.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Real-world objects are not the only kind of objects that are
of interest to us in SW development.
Other important kinds of objects are inventions of the
Design process whose collaboration with other such
objects to provide some high-level behavior or
functionality.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

An object represents an individual, identifiable item,


unit, or entity either in the real world or abstract,
with a well-defined role in the problem domain.
An object is anything that has crisply defined boundary.
Speed, color, temp etc. can not become objects. These
are actually properties of other objects.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

An object has state, behavior, and identity; the


structure and behavior of similar objects are defined
in their common class; the terms instance and object
are interchangeable.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

State of an object
The state of an object encompasses all the properties
(usually static) of an object plus the current (usually
dynamic) values of each of these properties.
All properties have values- simple or another object.
Every object has a state implies that every object takes
up some amount of space, be it in the real world, or
in the computer memory.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Objects encapsulate state.


However we can not capture the full intent of the
abstraction .
Hence we must consider how objects behave.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Behavior of an object
Objects dont stay in isolation.
They keep interacting with each other.
They act upon others and similarly are acted upon by
other objects.
Behavior is how an object acts and reacts, in terms of
its state changes and message passing.
Behavior of an object represents its outwardly
visible activity.
Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

We use the terms operation and message


interchangeably.
Some operations may change the state of an object and
some may not.
The operations that clients may perform upon an
object are generally declared as methods.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

An operation denotes a service that a class offers to its


clients.
Modifier - an operation that alters the state.
Selector - to access the state of an object.
Iterator - to access parts of an object in a well defined
way.
ConstructorDestructor -

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Roles and Responsibilities


The methods of an object comprise its protocol.
The protocol of an object thus defines the envelope of an
objects allowable behavior, and so comprises the entire
static and dynamic view of the object.
It is useful to divide this large protocol into logical
groupings.
Each partition denotes a role it can play.
Each role has some responsibilities.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Objects as machines
Object has state.
Hence it can behave like a machine.
In many cases we can characterize the behavior of an
object using finite state machine.
Active Object owns execution control, autonomous
Passive objectMultiple Active classes means multiple Threads of control.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Identity of an object
Identity is that property of an object which
distinguishes it from all other objects.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Parameter passing.
Call by Value and call by reference.
Object lifetime: It lives from the time it is created (using
new operator) until its space is reclaimed.
Memory leak.
Memory corruption.
Persistent objects transcend the lifetime of the program
that created it.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Relationships among Objects


Objects contribute to the behavior of the system by
collaborating with one another.
Link: Physical or conceptual connection between two
objects.
A link denotes the specific association between objects.
Through this an object applies for a service of another
object.
As a participant in a link, an object may play following
rolesActor; Server; Agent
Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Links among Objects


u: User

d: DB

a: ATM

Message Passing: u passing a message to a. and a passing a


message to d.
Visibility: the object u to pass a message to a, the object a
must be visible to u.
Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Synchronization Objects
When one object sends a message to another object across
a link, the two objects are said to be synchronized.
For objects in completely sequential applications, this
synchronization is achieved by simple method
invocation.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Synchronization Objects
When one active object has a link to a passive object, we
must choose one among the following for
synchronization.
approach

Description

Sequential

The semantics of passive object are guaranteed only in the presence of a


single active object at a time.

Guarded

The semantics of passive object are guaranteed in the presence of


multiple threads of control, but the active clients must collaborate to
achieve mutual exclusion.

Synchronous

The semantics of passive object are guaranteed in the presence of


multiple threads of control, and the supplier guarantees mutual
exclusion.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Aggregation
Links denote peer-to-peer or client/server relationships.
Aggregation denotes a whole-part hierarchy. In the sense it
is a specialized kind of association.
This could be physical or conceptual.
Ex: Desks are part of ClassRoom. (physical)
A graduate program has courses (conceptual).

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Aggregation
Aggregation encapsulates parts as secrets of whole.
Links enable looser coupling among objects.
An object that is an aggregate of another has a link to
aggregate. Aggregate can send a message to part
through this link.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Class
A class represents a set of objects that share a common
structure and a common behavior.
A single object is an instance of a class.
An individual object is a concrete entity that performs
some role in the overall system.
The class captures the structure and behavior common
to all related objects.
Class captures the contractual (between abstraction
and client) decisions in the interface.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Class
The interface of a class provides its outside view and
therefore helps in hiding the structure and secrets of
behavior.
By contrast the implementation of a class is its inside view,
which encompasses the secrets of its behavior.
Types of interfaces:
Public ; Private; Protected
Quality of abstraction

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Relationships among Class


Classes may be related in many interesting ways:

Association (semantic dependency, cardinality)


Inheritance
Aggregation
Using (A class using the services of other class)
Relationships

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Measuring the quality of


abstraction
Coupling (measure of strength of association)
Cohesion (degree of connectivity among the elements
of an abstraction)
Sufficient (capture enough characteristics to perform
meaningful interactions)
Completeness (capture all meaningful characteristics)
Primitiveness (operations need to be primitive)

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Criteria to be considered for


making decisions on operations
Reusability
Complexity
Applicability (is the behavior relevant to the type or
not).
Implementation knowledge (does implementation
depend on the internal details of type?)

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Message passing forms


Synchronous (both sender and receiver wait till both
are ready)
Balking (sender can abandon the operation if receiver
not ready)
Timeout
Asynchronous (sender will not look for the readiness of
receiver)

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Choosing Relationships
Collaborations (to invoke operations)
Aggregation
Inheritance

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Visibility
Supplier is global to client
Supplier object is passed on to the client as a
parameter through some method
Supplier is part of client
Local to client method.

Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Summary

Object - state, identity, and behavior


Active/passive objects
Parameter passing
Types of operations
Link and Aggregation
Class
Relationships
Quality of abstraction
Criteria for operations
Choosing relationships
Prof.R.Gururaj

Object Oriented Programming

BITS Pilani, Hyderabad Campus

Anda mungkin juga menyukai