Polymorphism in OOP

Understanding the ability of objects to take multiple forms in Object-Oriented Programming

What is Polymorphism?

Polymorphism is a fundamental OOP concept that allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to represent different underlying forms (data types).

Polymorphism क्या है? (हिंदी में)

Polymorphism OOP का एक महत्वपूर्ण सिद्धांत है जो अलग-अलग classes के objects को एक common superclass के objects की तरह treat करने की अनुमति देता है। यह एक single interface को different underlying forms (data types) को represent करने में सक्षम बनाता है।

Key Points:

  • Runtime Polymorphism: Achieved through method overriding
  • Compile-time Polymorphism: Achieved through method overloading
  • Interface-based: Can be implemented through interfaces
  • Dynamic Binding: Method calls are resolved at runtime

Types of Polymorphism

Runtime Polymorphism

Achieved through method overriding and virtual functions

Compile-time Polymorphism

Achieved through function overloading and operator overloading

Interface Polymorphism

Achieved through abstract classes and interfaces

Simple Example

#include <iostream>
#include <string>

// Base class
class Shape {
public:
    virtual double calculateArea() = 0;  // Pure virtual function
    virtual void display() {
        std::cout << "This is a shape" << std::endl;
    }
};

// Derived class 1
class Circle : public Shape {
private:
    double radius;

public:
    Circle(double r) : radius(r) {}
    
    double calculateArea() override {
        return 3.14159 * radius * radius;
    }
    
    void display() override {
        std::cout << "This is a circle with radius " << radius << std::endl;
    }
};

// Derived class 2
class Rectangle : public Shape {
private:
    double width;
    double height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}
    
    double calculateArea() override {
        return width * height;
    }
    
    void display() override {
        std::cout << "This is a rectangle with width " << width 
                  << " and height " << height << std::endl;
    }
};

int main() {
    // Create objects
    Circle circle(5);
    Rectangle rectangle(4, 6);
    
    // Use polymorphism through base class pointer
    Shape* shapes[] = {&circle, &rectangle};
    
    // Demonstrate runtime polymorphism
    for (Shape* shape : shapes) {
        shape->display();
        std::cout << "Area: " << shape->calculateArea() << std::endl;
    }
    
    return 0;
}

उदाहरण की व्याख्या (Example Explanation)

इस उदाहरण में polymorphism का उपयोग किया गया है:

  • Base Class (Shape): Pure virtual function calculateArea() और virtual function display()
  • Derived Classes (Circle, Rectangle): calculateArea() और display() functions को override करते हैं
  • Runtime Polymorphism: Base class pointer से derived class objects को access करना
  • Dynamic Binding: Method calls runtime पर resolve होते हैं

Benefits of Polymorphism

Polymorphism के फायदे

  1. कोड का लचीलापन (Code Flexibility)
    • एक ही interface से अलग-अलग implementations को handle कर सकते हैं
    • Code को modify किए बिना नए features add कर सकते हैं
  2. कोड का पुनः उपयोग (Code Reusability)
    • Common interface का उपयोग करके code reuse कर सकते हैं
    • Code duplication को कम करता है
  3. कोड का रखरखाव (Code Maintenance)
    • Code को maintain करना आसान होता है
    • Changes को implement करना आसान होता है

Best Practices

  • Use virtual functions for runtime polymorphism
  • Make base class destructors virtual
  • Use pure virtual functions for abstract classes
  • Keep polymorphic hierarchies shallow
  • Use override keyword to prevent errors