Polymorphism in Object-Oriented Programming in Hindi – Introduction, Types, and Examples


Polymorphism क्या है?

Object-Oriented Programming (OOP) में Polymorphism एक महत्वपूर्ण अवधारणा है, जिसका शाब्दिक अर्थ है “Many Forms” यानी एक ही चीज़ के कई रूप। Polymorphism का उपयोग एक ही Method को विभिन्न Context में अलग-अलग तरह से उपयोग करने के लिए किया जाता है।

Polymorphism की परिभाषा (Definition of Polymorphism)

Polymorphism एक ऐसी प्रक्रिया है, जिसमें एक Function, Method या Operator को विभिन्न Objects के लिए अलग-अलग तरीकों से उपयोग किया जा सकता है। यह Code को अधिक Dynamic और Reusable बनाता है।

Types of Polymorphism (Polymorphism के प्रकार)

Polymorphism मुख्य रूप से दो प्रकार का होता है:
  1. Compile-Time Polymorphism (Static Polymorphism): इसे Method Overloading और Operator Overloading के रूप में जाना जाता है।
  2. Run-Time Polymorphism (Dynamic Polymorphism): इसे Method Overriding के रूप में जाना जाता है।

1. Compile-Time Polymorphism (Method Overloading)

Compile-Time Polymorphism में एक ही Method का नाम विभिन्न Parameters के साथ उपयोग किया जाता है।

Python Example:

class MathOperations:
    def add(self, a, b, c=0):
        return a + b + c

math = MathOperations()
print(math.add(2, 3))       # Output: 5
print(math.add(2, 3, 4))    # Output: 9

2. Run-Time Polymorphism (Method Overriding)

Run-Time Polymorphism में Child Class Parent Class के Method को Override करती है।

Python Example:

class Animal:
    def sound(self):
        print("Animal makes a sound")

class Dog(Animal):
    def sound(self):
        print("Dog barks")

animal = Animal()
dog = Dog()

animal.sound()  # Output: Animal makes a sound
dog.sound()     # Output: Dog barks

Advantages of Polymorphism

  • Code Reusability: एक ही Method को कई Objects के लिए उपयोग किया जा सकता है।
  • Code Maintainability: Code को Maintain करना आसान बनाता है।
  • Extensibility: नए Features को आसानी से जोड़ा जा सकता है।
  • Dynamic Behavior: Programs अधिक Dynamic बनते हैं।

Applications of Polymorphism

Polymorphism का उपयोग विभिन्न क्षेत्रों में किया जाता है:

  1. Software Development
  2. Game Development
  3. Real-Time Systems
  4. Database Systems
  5. User Interface Design

Conclusion

Polymorphism Object-Oriented Programming का एक शक्तिशाली सिद्धांत है। यह Method Overloading और Method Overriding के माध्यम से Code को अधिक Reusable और Dynamic बनाता है। इसकी समझ Software Design को बेहतर बनाने में सहायक होती है।

Related Post