Object Oriented Paradigm (Java)
Object Oriented Paradigm (Java)
(Java)
S.S.R.K.M.GUPTA. M.Tech. (M.Phil.)
Aditya College of Engineering &
Technology
Surampalem-533437
Structured vs OO Example
message
Deposit
Withdraw
Withdraw
Deposit
Account
balance
messag
e
Account
balance
Show Balance
Show
Balance
message
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
AthleteID: 3557
Name: Arjay Solamo
Status:NEW
Squad:None
22
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
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
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
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
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
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 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