learners support publications object oriented programming

32
Learners Support Publications www.lsp4you.com Object Oriented Object Oriented Programming Programming

Upload: kerry-bradford

Post on 13-Dec-2015

225 views

Category:

Documents


3 download

TRANSCRIPT

Learners Support Publications www.lsp4you.com

Object Oriented Object Oriented ProgrammingProgramming

Learners Support Publications www.lsp4you.com

Objectives of this sessionObjectives of this session

Overview and Characteristics of Overview and Characteristics of Procedure-Oriented ProgrammingProcedure-Oriented Programming

Overview and Characteristics of Object-Overview and Characteristics of Object-Oriented ProgrammingOriented Programming

Object-Oriented Programming – Object-Oriented Programming – DefinitionDefinition

Basic Concepts of Object-Oriented Basic Concepts of Object-Oriented ProgrammingProgramming

Benefits of Object-Oriented ProgrammingBenefits of Object-Oriented Programming

Learners Support Publications www.lsp4you.com

Object Oriented Object Oriented ProgrammingProgrammingOOP is an approach to program OOP is an approach to program

organization and development that organization and development that attempts to eliminate some of the attempts to eliminate some of the

pitfalls of conventional programming pitfalls of conventional programming methods by incorporating the best of methods by incorporating the best of

structured programming features with structured programming features with several new concepts.several new concepts.

Learners Support Publications www.lsp4you.com

Procedure-Oriented Procedure-Oriented ProgrammingProgramming

Conventional programming using high Conventional programming using high level languages like COBOL, FORTRAN, level languages like COBOL, FORTRAN, C, etc.C, etc.

The problem is viewed as a sequence of The problem is viewed as a sequence of things to be done.things to be done.

The primary focus is on functions.The primary focus is on functions. Procedure-oriented programming Procedure-oriented programming

basically consists of writing a list of basically consists of writing a list of instructions for the computer to follow instructions for the computer to follow and organizing these instructions into and organizing these instructions into groups known as functions.groups known as functions.

Learners Support Publications www.lsp4you.com

Typical structure of procedure-oriented Typical structure of procedure-oriented programprogram

Main Program

Function-1 Function-2 Function-3

Function-4 Function-5

Function-6 Function-7 Function-8

Learners Support Publications www.lsp4you.com

Procedure-Oriented Procedure-Oriented ProgrammingProgramming

To revise an external data structure, To revise an external data structure, we also need to revise all functions we also need to revise all functions that access the data.that access the data.

This approach does not model real This approach does not model real world problems. This is because world problems. This is because functions are action-oriented and do functions are action-oriented and do not really correspond to the not really correspond to the elements of the problem.elements of the problem.

continue …

Learners Support Publications www.lsp4you.com

Relationship of data and functions in Relationship of data and functions in procedural programmingprocedural programming

Function-1 Function-2 Function-3

Local Data Local Data Local Data

Global Data Global Data

Learners Support Publications www.lsp4you.com

Characteristics of Characteristics of Procedure-Oriented Procedure-Oriented

ProgrammingProgramming Emphasis is on doing things.Emphasis is on doing things. Large programs are divided into smaller Large programs are divided into smaller

programs known as functions.programs known as functions. Most of the functions share global data.Most of the functions share global data. Data move openly around the system Data move openly around the system

from function to function.from function to function. Functions transform data from one form Functions transform data from one form

to another.to another. Employs top-down approach in program Employs top-down approach in program

design.design.

Learners Support Publications www.lsp4you.com

Object-Oriented Object-Oriented ProgrammingProgramming

OOP treat data as a critical element in OOP treat data as a critical element in the program development and does not the program development and does not allow it to flow freely around the system.allow it to flow freely around the system.

It ties data more closely to the functions It ties data more closely to the functions that operate on it, and protects it from that operate on it, and protects it from accidental modification from outside accidental modification from outside functions.functions.

OOP allows decomposition of a problem OOP allows decomposition of a problem into a number of entities called objects into a number of entities called objects and then build data functions around and then build data functions around these objects.these objects.

Learners Support Publications www.lsp4you.com

Object-Oriented Object-Oriented ProgrammingProgramming

The data of an object can be The data of an object can be accessed only by the functions accessed only by the functions associated with that object.associated with that object.

Functions of one object can access Functions of one object can access the functions of another objects.the functions of another objects.

continue …

Learners Support Publications www.lsp4you.com

Organization of data and functions in Organization of data and functions in OOPOOP

Data

Functions

Object A

Data

Functions

Object B

Data

Functions

Object C

Communication

Learners Support Publications www.lsp4you.com

