---
title: "Exploring Python’s OOP Principles with Examples"
description: "Here's a quick explanation of Python Object-oriented concepts. These are useful to say in interviews."
url: https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/
date: 2022-01-30
modified: 2023-03-01
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/01/pexels-photo-6476593.jpeg?fit=1880%2C1255&ssl=1
categories: ["Python"]
tags: ["Python oops concepts"]
type: post
lang: en
---

# 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](https://i0.wp.com/srinimf.com/wp-content/uploads/2022/01/programming-1.jpg?resize=765%2C306&ssl=1)

ON THIS PAGE

1. [Python oops Concepts](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#python-oops-concepts)
2. [1. Abstraction](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#1-abstraction)
   1. [Example](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#example)
3. [2. Encapsulation](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#2-encapsulation)
   1. [Example](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#example-program-to-practice-1)
4. [3. Inheritance](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#3-inheritance)
   1. [Example](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#example-program-to-practice-2)
5. [4. Polymorphism](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#4-polymorphism)
   1. [Operator Overloading](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#operator-overloading)
      1. [Example](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#example-program-to-practice-3)
   2. [Method Overloading](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#method-overloading)
      1. [Example](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/#example-program-to-practice-4)

## **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.

### Example

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

## 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.

### Example

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

## 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’. 

### Example

```
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()
cat.animal_details()
cat.show_animal_details()
print(cat.category)
print(cat.age)
cat.cat_details()
```

## 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.](https://www.geeksforgeeks.org/operator-overloading-in-python/)

#### Example

```
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**.

#### Example

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

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

Related posts

- [How to Understand Python Oops Concepts [ Simplified]](https://srinimf.com/2021/08/16/python-inheritance-polymorphism-overloading-tricky-examples/)DateAugust 16, 2021
- [Exclusive Loops Vs Iterators in Python](https://srinimf.com/2021/10/25/exclusive-loops-vs-iterator-object-in-python/)DateOctober 25, 2021
- [How to Access Child-class Methods from Parent-class in Python](https://srinimf.com/2022/09/06/how-to-access-child-class-methods-from-parent-class-in-python/)DateSeptember 6, 2022

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F01%2F30%2Fhow-to-say-about-python-oops-concepts%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F01%2Fpexels-photo-6476593.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=Exploring%20Python%27s%20OOP%20Principles%20with%20Examples)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/01/30/how-to-say-about-python-oops-concepts/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Exploring%20Python%27s%20OOP%20Principles%20with%20Examples&body=https%3A%2F%2Fsrinimf.com%2F2022%2F01%2F30%2Fhow-to-say-about-python-oops-concepts%2F&share=email)
-

### Like this:

Like Loading…
