0% found this document useful (0 votes)
108 views51 pages

Unit 5-Object Oriented Programming

This document discusses object-oriented programming and related concepts. It covers: 1) The different programming paradigms including monolithic, procedural, structured, and object-oriented programming. Object-oriented programming emphasizes classes and objects. 2) Key concepts of object-oriented programming like classes, objects, inheritance, polymorphism, and encapsulation. A class acts as a blueprint for creating objects with state and behavior. 3) How to define classes and create objects in Python. The __init__() method is used to initialize an object's state. Methods define the behavior of objects.

Uploaded by

SURABHI ANNIGERI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
108 views51 pages

Unit 5-Object Oriented Programming

This document discusses object-oriented programming and related concepts. It covers: 1) The different programming paradigms including monolithic, procedural, structured, and object-oriented programming. Object-oriented programming emphasizes classes and objects. 2) Key concepts of object-oriented programming like classes, objects, inheritance, polymorphism, and encapsulation. A class acts as a blueprint for creating objects with state and behavior. 3) How to define classes and create objects in Python. The __init__() method is used to initialize an object's state. Methods define the behavior of objects.

Uploaded by

SURABHI ANNIGERI
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 51

Unit 5-Object Oriented

Programming
Computer Programming &
Programming Languages
• A Program is a collection of instructions that tells the computer
how to solve a particular problem.
• We have already done with algorithms and flowcharts.
• Computer Programming goes a step ahead in problem solving.
• Programming is the process of taking an algorithm and writing it
in a particular programming language so it can be understood
by the computer and executed accordingly.
Generations of Programming
Languages
• There were basically 5 generations of programming languages:
• First Generation: Machine Language
• Second Generation: Assembly Language
• Third Generation: High Level language
• Fourth Generation: Very High level Language
• Fifth Generation Programming language
Programming Paradigms
• A Programming Paradigm is a fundamental style of
programming that defines how the structure and basic elements
of a computer program will be built.

• Programming paradigm supports the style of writing programs,


set of capabilities, limitations that a particular program.

• These paradigms, in sequence of their application can be


classified as follows:
Programming Paradigms
• Monolithic Programming – emphasizes on finding a solution
• Procedural Programming – lays stress on algorithms
• Structured Programming – focuses on modules
• Object-oriented Programming – emphasizes on classes and
objects
• Logic-oriented Programming – focuses on goals usually
expressed in calculus
• Rule-oriented Programming – makes use of ‘if-then-else’ rules
for computation
• Constant-oriented Programming – utilizes invariant relationships
to solve a problem
Monolithic Programming
• Programs written using monolithic programming languages
such as assembly language and BASIC consist of global data
and sequential code.
• Global data can be accessed and modified from any part of the
program, thereby, posing a serious threat to its integrity.
• A sequential code is the one in which all instructions are
executed in the specified sequence.
• In order to change the sequence of instructions, jump
statements or goto statements are used.
Monolithic Programming
Global Data
ADB 10
BDB 20
SUM DB?

MOV AX, A
ADD AX, B
MOV SUM, AX Sequential code
JMP STOP with JMP
Instructions
…………………………
……

STOP: .EXIT
Monolithic Programming
• They just have one program module as programming languages
do not support the concept of subroutines.
• Therefore everything is clubbed into a single application.
• This makes the program size huge but also very difficult to
debug and maintain.
Procedural Programming
• In procedural languages, a program is divided into n number of
subroutines that access global data.
• To avoid repetition of code, each subroutine performs a
well-defined task.
• A subroutine that needs the service provided by another
subroutine can call that subroutine.
• Therefore, with 'jump', 'goto', and 'call' instructions, the
sequence of execution of instructions can be altered.
• FORTRAN and COBOL are two popular procedural
programming languages.
Procedural Programming
Structured Programming
• Structured programming is also referred to as modular
programming.
• It was specifically designed to enforce a logical structure on the
program to make it more efficient and easier to understand and
modify.
• Structured programming was basically defined to be used in
large programs that require a large development team to
develop different parts of the same program.
• It applies top-down approach.
• This allows codes to be loaded into the memory more efficiently
and can be reused in other programs
Object Oriented Programming
• With the increase in size and complexity of programs, there was a
need for a new programming paradigm that could help to develop
maintainable programs.
• To implement this, the flaws in previous paradigms had to be
corrected.
• Consequently, OOP was developed.
• It treats data as a critical element in the program development and
restricts its flow freely around the system.
• We have seen that monolithic, procedural, and structured
programming paradigms are task-based as they focus on the actions
the software should accomplish.
Object Oriented Programming
• However, the object oriented paradigm is task-based and
data-based.
• In this paradigm, all the relevant data and tasks are grouped
together in entities known as objects.
Features of Object Oriented
Programming
• The object oriented language must support mechanisms to
define, create, store, manipulate objects and allow to
communicate between objects.
• They are as follows:
• Classes
• Inheritance
• Reusability
• Objects
• Polymorphism
Features of Object Oriented
Programming
• Delegation
• Methods
• Containership
• Data Abstraction and Encapsulation
• Message passing
Classes
• Python is an object oriented programming language.
• Every languages has some basic data types such as int, float,
long and so on.
• Almost everything in Python is an object, with its properties and
methods.
• A class is used to describe something in the world such as
occurrences, things, external entities and so on.
• A class provides the template or a blueprint that describes the
structure and behaviour of a set of similar objects.
• Once we create a class, instances can be very easily created.
Classes

