Object Oriented Paradigm
(Java)
S.S.R.K.M.GUPTA. M.Tech. (M.Phil.)
Aditya College of Engineering &
Technology
Surampalem-533437
Software crisis in 1970
(U.S.A)
Relative Cost/Effort in different phases
Cost of Maintenance phase
A Look on Procedure oriented language
Drawbacks of POP languages
67 % of project cost has to utilized in Maintenance
phase.
Software maintenance can be difficult and time
consuming.
When changes are made to the main procedure (top),
those changes can cascade to the sub procedures of
main, and the sub-sub procedures and so on, where the
change may impact all procedures in the pyramid.
Debugging is very difficult if lines of code crosses
30,000, and programmer looses his control on code.
This approach does not model real world problems very
well.
Global data is accessible by all functions, this leads to
bugs.
Object oriented programming paradigm
Structured vs OO Example
message
Deposit
Withdraw
Withdraw
Deposit
Account
balance
messag
e
Account
balance
Show Balance
Show
Balance
(a) Structured Paradigm
9
message
(b) Object Oriented
Paradigm
A C-program using Structure
Functions are called through objects
Each object has its own state
used by its functions
Object (Agent) in the world
What is an Object?
An object is a software bundle of related variables
and methods. Software objects are often used to
model real-world objects you find in everyday life.
Visual representation of a software object
software object
A bicycle modeled as a
Object
It is an entity
with a welldefined
boundary and
identity that
encapsulates
state and
behavior.
Operations
Attributes
Object's State
It is one of the
possible conditions
that an object may
exists in.
It is implemented by a
set of properties
called attributes,
along with its values
and the links it may
have on other objects.
AthleteID: 3556
Name: Joel Santos
Status:NEW
Squad:None
Object's Behavior
It determines
how an object
acts and
reacts.
It is
represented by
the operations
that the object
can perform.
Enrolls()
updateSquad()
Joel Santos
Object's Identity
Although two objects may share
the same state (attributes and
relationships), they are separate,
independent objects with their own
unique identity.
AthleteID: 3556
Name Joel Santos
Status:NEW
Squad:None
AthleteID: 3557
Name: Arjay Solamo
Status:NEW
Squad:None
In C-language, data is not secured
and easily corrupted by others
Each objects data cannot be
corrupted
by others in other class
Features of Object oriented
paradigm
A way of Viewing the World
Agents and Communities
Messages and Methods
Responsibilities
Classes and Instances
Class Hierarchies-Inheritance
Method Binding, Overriding, and
Exceptions
Summary of Object-Oriented
Concepts
Sending Flowers to a Friend
Suppose I (Ravi) wish to send flowers to a
friend, (Venu ), who lives in a city many
miles away.
What should I do?
22
The community of agents
helping me
Gardener
Friend (Venu)
Flower Arranger
Me (Ravi)
Grower
Delivery Person
Agent (Mahesh)
Wholesaler
Venus Florist
(Prasad)
23
An Observation
An object-oriented program is
structured as a community of
interacting agents, called objects.
Each object has a role to play.
Each object provides a service, or
performs an action, that is used by other
members of the community.
24
Agents and Communities
Solution: Find an appropriate agent, namely Mahesh,
and pass to him a message containing my request
It is the responsibility of Mahesh to satisfy my request
There is some method some algorithm or some set of
operations used to satisfy my request
This information, i.e., details, is usually hidden from my
inspection.
An object-oriented program is structured as a
community of interacting agents, called objects
Each object has a role to play
Each object provides a service, or performs an
action, that is used by other members of the
community
25
What is a message in OOP?
A request for an object to perform one of its
operations (methods)
What is a Message?
Software objects interact and communicate with
each other using messages.
The object to which the message is addressed
(YourBicycle)
The name of the method to perform (changeGears)
Any parameters needed by the method (lowerGear)
Messages and Methods
Actions is initiated in object-oriented programming by
the transmission of a message to an agent (an object)
responsible for the action
The message encodes the request for an action
It is accompanied by any additional information
(arguments) needed to carry out the request
The receiver is the object to whom the message is
sent
If the receiver accepts the message, it accepts the
responsibility to carry out the indicated action
In response to a message, the receiver will perform
some method to satisfy the request
Responsibilities
In object oriented programming actions are described in
terms of responsibilities.
A request to perform an action denotes to perform a task
to get desired result.
When a problem is evaluated in terms of responsibilities,
the abstraction level will be increased.
The objects thus become independent from each other
which will help in solving complex problems.
An object has a collection of responsibilities related with
it which termed as protocol.
It is important that objects be allowed to perform their
task however they see fit, without unnecessary
interactions or interference with other objects
Classes and Instances
Every object is instance of a class. A class
groups similar objects.
The class is the repository for behavior
associated with an object.
The method invoked by an object in response
to a message is determined by the class of
the receiver.
Behavior is associated with classes, not with
individual instances. All objects that are
instances of a class use the same method in
response to similar messages.
A class hierarchy of various
material objects
Material Object
Dog
Animal
Plant
Mammal
Flower
Human
Platypus
Shopkeeper Artist
Florist
Flash
Flora
Carnation
Dentist
Painter
Liz
Adam
Phyl
Sallys Flowers
More abstract classes are listed near the top of the tree
More specific classes and individuals are listed near the bottom.
30
Class Hierarchies
Classes are arranged in a treelike structure called a hierarchy
A class may have several ancestors, up to root class
When you define a sub class, you specify its superclass
Every class may have zero or more subclasses
The principle that knowledge of a more general category is
also applicable to a more specific category is called
inheritance.
The class Florist will inherit attributes of the class (or
category) Shopkeeper.
Classes can be organized into a hierarchical inheritance
structure.
A child class (or subclass) will inherit attributes from a
parent class higher in the hierarchy. An abstract parent class
is a class for which there are no direct instances; it is used
only to create subclasses.
31
Inheritance
Inheritance - terminology
Inheritance is a fundamental Object Oriented concept that a new
class acquires the features from another existing class, which
reflects reusability.
Inheritance allows programmer to define a general class
Later you define a more specific class
Adds new details to general definition
New class inherits all properties of initial, general class
A class can be defined as a "subclass" of another class.
The subclass inherits all data attributes of its superclass
The subclass inherits all methods of its superclass
The subclass inherits all associations of its superclass
The subclass can:
Add new functionality
Use inherited functionality
Override inherited functionality
An example of Inheritance
Method binding
Binding is what happens when a method
invocation is bound to an implementation
Involves lookup of the method in the class, or one
or its parents
Both method names and parameters are checked
Binding can happen at two different times
Compile time - static binding ( or ) Early binding
Run time - dynamic binding ( or ) Late binding
Java always supports to dynamic
binding by default.
Method Binding
Objects are used to call methods.
Method Binding is an object that can be used to call
an arbitrary public method, on an instance that is
acquired by evaluating the leading portion of a
method binding expression via a value binding.
It is legal for a class to have two or more methods
with the same name.
Java has to be able to uniquely associate the
invocation of a method with its definition relying on
the number and types of arguments.
Therefore the same-named methods must be
distinguished:
1) by the number of arguments, or
2) by the types of arguments
Overloading and inheritance are two ways to
implement polymorphism.
Method Binding - Example
Method Overriding
Method Overriding
1. Sub class can override the methods defined by the super class.
2. Overridden Methods in the sub classes should have same
name, same signature , same return type and may have either
the same or higher scope than super class method.
3. Java implements Run Time Polymorphism/ Dynamic Method
Dispatch by Method Overriding. [Late Binding]
4. Call to Overridden Methods is Resolved at Run Time.
5. Call to a overridden method is not decided by the type of
reference variable Rather by the type of the object where
reference variable is pointing.
6. While Overriding a Method, the sub class should assign either
same or higher access level than super class method.
Exception - Example
Exception Handling
Exception is an abnormal condition that arises in
the code sequence.
Exceptions occur during compile time or run time.
throwable is the super class in exception
hierarchy.
Compile time errors occurs due to incorrect
syntax.
Run-time errors happen when
User enters incorrect input
Resource is not available (ex. file)
Logic error (bug) that was not fixed
Exception Class Hierarchy
Summary of Object Oriented
Programming
Everything is an object
Objects perform computation by making
requests of each other through the passing of
messages
Every object has it's own memory, which
consists of other objects.
Every object is an instance of a class. A class
groups similar objects.
The class is the repository for behavior
associated with an object
Classes are organized into singly-rooted tree
structure, called an inheritance hierarchy.