पॉलीमॉर्फ़िज़्म
जानें कि पॉलीमॉर्फ़िज़्म लचीले और डायनेमिक कोड को कैसे संभव बनाता है
पॉलीमॉर्फ़िज़्म, CoddyKit पर Python Academy का एक निःशुल्क पाठ है। यह 6 में से 4वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह Python Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। Python Academy पाठ्यक्रम में कुल 6 पाठ शामिल हैं।
बहुरूपता का परिचय
बहुरूपता ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग (OOP) की एक मूल अवधारणा है, जो अलग-अलग क्लास के ऑब्जेक्ट को किसी समान सुपरक्लास के ऑब्जेक्ट के रूप में उपयोग करने की अनुमति देती है। यह कोड में लचीलापन और पुनः उपयोग की सुविधा प्रदान करती है।
इस पाठ में आप Python में विधियों और क्लास की सहायता से बहुरूपता लागू करना सीखेंगे।

बहुरूपता क्या है?
बहुरूपता अलग-अलग क्लास की विधियों को अलग-अलग व्यवहार करते हुए भी समान नाम साझा करने की अनुमति देती है। इससे कोड अधिक लचीला और विस्तारित करना आसान हो जाता है।
# 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विधियों के साथ बहुरूपता
अलग-अलग क्लास में समान नाम वाली विधियाँ अलग-अलग कार्यान्वयन प्रदान कर सकती हैं।
# 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विधि बहुरूपता का उदाहरण
विधियों के साथ बहुरूपता
अलग-अलग क्लास में समान नाम वाली विधियाँ अलग-अलग कार्यान्वयन प्रदान कर सकती हैं।
# 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फ़ंक्शन के साथ बहुरूपता
बहुरूपता एक ही फ़ंक्शन को अलग-अलग प्रकार के ऑब्जेक्ट पर काम करने की अनुमति देती है।
# 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इनहेरिटेंस के साथ बहुरूपता
बहुरूपता अक्सर इनहेरिटेंस के माध्यम से प्राप्त की जाती है, जिसमें सबक्लास सुपरक्लास की विधियों को ओवरराइड करती हैं।
# 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 में कैसे काम करती है।
- विधियों और इनहेरिटेंस के साथ बहुरूपता लागू करना।
- OOP में बहुरूपता का उपयोग करने के लाभ।
बहुत अच्छा! अब अगले विषय पर चलते हैं।

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