Unit 5 - Object Oriented Programmimg
Unit 5 - Object Oriented Programmimg
Monolithic Programming
Procedural Programming
Structured Programming
Object oriented Programming
• Global data is shared and therefore may get altered (mistakenly). © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Structured Programming
Structured programming, also referred to as modular programming.
Advantages
Efficient, correct programs that are easy to understand, debug and change.
• Modules enhance programmer’s productivity.
Many programmers can work on a single, large program
• A structured program takes less time to be written than other programs. • Each
module performs a specific task.
• Each module has its own local data.
First to introduce the concept of functional abstraction.
Disadvantages
• Not data-centered.
• Global data is shared and therefore may get inadvertently modified. 5
• Main focus on functions. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Object Oriented Programming (OOP)
In the object oriented paradigm, the list and the associated operations are treated as one entity known as an
object.
7
Classes, Objects, and Methods
A class is used to describe something in the world, such as occurrences, things,
external entities, and so on. A class provides a template or a blueprint that
describes the structure and behavior of a set of similar objects. Once we have
the definition for a class, a specific instance of the class can be easily created.
A class can have multiple instances or objects. Every object contains some data
and procedures.They are also called methods.
A method is a function associated with a class. It defines the operations that
the object can execute when it receives a message. In OOP language, only
methods of the class can access and manipulate the data stored in an instance
of the class (or object).
Two objects can communicate with each other through messages. An object
8
containership, and polymorphism. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
Data Abstraction and Encapsulation
Data abstraction refers to the process by which data and functions are defined in such a way that only
essential details are revealed and the implementation details are hidden. The main focus of data abstraction
is to separate the interface and the implementation of a program.
Data encapsulation, also called data hiding, is the technique of packing data and functions into a single
component (class) to hide implementation details of a class from the users. Users are allowed to execute
only a restricted set of operations (class methods) on the data members of the class. Therefore,
encapsulation organizes the data and methods into a structure that prevents data access by any function (or
method) that is not specified in the class.This ensures the integrity of the data contained in the object.
11
12
13
Creating an object or instance of a class is known as class instantiation. From the syntax, we can see that class
instantiation uses function notation. Using the syntax, an empty object of a class is created.Thus, we see that
in Python, to create a new object, call a class as if it were a function. The syntax for accessing a class member through
the class object is
Example:
14
to be used. For this reason, they are often referred to as instance methods. © OXFORD UNIVERSITY PRESS 2017. ALL RIGHTS RESERVED.
The __init__() Method (The Class Constructor)
The __init__() method has a special significance in Python classes. The __init__() method is automatically
executed when an object of a class is created. The method is useful to initialize the variables of the class
object. Note the __init__() is prefixed as well as suffixed by double underscores.
Example:
17
19
20
Example:
21
22
Example:
23
Example:
24
26
28
29
30
31