
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
2. Key Concepts of Polymorphism
3.1. Polymorphism with Functions and Objects
3.2. Polymorphism with Inheritance
Polymorphism, Object-Oriented Programming का एक concept है, python classes and objects programming मे जब एक function, method या operator different contexts मे different types के objects के साथ काम करता है तो इस concept को 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 कर सकते है।
Python मे हम एक single function को different types के objects के साथ use कर सकते है, ये इस बात पर depend करता है कि object का class क्या provide करता है।
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
Inheritance के through polymorphism को achive कर सकते है जहां child class अपने parent class के method को override करता है।
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 different objects के साथ different types से behave करता है, इसे हम methods के through achieve कर सकते है।
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 करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...