Polymorphism OOP का सबसे स्मार्ट और मज़ेदार concept है।
इससे code इतना flexible बन जाता है कि:
एक ही function
अलग-अलग classes में
अलग-अलग तरीके से behave करता है
बिल्कुल real-life की तरह—
Mobile का एक ही “camera button”
फोटो लेता है
वीडियो रिकॉर्ड करता है
पोर्ट्रेट बनाता है
Mode बदलते ही behavior बदल जाता है।
इसे ही कहते हैं: Polymorphism – Many Forms
एक नाम — कई व्यवहार।
A single method behaves differently in different classes.
C# में polymorphism दो तरह का होता है:
Compile-time → Method Overloading
Run-time → Method Overriding
आज हम दोनों को practical + GPS example में समझेंगे।
एक ही नाम की कई methods,
लेकिन parameters अलग।
void Send(string msg) { }
void Send(string msg, string number) { }
void Send(string msg, string number, int type) { }
Call करते समय compiler decide करता है कौन सा चलेगा।
User discount देता है:
Discount(%)
Discount(amount)
Discount(coupon)
एक method → तीन forms.
Child class parent वाले method को override करती है।
class GPSDevice
{
public virtual void ProcessCommand(string cmd)
{
Console.WriteLine("Processing command in base device");
}
}
class G17 : GPSDevice
{
public override void ProcessCommand(string cmd)
{
Console.WriteLine("G17 command processed");
}
}
class S15 : GPSDevice
{
public override void ProcessCommand(string cmd)
{
Console.WriteLine("S15 specific command processed");
}
}
जब आपका server command जारी करता है:
"REBOOT"
"LOCATION"
"STOP"
"FIND VEHICLE"
तो हर device अलग तरीके से response देता है।
लेकिन आप server पर हमेशा यही call करते हो:
device.ProcessCommand("REBOOT");
Server को यह नहीं पता होता कि device:
G17 है
S15 है
A20 है
TR06 है
फिर भी सही method run होता है!
क्यों? → Polymorphism
GPSDevice d1 = new G17();
GPSDevice d2 = new S15();
d1.ProcessCommand("REBOOT");
d2.ProcessCommand("REBOOT");
Output:
G17 command processed
S15 specific command processed
एक ही command → अलग behavior
यही है polymorphism की ताकत।
Server कहता है:
“Give me battery”
G17 कहता है: “Battery=83%”
S15 कहता है: “B:82;Status=Normal”
LK209 कहता है: “BAT:8.2V”
लेकिन code:
device.GetBattery();
Inside each class:
public override void GetBattery() { … }
Polymorphism का उपयोग होता है:
Payment Gateways (Paytm, UPI, Card, Wallet → Same method)
Notification Systems (SMS, Email, WhatsApp → Same method)
Billing (Different GST rates)
Transport systems (Bike, Car, Truck → Same method Start())
Games (Player, Enemy → Same Move() method)
Polymorphism असल में आपके code को देता है:
✔ Flexibility
✔ Power
✔ Scalability
✔ Clean Architecture
✔ Future-proofing
GPS, Billing, School, E-commerce —
हर बड़े system में polymorphism foundation है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav