Python Object Oriented Programming

  • Python

In this tutorial we’ll learn about object oriented programming in python and it’s fundamental approaches.

Python supports different programming paradigms and this is why it is known as a multi-paradigm programming language.

Just like in real world we need objects to complete a task like to heat something we need a heater. Similarly, we need objects in Python too.

An object consists of two parts, attribute and behavior. Attributes can be defined as properties like age and name of a person. Behavior can be defined as sitting or running that the person will do.

In Python OOP, we have many concepts like Classes and Objects, Encapsulation, Abstraction, Inheritance, and Polymorphism.

Let’s understand these with some basic definitions .

Class

A class in python is like a blueprint using which we can create objects and methods. Methods are functions in OOP. To define a class, we use class keyword.

class Man:
  age = 32

We crated a class called Man and age is the property.

Object

Objects are instances of the class. When we create an object, only then memory is allocated to it. We can create multiple objects for a class.

class Man:
  age = 32

obj1 = Man()
print(obj1.age)

Output :

32

Encapsulation

Restricting access to the variables and methods to prevent accidental data change in Python is known as Encapsulation. In python, we have two types of access specifier private an public.

In functions, we have learned about the public methods that can be accessed outside the class whereas private methods cannot be called outside the class.

Let’s understand using an example where we have a class called Person and we do not want to change the height attribute.

class Person:
  def __init__(self):
    self.__height = 180

  def attr(self):
    print("Height of the person is {}".format(self.__height))

  def changeHeight(self, new):
    self.__height = new

x = Person()
x.attr()

x.__height = 190
x.attr()

Output :

Height of the person is 180
Height of the person is 180

In the above example, we tried to change the height of the Person but we were unable to as we used the private method. Preventing a data change is known as encapsulation.

Inheritance

Inheritance in python is using methods and properties of another class in the current class. The class from which we will be using details is known as Parent Class and the current class is known as Child class.

class Flower:
  def __init__(self):
    print("Color of the flower is Red")

  def name(self):
    print("I am beautiful")

class Tulip(Flower):
  def __init__(self):
    print("I am a Tulip")

 
tul = Tulip()
tul.name()

Output :

I am a Tulip
I am beautiful

Polymorphism

Polymorphism is a combination of two words Poly + Morphism where Poly means Many and Morph means Form. Understand this using a real-life example of a woman having multiple forms like Mother, Daughter, etc.

Polymorphism in Python is the ability to behave differently in different situations using objects. Polymorphism can be implemented in different ways like below.

  • Duck Typing
  • Operator Overloading
  • Method Overloading
  • Method Overriding