Characteristics of Object-Characteristics of Object-Oriented ProgrammingOriented Programming

Emphasis is on data rather than Emphasis is on data rather than procedure.procedure.

Programs are divided into objects.Programs are divided into objects. Data structures are designed such that Data structures are designed such that

they characterize the objects.they characterize the objects. Functions that operate on the data of an Functions that operate on the data of an

object are tied together in the data object are tied together in the data structure.structure.

Data is hidden and can not be accessed by Data is hidden and can not be accessed by external functions.external functions.

Learners Support Publications www.lsp4you.com

Characteristics of Object-Characteristics of Object-Oriented ProgrammingOriented Programming

Objects may communicate with each Objects may communicate with each other through functions.other through functions.

New data and functions can be New data and functions can be added easily whenever necessary.added easily whenever necessary.

Follows bottom-up approach in Follows bottom-up approach in program design.program design.

continue …

Learners Support Publications www.lsp4you.com

Object-Oriented Object-Oriented ProgrammingProgramming

Definition:Definition:

It is an approach that provides a way of It is an approach that provides a way of modularizing programs by creating modularizing programs by creating partitioned memory area for both data partitioned memory area for both data and functions that can be used as and functions that can be used as templates for creating copies of such templates for creating copies of such modules on demand. modules on demand. Thus the object is Thus the object is considered to be a partitioned area of considered to be a partitioned area of computer memory that stores data and set of computer memory that stores data and set of operations that can access that data.operations that can access that data.

Learners Support Publications www.lsp4you.com

Basic Concepts of Object-Basic Concepts of Object-Oriented ProgrammingOriented Programming

ObjectsObjects ClassesClasses Data Abstraction and EncapsulationData Abstraction and Encapsulation InheritanceInheritance PolymorphismPolymorphism Dynamic BindingDynamic Binding Message PassingMessage Passing

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

ObjectsObjectsObjects are the basic run-time entities in Objects are the basic run-time entities in

an object-oriented system. They may an object-oriented system. They may represent a person, a place, a bank represent a person, a place, a bank account, etc. Objects take up space in the account, etc. Objects take up space in the memory and have an associated address memory and have an associated address like a structure in C.like a structure in C.

When a program is executed, the objects When a program is executed, the objects interact by sending messages to one interact by sending messages to one another.another.

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

ObjectsObjects

Object : CUSTOMER

DATA AC No. Name of AC Holder Address

FUNCTIONS Deposit Withdrawal AC Balance Display

Object : ACCOUNT

DATA AC No. AC Balance Type of Account

FUNCTIONS Account Balance

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

ClassesClassesClasses are user-defined data types.Classes are user-defined data types.

The entire set of data and code of an object can The entire set of data and code of an object can be made a user-defined data type with the be made a user-defined data type with the help of a class. Objects are variables of the help of a class. Objects are variables of the type class. Once a class has been defined, we type class. Once a class has been defined, we can create any number of objects belonging to can create any number of objects belonging to that class. Each object is associated with the that class. Each object is associated with the data of type class with which they are data of type class with which they are created.created.

A class is a collection of objects of similar type.A class is a collection of objects of similar type.

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Classes Classes

If fruit has been defined as a class, then the If fruit has been defined as a class, then the statementstatement

f r u i t m a n g o ;f r u i t m a n g o ;

will create an object will create an object mangomango belonging to the class belonging to the class fruitfruit..

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Data Abstraction and Encapsulation Data Abstraction and Encapsulation o The wrapping up of data and functions into a The wrapping up of data and functions into a

single unit is known as encapsulation. single unit is known as encapsulation.

o The data is not accessible to the outside world, and The data is not accessible to the outside world, and only those functions which are wrapped in the only those functions which are wrapped in the class can access it. class can access it.

o These functions provide the interface between the These functions provide the interface between the object’s data and the program. This insulation of object’s data and the program. This insulation of the data from direct access by the program is the data from direct access by the program is called data hiding or information hiding.called data hiding or information hiding.

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Data Abstraction and Data Abstraction and Encapsulation Encapsulation

The attributes wrapped in the classes are called data The attributes wrapped in the classes are called data members and the functions that operate on these members and the functions that operate on these data are called methods or member functions.data are called methods or member functions.

Since the classes use the concept of data Since the classes use the concept of data abstraction, they are known as Abstracted Data abstraction, they are known as Abstracted Data Types (ADT).Types (ADT).

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Inheritance Inheritance o Inheritance is the process by which objects of Inheritance is the process by which objects of

one class acquire the properties of objects of one class acquire the properties of objects of another class.another class.

o It supports the concept of hierarchical It supports the concept of hierarchical classification.classification.

