Sei sulla pagina 1di 16

OOPS CONCEPTS

Chapter-1/1
OBJECTIVE
Introduction to Object-Oriented Programming
SCOPE
What is Object-Orientation
Why Object-Oriented Programming (OOP)
Object-Oriented Programming versus Traditional Structured Programming
Benefits & Applications of OOP
Object-Oriented Terminology
OOPS CONCEPTS
Chapter-1/2
WHAT IS OBJECT-ORIENTATION
OBJECT
Basic building block for Object-Oriented Programming (OOP).
Usually correspond to real-life entities.
An object is a unit of software comprising of :
(a) State / Attribute . Defines the properties of the object & the current
values of each of these properties.
(b) Behavior . Defines the response of an object reacts in terms of its
state changes & message passing.
(c) Identity . Defines an unique name.
EXAMPLE
Car, Mobile phone, An invoice, John
Object : John
IDENTITY
NAME OF THE PERSON
STATE / ATTRIBUTES
AGE
ADDRESS
PHONE
BODY PARTS
BEHAVIORS
JUMP
WALK
MOVE ARMS
OOPS CONCEPTS
Chapter-1/3
CLASS
User-defined data type.
Used to represent a template for several similar type of objects.
Describes how the objects are structured internally.
Objects of the same class have the same definition both for their state &
behavior.
EXAMPLE
CLASS : PERSON
STATE / ATTRIBUTES
AGE
ADDRESS
PHONE
BODY PARTS
BEHAVIORS
JUMP
WALK
MOVE ARMS
HOME ASSIGNMENT
Identify the state, behavior & identity of objects
(a) Mobile Phone
(b) Car
OOPS CONCEPTS
Chapter-1/4
INSTANCE
Creation of an object for a particular class.
Attributes & behavior are described by the class.
Each instance has a unique identity
Several different instances can be created from the same class.
Each instance will have different internal states depending upon the different
sequences of operations performed.
EXAMPLE
CLASS : PERSON
ATTRIBUTES
AGE
ADDRESS
PHONE
BODY PARTS
BEHAVIORS
JUMP
WALK
MOVE ARMS
instances of instances of instances of
VIVEK RAVI NEHA
OOPS CONCEPTS
Chapter-1/5
OBJECT-ORIENTATION
Object-orientation is a technique for system modeling, where system is modeled
as a set of interacting objects.
EXAMPLE
Surrounding consists of objects like people, cars, trees and houses which are in some
way related to each other.
Advantages
The systems designed using this technology are easy to understand.
Complexity of the software is reduced due to small semantic gap between
reality & the model.
Encourages the reuse of software leading to creation of reusable application
frameworks.
HOME ASSIGNMENT
1. Identify the attributes & behavior of class - Motorcycle ?
2. Identify the instances of class - Motorcycle ?
OOPS CONCEPTS
Chapter-1/6
WHY OBJECT-ORIENTED PROGRAMMING
OBJECT-ORIENTED PROGRAMMING (OOP)

It is a method of programming which uses objects and not algorithms as its
fundamental logical building blocks.

Each object is an instance of some class.

Classes can be related together.
Languages Used For OOP
Object-Oriented Language
Languages which support the concept of objects, classes and inheritance
are called Object-Oriented languages.
Examples of Object-Oriented Languages are :
(a) Smalltalk

(b) Object Pascal

(c) C++

(d) Eiffel