• For ex:
• We create a class named student.
• A student has attribute such as roll no, name, course, and
aggregate.
• Therefore we can say that a class describes one or more similar
objects.
• Thus, a class is a collection of objects.
Defining a class
• A Class is like an object constructor, or a "blueprint" for creating
objects.
• Like function definitions begin with the def keyword, class definitions
begin with a class keyword.
• Syntax: class class_name :
class body
• Ex:
• class MyClass:
x=5
Objects
• An object is an instance of a class.
• A class is like a blueprint while an instance is a copy of the class
with actual values.
• The object is an entity that has a state and behaviour associated with it.
• It may be any real-world object like the mouse, keyboard, chair, table,
pen, etc.
• Integers, strings, floating-point numbers, even arrays, and dictionaries,
are all objects
• When we define a class only the description or a blueprint of the object is
created.
• There is no memory allocation until we create its object.
Objects
• An object consists of :
• State: It is represented by the attributes of an object. It also
reflects the properties of an object.
• Behavior: It is represented by the methods of an object. It also
reflects the response of an object to other objects.
• Identity: It gives a unique name to an object and enables one
object to interact with other objects.
Objects
State / Behaviours
Identity Attributes
Name of the Breed Bark
dog Age Sleep
Color Eat
Declaring Objects
• It is also called as instantiating a class.
• When an object of a class is created, the class is said to be
instantiated.
• All the instances share the attributes and the behaviour of the
class.
• But the values of those attributes, i.e. the state are unique for
each object.
• A single class may have any number of instances.
Declaring Objects
Class Dog

Dog 1 State / Attributes: Dog 3


Breed, Age, Color

Dog 2 Dog 4
Behaviour:
Bark, Eat, Sleep
Declaring Objects
• Syntax: object_name=class_name()
statements

• p1 = MyClass()
print(p1.x)

• Here p1 is the object and MyClass() is the class.


Objects
• So consider the example of student class.
• Therefore if student is a class then all students (eg. 60) in a
course are the objects of the student class.
• Therefore Aditya, Deepti, Isha are all objects of the class .
• As we know a class can have multiple instances.
• Every object contains some data and functions which are called
as Methods.
• These methods store data in variables and responds to the
mesaages that they receive from other objects by executing
their methods.
Method and Message Passing
• Method is a function associated with a class.
• It defines the operation that the object can execute when it
receives the message.
• In OOL, only methods of the class can access and manipulate
the data stored in an instance of the class.
Method and Message Passing
Message : get_details of student with roll no 1

Sender Receiver
Object Object

The data that is transferred with the message is called


parameters.
Method and Message Passing
• Every object of the class has its own set of values.
• Generally the set of values that the object takes at a particular
time is known as the state of the object.
• State can be changed by applying a particular method.
• Therefore we can say that the messages that are sent to other
objects consist of three aspect –
• The receiver object
• Name of the method that the receiver should invoke
• The parameters that must be used with the methods.
__init__() function
• The examples that we saw of classes and objects in their simplest
form, and are not really useful in real life applications.
• To understand the meaning of classes we have to understand the
built-in __init__() function.
• All classes have a function called __init__(), which is always
executed when the class is being initiated.
• Use the __init__() function to assign values to object properties, or
other operations that are necessary to do when the object is being
created.
• Let us see an example for it.
The self Parameter
• The self parameter is a reference to the current instance of the
class and is used to access the variables that belongs to the class.
• It doesn’t mean that the word must be self, you can put any name
you want.
• Let us see an example for it.
Inheritance
• Inheritance is a powerful feature in object oriented programming.
• Inheritance allows us to define a class that inherits all the methods
and properties from another class.
• So here we have 2 classes i.e. Parent class and sub / child class.
• The new class, often known as subclass contains the attributes and
methods of the parent class.
• The one from which it inherits all the methods and properties is
called Parent class.
Inheritance
Parent
features

