Polymorphism
अध्ययन करें कि Polymorphism लचीले और गतिशील Code को कैसे सक्षम बनाता है
Polymorphism, CoddyKit पर पाइथन के साथ एआई सीखें का एक निःशुल्क पाठ है। यह 6 में से 4वाँ पाठ है। इस अध्ययन पथ के 3 तक कोई भी पाठ पूरा पढ़ना निःशुल्क है — इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ व्यावहारिक अभ्यास भी उपलब्ध कराता है। यह पाइथन के साथ एआई सीखें सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। पाइथन के साथ एआई सीखें पाठ्यक्रम में कुल 6 पाठ शामिल हैं।
बहुरूपता का परिचय
बहुरूपता ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (ओओपी) की एक मूलभूत अवधारणा है, जो अलग-अलग वर्गों की वस्तुओं को किसी साझा सुपरक्लास की वस्तुओं के रूप में उपयोग करने की सुविधा देती है। इससे कोड में लचीलापन और पुनःप्रयोग-क्षमता आती है।
इस पाठ में आप विधियों और वर्गों का उपयोग करके पाइथन में बहुरूपता लागू करना सीखेंगे।

बहुरूपता क्या है?
बहुरूपता अलग-अलग वर्गों की विधियों को एक ही नाम साझा करने की अनुमति देती है, जबकि उनका व्यवहार अलग-अलग होता है। इससे कोड अधिक लचीला और विस्तारित करना आसान हो जाता है।
# Defining a superclass
class Animal:
# Method in the superclass
def speak(self):
print("Animal speaks") # Generic message for all animals
# Defining a subclass that overrides the speak method
class Dog(Animal):
def speak(self):
print("Dog barks") # Specific behavior for Dogविधियों with बहुरूपता
अलग-अलग वर्गों में समान नाम वाली विधियाँ अलग-अलग कार्यान्वयन प्रदान कर सकती हैं।
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meowsविधि-आधारित बहुरूपता का उदाहरण
विधियों with बहुरूपता
अलग-अलग वर्गों में समान नाम वाली विधियाँ अलग-अलग कार्यान्वयन प्रदान कर सकती हैं।
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meowsफ़ंक्शनों with बहुरूपता
बहुरूपता एक ही फ़ंक्शन को अलग-अलग प्रकार की वस्तुओं पर काम करने की सुविधा देती है।
# Defining two unrelated classes
class Bird:
def fly(self):
print("Bird flies") # Behavior specific to birds
class Plane:
def fly(self):
print("Plane flies") # Behavior specific to planes
# Function demonstrating polymorphism
# It can accept objects of both Bird and Plane
def flying_object(obj):
obj.fly() # Calls the appropriate fly method based on the object type
# Creating objects of Bird and Plane
bird = Bird()
plane = Plane()
# Calling the function with different objects
flying_object(bird) # Outputs: Bird flies
flying_object(plane) # Outputs: Plane fliesविरासत with बहुरूपता
बहुरूपता अक्सर विरासत के माध्यम से प्राप्त की जाती है, जिसमें उपवर्ग सुपरक्लास की विधियों को ओवरराइड करते हैं।
# Superclass representing a generic vehicle
class Vehicle:
def start(self):
print("Vehicle starts") # Generic behavior for all vehicles
# Subclass representing a specific type of vehicle
class Car(Vehicle):
def start(self):
print("Car starts") # Specific behavior for cars
# Subclass representing another specific type of vehicle
class Bike(Vehicle):
def start(self):
print("Bike starts") # Specific behavior for bikes
# Function demonstrating polymorphism
# It can accept objects of any subclass of Vehicle
def start_vehicle(vehicle):
vehicle.start() # Calls the appropriate start method based on the object type
# Creating objects of Car and Bike
car = Car()
bike = Bike()
# Calling the function with different objects
start_vehicle(car) # Outputs: Car starts
start_vehicle(bike) # Outputs: Bike startsबहुरूपता के लाभ
बहुरूपता के कई लाभ हैं:
- कोड का पुनःप्रयोग और लचीलापन।
- कोड की बेहतर पठनीयता और सुव्यवस्था।
- साझा इंटरफ़ेस के उपयोग को प्रोत्साहन।
वास्तविक दुनिया का उदाहरण
यहाँ बहुरूपता का उपयोग करने वाला वास्तविक दुनिया का उदाहरण दिया गया है।
# Real-world example of polymorphism
class Developer:
def work(self):
print("Writing code") # Specific task for developers
class Designer:
def work(self):
print("Designing user interfaces") # Specific task for designers
# Function demonstrating polymorphism
# It can accept objects of both Developer and Designer
def workday(employee):
employee.work() # Calls the appropriate work method based on the object type
# Creating objects of Developer and Designer
developer = Developer()
designer = Designer()
# Calling the function with different objects
workday(developer) # Outputs: Writing code
workday(designer) # Outputs: Designing user interfacesबहुरूपता के साथ सामान्य गलतियाँ
इन गलतियों से बचें:
- आवश्यक होने पर उपवर्गों में विधियों को अधिलेखित करना भूल जाना।
- बहुविरासत में विधि-समाधान क्रम (MRO) को लेकर भ्रमित होना।
- सरल कार्यों के लिए अनावश्यक रूप से बहुरूपता का उपयोग करना।
हमने क्या सीखा?
इस पाठ में आपने सीखा:
- बहुरूपता क्या है और यह पाइथन में कैसे काम करती है।
- विधियों और विरासत के साथ बहुरूपता को कैसे लागू किया जाता है।
- ओओपी में बहुरूपता का उपयोग करने के लाभ।
बहुत अच्छा! अब अगले विषय पर चलते हैं।

एआई शिक्षक के साथ Python सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 53
- पाठ
- 225
अक्सर पूछे जाने वाले प्रश्न
क्या “Polymorphism” पाठ निःशुल्क है?
हाँ — पाइथन के साथ एआई सीखें अध्ययन पथ के 3 तक कोई भी पाठ, जिसमें “Polymorphism” भी शामिल है, यहाँ वेब पर पूरा पढ़ना निःशुल्क है। इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ इंटरैक्टिव अभ्यास भी उपलब्ध कराता है। पाइथन के साथ एआई सीखें पाठ्यक्रम में कुल 6 पाठ शामिल हैं।
“Polymorphism” में मैं क्या सीखूँगा?
अध्ययन करें कि Polymorphism लचीले और गतिशील Code को कैसे सक्षम बनाता है आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ पाइथन के साथ एआई सीखें का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या पाइथन के साथ एआई सीखें शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर पाइथन के साथ एआई सीखें शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 6 में से 4वाँ पाठ है।
“Polymorphism” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस पाइथन के साथ एआई सीखें पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर पाइथन के साथ एआई सीखें पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- Classes और Objects
- Attributes और Methods
- Inheritance
- Polymorphism
- Encapsulation और Abstraction
- Magic Methods (__str__, __repr__, आदि)