Polymorphism : महत्व, प्रकार, लाभ व हानियाँ

Polymorphism (बहुरूपता)

Polymorphism, Object-Oriented Programming (OOPs) का एक important feature है| यह शब्द Greek language से लिया गया है, जहाँ Poly का अर्थ है “Many” और Morph का अर्थ है “Forms”।
यानी One Name – Many Forms (एक नाम – अनेक रूप)।

Simple words में, Polymorphism का मतलब है कि एक ही function या operator अलग-अलग situations में अलग-अलग तरह से काम कर सकता है।

Daily life example (दैनिक जीवन का उदाहरण) :

  • Mobile Phone का Power Button सोचिए वही button phone को On भी करता है और Off भी करता है।
  • Paytm App से आप Recharge कर सकते हैं, Bill Payment कर सकते हैं और साथ ही Movie Tickets भी book कर सकते हैं।

Importance of Polymorphism (महत्व)

  • Code Reusability – अगर एक ही function अलग-अलग जगह काम आ जाए, तो हमें नया code बार-बार लिखने की ज़रूरत नहीं पड़ती। इससे समय भी बचता है, और code छोटा व साफ-सुथरा रहता है।
  • Flexibility Polymorphism की वजह से program flexible बन जाता है। एक ही interface अलग-अलग forms में काम कर सकता है।
  • Easy Maintenance – जब functions के नाम common हों, और बस उनका काम अलग हो, तो code को समझना और maintain करना आसान हो जाता है।
  • Extensibility – अगर future में हमें नए features जोड़ने हों तो existing code को बदले बिना भी हम add कर सकते हैं।
  • Run-time Decision– Runtime Polymorphism की वजह से program खुद decide करता है कि कौन-सा function चलना चाहिए। इससे program ज्यादा smart और efficient बनता है।

Types of Polymorphism (प्रकार)

Polymorphism के दो प्रमुख प्रकार होते हैं –

  1. Compile – Time Polymorphism (Static Polymorphism)
  2. Run – Time Polymorphism (Dynamic Polymorphism)

1. Compile-Time Polymorphism (Static Polymorphism) :

Compile-Time Polymorphism, Polymorphism का type है, जिसमें एक ही function या operator के अलग-अलग behaviors compile होने के समय तय (resolve) होते हैं।

प्रमुख बिंदु:

  • इसे Static Polymorphism भी कहते हैं।
  • Compiler decide करता है, कि कौन सा function call होगा।
  • Speed fast होती है, क्योंकि decision run-time पर नहीं होता।
  • Function Overloading और Operator Overloading इसके मुख्य examples हैं।
(a) Function Overloading (फंक्शन ओवरलोडिंग) :-

Function Overloading तब होती है, जब एक ही नाम के multiple functions एक class में होते हैं लेकिन उनके parameters अलग-अलग होते हैं। यह Compile-Time Polymorphism (Static Polymorphism) का एक प्रमुख प्रकार है।

प्रमुख बिंदु :

  • Function name same होना चाहिए।
  • Parameter list में number, type या order अलग होना चाहिए।
  • Return type अलग होने मात्र से overloading नहीं होती।
  • Compiler decide करता है कौन सा function call होगा (compile-time)।

Syntax :

class ClassName {
public:
ReturnType functionName(ParameterList1) {
// Code for first version
}

ReturnType functionName(ParameterList2) {
// Code for second version
}

ReturnType functionName(ParameterList3) {
// Code for third version
}
};

Example :

//  Same function name `add()` is used with DIFFERENT parameters
//  This is where FUNCTION OVERLOADING occurs

#include <iostream>
using namespace std;

//  add() with two parameters

int add(int a, int b) {
return a + b;
}

// add() with three parameters

int add(int a, int b, int c) {
return a + b + c;
}

int main() {
// Compiler decides which add() to call based on arguments
cout << add(10, 20) << endl; // calls add(int, int)
cout << add(10, 20, 30) << endl; // calls add(int, int, int)
return 0;
}

(b) Operator Overloading (ऑपरेटर ओवरलोडिंग) :-

Operator Overloading में existing operators [ +, -,  /, ==, etc. ] को custom meaning देने के लिए redefine करते हैं। यह भी Compile – Time Polymorphism का हिस्सा है।

