Exploring Python’s OOP Principles with Examples

Here are Oops concepts in python VIZ: Abstraction, Encapsulation, Inheritance and Polymorphism. Beginners in Python face ambiguity while telling answers for these concepts in interviews. This post simplifies those concepts.

A program that is written in any high-level computer language follows these methodologies. Knowing of these would simplify your effort to understand the OOPS concepts.

3 Programming paradigms


  1. Python oops Concepts
  2. 1. Abstraction
    1. Example
  3. 2. Encapsulation
    1. Example
  4. 3. Inheritance
    1. Example
  5. 4. Polymorphism
    1. Operator Overloading
      1. Example
    2. Method Overloading
      1. Example

Python oops Concepts

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

1. Abstraction

It means you only show the implementation details of a particular process and hide the details from the user. You can use it to simplify complex problems by modeling classes appropriate to them.


from abc import ABC,abstractmethod
class employee(ABC):
def emp_id(self,id,name,age,salary):    //Abstraction
class childemployee1(employee):
def emp_id(self,id):
print("emp_id is 12345")
emp1 = childemployee1()

2. Encapsulation

The class is an entity. That has both data and functions. So the clubbing of these two and operating on the data is called encapsulation.

Encapsulation is one of the core principles of the object-oriented paradigm.

Encapsulation simplifies the handling of objects and improves the manageability of software.


class Person:
    "This is a person class"
    age = 10
    def greet(self):
# Output: 10
# Output: <function Person.greet>
# Output: "This is a person class"

3. Inheritance

  • It is possible with the classes that you can sub-classed. This art of dividing the class into a subclass(es) is inheritance.
  • The movie class can be sub-classed as art_movie, commercial_movie, etc.
  • Likewise, the student class can be sub-classed into ‘regular student’ and ‘part_time_student’. 


class Animal:
    def animal_details(self):
        self.name = "Animal"
        self.category = "Mammal"
        self.age = 0
    def show_animal_details(self):
        print("The name of parent class is ", self.name)
class Cat(Animal):
    def cat_details(self):
        self.number_of_paws = 4
        print("A cat has", self.number_of_paws , "pawns")
class Butterfly(Animal):
    def butterfly_details(self):
        self.number_of_wings = 8
        print("A butterfly has", self.number_of_wings, "wings")
class Fish(Animal):
    def fish_details(self):
        self.number_of_fins = 4
        print("A fish has", self.number_of_fins, "fins")
cat = Cat()

4. Polymorphism

Poly means many and morphism is formed, so polymorphism means many forms. Polymorphism can be implemented in many ways.

Operator Overloading

One of the simplest examples of polymorphism is operator overloading. Operator overloading means using the same operator in more than one way. For example, “+” is used between integers to add, with strings for concatenation. Also, you can use it in user-defined data types.


print ("a" + "b")
print(1 + 2)

Method Overloading

Having the same name function in a class with a different number of parameters, or different types of parameters, is called function overloading.


class Person:
    def Hello(self, name=None):
        if name is not None:
            print('Hello ' + name)
           print('Hello ')
# Create instance
obj = Person()
# Call the method
# Call the method with a parameter

The method Hello(), we called here in two ways. This is called method overloading.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.