(e) CLOS
Object-Based Language
Languages which support the concept of objects and classes only are
called Object-Based languages.
Example of Object-Based Language is Ada
OOPS CONCEPTS
Chapter-1/7
OOP VERSUS TRADITIONAL STRUCTURED PROGRAMMING
Traditional Structured Programming Object-Oriented Programming
Emphasis on procedures Emphasis on data
Basic building block is function Basic building block is object
Data can be accessed by external
functions.
Data is hidden & cannot be accessed by
external functions
Adding new data & functions is not
easy
New data & functions can be easily
added whenever necessary.
Follows top-down approach in program
design
Follows bottom-up approach in program
design.
Module 1 Module 2 Module 3
WHERE
indicates SHARED DATA
indicates SUBPROGRAM
OBJECT 1
OBJECT 2
OBJECT 3
WHERE
indicates BEHAVIOR
indicates ATTRIBUTE
OOPS CONCEPTS
Chapter-1/8
BENEFITS & APPLICATIONS OF OOP
BENEFITS OF OOP
Data-centered design approach enables us to capture more details of a
model in implementable form.
It is easy to partition the work in a project based on objects.
Software complexity can be easily managed.
The objects & external systems interact with the help of passing messages.
This interface description between the objects & external systems is very
simple.
Principle of data hiding helps to build secure programs that are unaffected
by the code in the other parts of the program.
Upgradation from small to large systems is very easy.
The existing classes can be extended. Thus, eliminating redundant codes.
APPLICATIONS OF OOP
OOP is useful in real-business systems as it can simplify a complex
problem.
Areas where OOP can be used efficiently are as follows :
Object-Oriented databases
Simulation
Hypertext & Hypermedia
Artificial Intelligence & Expert Systems
Neural networks

Parallel programming
OOPS CONCEPTS
Chapter-1/9
IS-A IS-A
IS-A IS-A
HUMAN BEING
MALE FEMALE
IS-A
OBJECT-ORIENTED TERMINOLOGY
INHERITANCE
A property which allows a class to inherit the properties from another class.
Supports the concept of hierarchical classification.
The parent class is known as base class and the descendent class is known as
derived class.
The base class serves as a pattern for the derived class.
EXAMPLE
DIAGRAMMATIC REPRESENTATION OF INHERITANCE
Advantages
Core idea for reuse in the software industry.
Useful for easy modification of models.
Useful for avoiding redundancy , leading to smaller models that are easier to
understand.
MAMMAL
IS-A
ANIMAL
CARNIVOROUS HERBIVOROUS
OOPS CONCEPTS
Chapter-1/10
IS-A
Types Of Inheritance
(a) Single Inheritance

If the derived class inherits from a single parent, the inheritance is said to
be single inheritance.

EXAMPLE

DIAGRAMMATIC REPRESENTATION OF SINGLE INHERITANCE












(b) Multiple Inheritance

If the derived class inherits from two or more parents, the inheritance is
said to be multiple inheritance.

EXAMPLE

DIAGRAMMATIC REPRESENTATION OF MULTIPLE INHERITANCE
IS-A
HUMAN BEING
MALE
FEMALE
BASE CLASS
DERIVED
CLASSES
PET ANIMAL
PET DOG
DOG
BASE CLASSES
DERIVED CLASS
OOPS CONCEPTS
Chapter-1/11
POLYMORPHISM
Ability to take more than one form
The same operation may exhibit different behavior in different instances.
Behavior depends upon the types of data used in the operation.
EXAMPLE
Operation to be performed : Addition
Input Data Type Result
Integer Sum
Character Concatenated String
Advantages
Extensively used in implementing inheritance.
Allows objects having different internal structures to share the same external
interface.
Types Of Polymorphism
Binding refers to the linking of a procedure call to the code to be
executed in response to the call.
(a) Compile-time Polymorphism
Achieved with static binding.
The procedure call is replaced with memory address statically at
compile-time.
Polymorphism is achieved with function overloading.
Function Overloading means that in a program more than one
functions can be defined with the same name & return type. The type
& number of parameters can vary.
OOPS CONCEPTS
Chapter-1/12
EXAMPLE
Let the function defined in a program be :
Add(int , int)
Add(char , char)
Add(int , int , int)
Function call made in the
program
Function which will respond
Add(s, a); Add(char , char)
Add(2,3,4); Add(int , int , int)
Add(4,5); Add(int , int)
(b) Run-time Polymorphism
Achieved with dynamic binding.
At run-time the procedure call matching the object under current
reference will be called.
This gives objects the ability to respond to messages from
routines when the objects exact type is not known till the run-time.
EXAMPLE
DIAGRAMMATIC REPRESENTATION OF RUN-TIME POLYMORPHISM

