Inheritance & Polymorphism
Inheritance और polymorphism क्या हैं
Inheritance और polymorphism object-oriented programming के दो मुख्य विचार हैं। Inheritance एक class को दूसरी का code दोबारा इस्तेमाल करने देता है; polymorphism एक ही method को अलग classes के लिए अलग व्यवहार करने देता है। साथ में ये code को दोबारा-इस्तेमाल-योग्य और लचीला बनाते हैं।
Inheritance — parent दोबारा इस्तेमाल
class Animal:
def eat(self):
return "eating"
class Dog(Animal): # Dog, Animal से inherit करता है
def bark(self):
return "woof"
d = Dog()
print(d.eat()) # inherited
print(d.bark()) # अपना
woof
Dog(Animal) का मतलब Dog, Animal से सब कुछ inherit करता है। तो d.eat() चलता है भले वो Animal में define है, और Dog अपना bark() जोड़ता है।
Method override करना
Child parent के method को उसी नाम का अपना version define करके बदल सकती है।
class Animal:
def speak(self):
return "some sound"
class Cat(Animal):
def speak(self): # override
return "Meow"
print(Cat().speak())
Cat का speak() Animal के को override करता है। Cat object के लिए, parent के बजाय child version चलता है।
super() इस्तेमाल
super() child को parent का method call करने देता है — __init__ में आम।
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, name, roll):
super().__init__(name) # parent हिस्सा set करो
self.roll = roll
s = Student("Aman", 12)
print(s.name, s.roll)
super().__init__(name) पहले parent का setup चलाता है, तो self.name Person संभालता है, और Student सिर्फ़ roll जोड़ता है।
Polymorphism व्यवहार में
class Dog:
def speak(self): return "Woof"
class Cat:
def speak(self): return "Meow"
for animal in [Dog(), Cat()]:
print(animal.speak())
Meow
Loop हर object पर वही speak() call करता है, पर हर class अपना result देती है। Loop को exact type पता होने की ज़रूरत नहीं — यही polymorphism है।
आम गलतियाँ
super().__init__()भूलना। तब parent का setup नहीं चलता और उसके attributes गायब रहते हैं।- Class line में गलत parent नाम।
class Dog(Animal)— parent मौजूद और सही spelling होनी चाहिए। - गलती से override करना। Parent method नाम दोबारा इस्तेमाल उसे बदल देता है — नया method चाहिए तो rename कीजिए।
selfभूलना। हर method को पहले parameter के रूप मेंselfचाहिए।
सारांश
- Inheritance: child class parent की properties और methods दोबारा इस्तेमाल करती है।
- Method override कीजिए child में वही नाम define करके।
super()parent का method call करता है, अक्सर__init__में।- Polymorphism: एक ही method नाम हर class में अलग व्यवहार करता है।
- साथ में ये OOP code को दोबारा-इस्तेमाल-योग्य और लचीला बनाते हैं।
आगे, classes दोहराइए self और __init__ में, या देखिए multiple inheritance और MRO।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
Python में inheritance क्या है?
class Dog(Animal) लिखते हैं, और Dog को अपने-आप वो सब मिलता है जो Animal के पास है, साथ में जो आप जोड़ें। यह साझा code दोहराने से बचाता है।Polymorphism क्या है?
speak() method Dog के लिए "Woof" और Cat के लिए "Meow" कह सकता है। एक ही call, अलग result।super() क्या करता है?
super() parent class का method call करता है। यह सबसे अक्सर __init__ में इस्तेमाल होता है ताकि child पहले parent का हिस्सा set करे, फिर अपना जोड़े — जैसे super().__init__(name)।Method overriding क्या है?
असली code में polymorphism कैसे उपयोगी है?
area() call कर सकते हैं, और हर shape अपना area निकालता है — बिना आपके loop को exact type पता हुए।