Home >> Python Tutorial >> Python Polymorphism
Edit Template
Edit Template

Polymorphism in Python in Hindi

What is Polymorphism in Python

Polymorphism, Object-Oriented Programming का एक concept है, python classes and objects programming  मे जब एक function, method या operator different contexts मे different types के objects के साथ काम करता है तो इस concept को polymorphism कहते है।

Key Concepts of Polymorphism

Method Overriding: Polymorphism मे method overriding तब होता है जब child class अपने parent class के method को redefine कर देता है। 

Method Overloading: जब एक ही class मे multiple methods, same name लेकिन different parameters के साथ होते है तब Method Overriding होती है। 

Operator Overloading: Python मे operators को objects के साथ differently behave करने के लिए redefine कर सकते है।

Types of Polymorphism

Polymorphism with Functions and Objects

Python मे हम एक single function को different types के objects के साथ use कर सकते है, ये इस बात पर depend करता है कि object का class क्या provide करता है।

🐍
Example 1.py
Copy to clipboard
class Cat:
    def sound(self):
        return "Meow"
 
class Dog:
    def sound(self):
        return "Bark"
 
def make_sound(animal):
    print(animal.sound())
 
cat = Cat()
dog = Dog()
 
make_sound(cat)  # Output: Meow
make_sound(dog)  # Output: Bark

Polymorphism with Inheritance

Inheritance के through polymorphism को achive कर सकते है जहां child class अपने parent class के method को override करता है।

🐍
Example 2.py
Copy to clipboard
class Animal:
    def sound(self):
        raise NotImplementedError("Subclass must implement abstract method")
 
class Cat(Animal):
    def sound(self):
        return "Meow"
 
class Dog(Animal):
    def sound(self):
        return "Bark"
 
animals = [Cat(), Dog()]
 
for animal in animals:
    print(animal.sound())

Operator Overloading

Operator Overloading मे operator different objects के साथ different types से behave करता है, इसे हम methods के through achieve कर सकते है।

🐍
Example 3.py
Copy to clipboard
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
    def __add__(self, other):
        return Point(self.x + other.x, self.y + other.y)
 
    def __str__(self):
        return f"({self.x}, {self.y})"
 
p1 = Point(1, 2)
p2 = Point(3, 4)
 
print(p1 + p2)  # Output: (4, 6)

उम्मीद करते है कि आपको Python Classes and Objects का एक useful concept Polymorphism अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Python Modules

Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules      2.1. math      2.2. random...

Python Iterators

Table of Contents All Chapters 1. Iterators      1.1. Iterable      1.2. Iterator      1.3. Iterator Protocol...

Python Polymorphism

Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism      3.1. Polymorphism...

Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Edit Template
Scroll to Top