FUNCTION: DRAW( )
FUNCTION: DRAW( ) FUNCTION: DRAW( ) FUNCTION: DRAW( )
CLASS : CIRCLE CLASS : BOX CLASS : TRIANGLE
EXPLANATION
1. Class SHAPE is a base class
2. Classes CIRCLE, BOX, TRIANGLE are derived classes of SHAPE
3. By inheritance, every derived class object will have the function DRAW( )

OVERLOADED FUNCTIONS
CLASS :
DERIVED CLASSES
BASE CLASS
OOPS CONCEPTS
Chapter-1/13

4. DRAW( ) will be redefined in each derived class such that it draws the
corresponding shape of the object.
5. At run-time, the code matching the current reference object will be executed.
DATA ABSTRACTION
Refers to the act of representing essential features without including the
background details or explanations.
Controls the visibility of information.
Focuses upon the essential characteristics of some object, relative to the
perspective of the viewer.
EXAMPLE
Object : Pet Dog
This concept is used by classes, which are known as Abstract Data Types
(ADT).
A data abstraction consists of the following parts :
(a) A set S of objects, whose representation is undefined.
(b) A set P of operations defined on elements of S.
Sweet, lovable &
playful toy
Perspective of an
owner
Perspective of a
veterinary doctor
Collection of
body parts viz.
heart, kidney,
stomach, lungs,
bones
Perspective of the
person who dislikes
dog
A four legged
animal who
barks & bites.
OOPS CONCEPTS
Chapter-1/14
(c) A set R of rules that define the operations & relationships between
the elements of the set.
EXAMPLE
ABSTRACT DATA TYPE : STACK
The set S contains STACK ( its structure i.e. implementation with arrays or linked list , is
undefined ).
The set P contains PUSH & POP operations.
The set R contains following rules :
1. Initialize the stack to be empty

2. To Pop an element :
(a) Determine if the stack is empty or not.
(b) If the stack is not empty, then retrieve & delete the node at the top of the stack.
(c) If the stack is empty, then pop is an error.

3. To Push an element :
(a) Determine if the stack is full or not.
(b) If the stack is not full, then insert a new node at the top of the stack.
(c) If the stack is full, then push is an error.
ENCAPSULATION
The method of combining the data member & member functions of an object
into a single structure / unit (called class).
Isolates the functional details of the object from outside the class.
Helps in data hiding.
EXAMPLE
1. Atmosphere encapsulates Earth
2. The skin encapsulates the internal body parts (heart, kidney, liver, lungs) from outside
world.
The working of the internal body parts is not known to the world.
EARTH
ATMOSPHERE
OOPS CONCEPTS
Chapter-1/15
The functional details of the object can be controlled by using following
descriptors:
(a) Public Descriptor : This is used for the interface methods that makes
the class reusable across applications.
(b) Private Descriptor : This limits the availability of data or methods to
the class itself.
(c) Protected Descriptor : This limits the availability of data or methods to
the class & any of its derived classes.
EXAMPLE
CLASS : A Birthday Gift
INSTANCE : Sofaset Camera Yearly Planner
Descriptor : PUBLIC PROTECTED PRIVATE
Interface :
Can be used by
everybody at home
as well as all guests
CLASSROOM EXERCISE
Differentiate between encapsulation & abstraction ?
HOME ASSIGNMENT
1. Give examples of different types of inheritance ?
2. Write the data abstraction parts of an ADT - Queue ?
Can be used by the
person only
Can be used by
only near & dear
OOPS CONCEPTS
Chapter-1/16
SUMMARY
Key points covered in this session
The object-oriented approach reflects natural perceptions of the world as
composed of objects.
Object-orientation enhances modularity & reusability.
Object-orientation uses the concept of classes.
Object-oriented programming is based on creating objects from ADT (class) &
defining their relationships
The main ideas involved in object-oriented programming are encapsulation,
inheritance & polymorphism.

Potrebbero piacerti anche