o Each derived class shares common Each derived class shares common characteristics with the class from which it is characteristics with the class from which it is derived.derived.

continue …

Learners Support Publications www.lsp4you.com

Property InheritanceProperty InheritanceBird

Attributes:FeathersLay eggs

Flying Bird

Attributes:------------------------

Non-flying Bird

Attributes:------------------------

Robin

Attributes:------------------------

Swallow

Attributes:------------------------

Penguin

Attributes:------------------------

Kiwi

Attributes:------------------------

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Inheritance Inheritance o Inheritance provides the idea of Inheritance provides the idea of

reusability.reusability.

o We can add additional features to an We can add additional features to an existing class without modifying it. existing class without modifying it.

(By deriving new class from existing one. (By deriving new class from existing one. The new class will have the combined The new class will have the combined features of both the classes.)features of both the classes.)

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Polymorphism Polymorphism - ability to take more than - ability to take more than one formone form

o An operation may exhibit different An operation may exhibit different behaviours in different instances.behaviours in different instances.

o The behaviour depends upon the types The behaviour depends upon the types of data used in the operation.of data used in the operation.

o add( 3, 5) gives 8add( 3, 5) gives 8o Add(“hello”, “-world”) gives “hello-Add(“hello”, “-world”) gives “hello-

world”world”

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Polymorphism Polymorphism - ability to take more than one - ability to take more than one formform

o The process of making an operator to exhibit The process of making an operator to exhibit different behaviours in different instances is different behaviours in different instances is known as known as operator overloadingoperator overloading..

o << Insertion Operator<< Insertion Operatoro << Left-shift bit-wise operator<< Left-shift bit-wise operator

o Using a single function name to perform Using a single function name to perform different types of tasks is known as different types of tasks is known as function function overloadingoverloading..

o add( 3, 5) gives 8add( 3, 5) gives 8o Add(“hello”, “-world”) gives “hello-world”Add(“hello”, “-world”) gives “hello-world”

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Dynamic BindingDynamic BindingBinding refers to the linking of a procedure call to Binding refers to the linking of a procedure call to

the code to be executed in response to the call.the code to be executed in response to the call.

Dynamic binding ( late binding ) means that the Dynamic binding ( late binding ) means that the code associated with a given procedure call is code associated with a given procedure call is not known until the time of the call at run-time.not known until the time of the call at run-time.

It is associated with polymorphism and It is associated with polymorphism and inheritance.inheritance.

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Message PassingMessage Passingo An oop consists of a set of objects that An oop consists of a set of objects that

communicate with each other.communicate with each other.o Oop involves the following steps:Oop involves the following steps:

o Creating classes that define objects and their Creating classes that define objects and their behaviour.behaviour.

o Creating objects from class definitions.Creating objects from class definitions.o Establishing communication among objects.Establishing communication among objects.

o Objects communicate with one another Objects communicate with one another by sending and receiving information.by sending and receiving information.

continue …

Learners Support Publications www.lsp4you.com

Basic Concepts of OOPBasic Concepts of OOP

Message PassingMessage Passingo A message for an object is a request for A message for an object is a request for

execution of a procedure.execution of a procedure.o The receiving object will invoke a The receiving object will invoke a

function and generates results.function and generates results.o Message passing involves specifying:Message passing involves specifying:

o The name of the Object.The name of the Object.o The name of the Function.The name of the Function.o The information to be send.The information to be send.

continue …

Learners Support Publications www.lsp4you.com

Benefits of OOPBenefits of OOP

Inheritance – eliminate redundant Inheritance – eliminate redundant code and extend the use of existing code and extend the use of existing classes.classes.

We can build programs from the We can build programs from the standard working module, no need of standard working module, no need of starting from the scratch.starting from the scratch.

Data hiding helps the programmer to build Data hiding helps the programmer to build secure programs that can not be invaded secure programs that can not be invaded by code in other parts of the program.by code in other parts of the program.

Learners Support Publications www.lsp4you.com

Benefits of OOPBenefits of OOP Multiple instances of an objects can Multiple instances of an objects can

co-exists with out any interference.co-exists with out any interference. It is easy to partition the work in a It is easy to partition the work in a

project based on objects.project based on objects. Object-oriented system can be easily Object-oriented system can be easily

upgraded from small to large systems.upgraded from small to large systems. Message passing techniques for Message passing techniques for

communication between objects makes the communication between objects makes the interface descriptions with external interface descriptions with external systems much simpler.systems much simpler.

Software complexity can be easily Software complexity can be easily managed.managed.

continue …

Learners Support Publications www.lsp4you.com

Thank YouThank You