Parent+
child’s
features
Inheritance
• Python Inheritance syntax:

class BaseClass:
Body of base class
class DerivedClass(Base Class):
Body of Dervied class

Consider this example


Polymorphism
• Polymorphism defines the ability to take different forms.
• Polymorphism in Python allows us to define methods in the
child class with the same name as defined in their parent class.
• Polymorphism is taken from the Greek words Poly (many) and
morphism (forms).
• It means that the same function name can be used for different
types.
Polymorphism in Python
• A child class inherits all the methods from the parent class.
• However, in some situations, the method inherited from the
parent class doesn’t quite fit into the child class.
• In such cases, you will have to re-implement method in the child
class.
• There are different methods to use polymorphism in Python.
• You can use different function, class methods or objects to
define polymorphism.
Containership
• It is the ability of a class to contain objects of one or more
classes as member data.
• For eg: class One can have an object of class Two as its data
member.
• This would allow the object of class One to call the public
functions of class Two.
• Here One becomes the container, whereas class Two becomes
the contained class.
• Containership is also called as composition because as in this
ex, class one is compound of class Two.
Reusability
• It means developing codes that can be reused either in the
same program or in different programs.
• Python gives due importance to building programs that are
reusable.
• It is attained through inheritance, containership and
polymorphism.
Delegation
• To provide maximum flexibility to programmers and to allow
them to generate a reusable code, OOL also supports
delegation.
• Here more than one object is involved in handling a request.
• The object that receives the request for a service, delegates it to
another object called its delegate.
• For ex: Our body is made up of brain, heart, hands, eyes, etc.
• The functionality of the whole body as a system rests on correct
functioning of its parts it is composed of.
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.
• For eg: as users of TV, we can switch on or off, change the
channel, set the volume, and add external devices such as
speaker and all without knowing the details about how its
functionality has been implemented.
• Therefore the internal implementation is completely hidden from
the external world.
Classes and Objects
• So we have completed the concept of class and object.
• The next topic is types of variables
Types of variables
• In OOP’s we have 2 different types of variables:
• Instance variable
• Class Variable / Static variable
Types of variables
• Instance variable: As the object changes, this value also changes.
• If we define a variable inside __init__ method, it is called as a
instance variable
• So we will understand this using an example.
Types of variables
CA
R

Mil=10
Com=BMW

C C
1 2

Mil = 10 Mil = 10
Com=BMW Com=BMW
Types of variables
CA
R

Mil=10
Com=BMW

C C
1 2

Mil = 8 Mil = 10
Com=BMW Com=BMW
Types of variables
• Class Variables:
• They are also called as static variables
• These are the variables which are common for all.
• If we define a variable outside __init__ method but inside a class ,
it is called as a class variable.
• So we will understand this using an example.
Types of variables
CA
R

Mil=10
Com=BMW

Wheels C C Wheels
=4 1 2 =4

Mil = 8 Mil = 10
Com=BMW Com=BMW
Types of variables
CA
R
Wheels =4 Class Variables

__init__
Mil=10 Instance
Variables
Com=BMW
Class method
• A class method is a method that is bound to a class rather than its
object.
• It doesn't require creation of a class instance.
• The syntax is :
@classmethod
def func(cls,args,…)

• Always remember that the first argument in function should be cls.


Public and private members
• Various object-oriented languages like C++, Java, Python control
access modifications which are used to restrict access to the
variables and methods of the class.
• Most programming languages has three forms of access modifiers,
which are public, protected and private in a class.
• A Class in Python has three types of access modifiers:
• Public Access Modifier
• Protected Access Modifier
• Private Access Modifier
Public Access Modifier :
• The members of a class that are declared public are easily
accessible from any part of the program.
• All data members and member functions of a class are public by
default.
• We will see a program for this.
Private Access Modifier :
• The members of a class that are declared private are accessible
within the class only.
• Data members of a class are declared private by adding a double
underscore ‘__’ symbol before the data member of that class.
• We will understand this with the help of an example

You might also like