📘 Lesson  ·  Lesson 86

Inheritance & Polymorphism

दो OOP स्तंभ

💡 एक नज़र में

Inheritance एक class को दूसरी का code reuse करने देता है। Polymorphism एक ही method को अलग classes में अलग व्यवहार करने देता है।

Inheritance

Python
class Animal:
    def sound(self):
        print("Some sound")

class Dog(Animal):       # Dog, Animal को inherit करता है
    def sound(self):     # override करता है
        print("Bark")

d = Dog()
d.sound()
Bark

Polymorphism

Python
class Cat:
    def sound(self): print("Meow")
class Cow:
    def sound(self): print("Moo")

for animal in [Cat(), Cow()]:
    animal.sound()   # एक ही call, अलग result
Meow Moo

सारांश

  • Inheritance parent code reuse करता; child methods override कर सकता है।
  • Polymorphism = एक method नाम, हर class में अलग व्यवहार।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.