प्रमुख बिंदु :

  • Mainly C++ में use होता है।
  • Programmer operator का behavior define करता है।
  • Helps to make user-defined objects work like primitive types.

Syntax:

class ClassName {
DataType data; // Class का data member
public:
// Operator overloading function
ReturnType operatorOpName(ClassName obj) {
// Operator का custom behavior define करें
}
};

Example :

// Situation : We want to add two objects using ‘+’ operator

#include <iostream>
using namespace std;

class Number {
public:
int value;

Number(int v) {
value = v;
}

// ‘+’ operator is overloaded to add two Number objects

Number operator + (Number obj) {
Number temp(0);
temp.value = value + obj.value;
return temp;
}
};

int main() {
Number n1(10);
Number n2(20);

// ‘+’ operator works with objects because of operator overloading

Number result = n1 + n2;

cout << result.value; // Output: 30
return 0;
}

2. Run-Time Polymorphism (Dynamic Polymorphism)

Run-time Polymorphism का मतलब है, कि कौन-सा function execute होगा, यह निर्णय (decision) program के run-time पर लिया जाता है।
इसे Late Binding या Dynamic Dispatch भी कहा जाता है।

प्रमुख बिंदु:

  • Compile-Time पर decision नहीं होता, बल्कि Run-Time पर तय होता है।
  • इसका main तरीका है Function Overriding.
  • इसमें Virtual Function का use जरूरी होता है।
  • Object-oriented programs में flexibility और dynamic behavior के लिए उपयोगी है।

Syntax:

class Base {
public:
virtual void functionName(); // Virtual Function
};

class Derived : public Base {
public:
void functionName() override; // Function Overriding
};

int main() {
Base* basePtr;
Derived d;
basePtr = &d;

basePtr->functionName(); // Run-time polymorphism

return 0;
}

(a) Method Overriding (मेथड ओवरराइडिंग) :-
  • जब derived class में base class के function को same signature के साथ redefine किया जाता है।

Syntax:

class Base {
public:
virtual void display(); // virtual function
};

class Derived : public Base {
public:
void display() override; // overriding
};

Example :

// Situation : Child class changes the behavior of parent class method

#include <iostream>
using namespace std;

class Animal {
public:
// Parent class method
virtual void sound() {
cout << “Animal makes a sound” << endl;
}
};

class Dog : public Animal {
public:

// Method Overriding occurs here:
// Same method name, same parameters, different implementation

void sound() {
cout << “Dog barks” << endl;
}
};

int main() {
Animal* a;
Dog d;

a = &d;

// Calls Dog’s sound(), not Animal’s
// This happens due to METHOD OVERRIDING

a->sound();

return 0;
}

(b) Virtual Functions (वर्चुअल फंक्शन )
  • Virtual function वही function है जिसे base class में virtual keyword के साथ declare किया गया हो।
  • इसे overriding के लिए use किया जाता है।

Syntax:

virtual returnType functionName(parameters);

Example :

#include <iostream>
using namespace std;

class Base {
public:
virtual void show() {
cout << “Base class” << endl;
}
};

class Derived : public Base {
public:
void show() {
cout << “Derived class” << endl;
}
};

int main() {
Base* b = new Derived();
b->show();    // Calls Derived’s show() due to virtual function
}

Advantages (लाभ)

  • Code Reusability – Polymorphism के कारण एक ही function अलग-अलग classes या data types के लिए use किया जा सकता है। इससे duplicate code कम होता है।
  • Flexibility in Program – Run-time पर कौन सा function execute होगा, तय किया जा सकता है।
  • Readability and Maintainability Improves – Same नाम के functions होने से code आसानी से समझा जा सकता है। Team work में आसानी होती है।
  • Extensibility – New features add करना आसान हो जाता है क्योंकि existing code को modify करने की जरूरत कम होती है।

Disadvantages (हानि)

  • Performance Impact – Run-Time Polymorphism में Late Binding use होता है, जिससे execution थोड़ा slow हो सकता है।
  • Complexity – बहुत ज्यादा inheritance और overriding होने पर program की complexity बढ़ जाती है।
  • Debugging – अगर multiple overridden functions हों, तो error trace करना थोड़ा challenging हो जाता है।
  • Overhead in Memory – Polymorphic code में extra memory requirement हो सकती है क्योंकि compiler और runtime को extra information store करनी पड़ती है।
error: Content is protected !!