0% found this document useful (0 votes)
3 views

Object Oriented Programming

The document provides an overview of Object-Oriented Programming (OOP) in Python, highlighting key concepts such as classes, objects, methods, inheritance, encapsulation, and polymorphism. It explains how OOP promotes code reusability and efficiency through the creation of objects with attributes and behaviors. Additionally, it emphasizes the importance of encapsulation for data protection and polymorphism for using a common interface across different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Object Oriented Programming

The document provides an overview of Object-Oriented Programming (OOP) in Python, highlighting key concepts such as classes, objects, methods, inheritance, encapsulation, and polymorphism. It explains how OOP promotes code reusability and efficiency through the creation of objects with attributes and behaviors. Additionally, it emphasizes the importance of encapsulation for data protection and polymorphism for using a common interface across different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Object Oriented

Programming
Object Oriented Programming

Python is a multi-paradigm
programming language. It
supports different programming
approaches.
One of the popular approaches
to solve a programming problem
is by creating objects. This is
known as Object-Oriented
Programming (OOP).
An object has two characteristics:
 attributes
 behavior

Let's take an example:


A parrot is an object, as it has the following
properties:
 name, age, color as attributes
 singing, dancing as behavior

• The concept of OOP in Python focuses on


creating reusable code.
• This concept is also known as DRY (Don't
Repeat Yourself).
Class
A class is a blueprint for the object.
We can think of class as a sketch of a parrot with
labels. It contains all the details about the name,
colors, size etc. Based on these descriptions, we
can study about the parrot. Here, a parrot is an
object.
The example for class of parrot can be :
class Parrot:
pass
Here, we use the class keyword to define an
empty class Parrot.
From class, we construct instances.
An instance is a specific object created from a
particular class.
Object
An object (instance) is an instantiation of a
class.

When class is defined, only the description for


the object is defined.

Therefore, no memory or storage is allocated.

The example for object of parrot class can be:


obj = Parrot()
 Here, obj is an object of class Parrot.
Creating Class and Object in Python
class Parrot: # access the class attributes

print("Blu is a
# class attribute {}".format(blu.__class__.species))
species = "bird" print("Woo is also a
{}".format(woo.__class__.species))
# instance attribute
# access the instance attributes
def __init__(self, name,
age):
print("{} is {} years
self.name = name old".format( blu.name, blu.age))
self.age = age print("{} is {} years
old".format( woo.name, woo.age))

# instantiate the Parrot


class Output
Blu is a bird
Woo is also a bird
blu = Parrot("Blu", 10)
Blu is 10 years old
woo = Parrot("Woo", 15) Woo is 15 years old
Methods
Methods are functions defined inside the
body of a class. They are used to define the
behaviours of an object.
Creating Methods in Python
class Parrot: # instantiate the
object
# class attribute
blu = Parrot("Blu",
species = "bird"
10)
# instance attribute
def __init__(self, name, age): # call our instance
self.name = name methods
self.age = age
print(blu.sing("'Happ
y'"))
# instance method
def sing(self, song): print(blu.dance())
return "{} sings {}".format(self.name,
song)
Output
def dance(self): Blu sings 'Happy'
return "{} is now Blu is now dancing
dancing".format(self.name)
Inheritance
Inheritance is a way of creating a new class
for using details of an existing class without
modifying it.
The newly formed class is a derived class (or
child class).
Use of Inheritance in Python
# parent class # child class
class Bird: class Penguin(Bird):

def __init__(self): def __init__(self):


print("Bird is # call super() function
ready") super().__init__()
print("Penguin is ready")
def whoisThis(self):
print("Bird") def whoisThis(self):
print("Penguin")
def swim(self):
def run(self):
print("Swim
faster") print("Run faster")
peggy = Penguin() Output
peggy.whoisThis() Bird is ready
peggy.swim() Penguin is ready
peggy.run() Penguin
Swim faster
Run faster
Encapsulation
Using OOP in Python, we can restrict access
to methods and variables.
This prevents data from direct modification
which is called encapsulation.
In Python, we denote private attributes using
underscore as the prefix
i.e single _ or double __.
Data Encapsulation in Python
class Computer:

def __init__(self):
self.__maxprice = 900 Output
def sell(self):
Selling Price:
print("Selling Price: 900
{}".format(self.__maxprice))
Selling Price:
def setMaxPrice(self, price):
self.__maxprice = price
900
Selling Price:
c = Computer()
c.sell() 1000
# change the price
c.__maxprice = 1000
c.sell()

# using setter function


c.setMaxPrice(1000)
c.sell()
In the above program, we defined a Computer
class.
We used __init__() method to store the
maximum selling price of Computer.
 Here, notice the code
c.__maxprice = 1000
Here, we have tried to modify the value
of __maxprice outside of the class.
However, since __maxprice is a private variable,
this modification is not seen on the output.
As shown, to change the value, we have to use a
setter function
 i.e setMaxPrice() which takes price as a
parameter.
Polymorphism
Polymorphism is an ability (in OOP) to use a
common interface for multiple forms (data types).

Suppose, we need to color a shape, there are


multiple shape options (rectangle, square, circle).

However we could use the same method to color


any shape.

This concept is called Polymorphism.


Using Polymorphism in Python
class Parrot: # common interface
def flying_test(bird):
def fly(self): bird.fly()
print("Parrot can fly")
#instantiate objects
def swim(self): blu = Parrot()
print("Parrot can't swim") peggy = Penguin()

class Penguin: # passing the object


flying_test(blu)
def fly(self): flying_test(peggy)
print("Penguin can't fly")
Output
def swim(self): Parrot can fly
print("Penguin can swim") Penguin can't fly
In the above program, we defined two
classes Parrot and Penguin.

Each of them have a common fly() method.

However, their functions are different.

To use polymorphism, we created a common


interface i.e flying_test() function that takes any
object and calls the object's fly() method.

Thus, when we passed the blu and peggy objects


in the flying_test() function, it ran effectively.
Key Points to Remember:
Object-Oriented Programming makes the
program easy to understand as well as efficient.

Since the class is sharable, the code can be


reused.

Data is safe and secure with data abstraction.

Polymorphism allows the same interface for


different objects, so programmers can write
efficient code.

You might also like