📘 C# Blog Series – Blog 10 “C# Polymorphism — एक Method, कई रूप (Real-Life Example: अलग-अलग GPS Devices, Same Command)”

📘 C# Blog Series – Blog 10

“C# Polymorphism — एक Method, कई रूप (Real-Life Example: अलग-अलग GPS Devices, Same Command)”






Polymorphism OOP का सबसे स्मार्ट और मज़ेदार concept है।
इससे code इतना flexible बन जाता है कि:

  • एक ही function

  • अलग-अलग classes में

  • अलग-अलग तरीके से behave करता है

बिल्कुल real-life की तरह—
Mobile का एक ही “camera button”

  • फोटो लेता है

  • वीडियो रिकॉर्ड करता है

  • पोर्ट्रेट बनाता है
    Mode बदलते ही behavior बदल जाता है।

इसे ही कहते हैं: Polymorphism – Many Forms


🟦 1. Polymorphism क्या है? (सबसे आसान परिभाषा)

एक नाम — कई व्यवहार।
A single method behaves differently in different classes.

C# में polymorphism दो तरह का होता है:

  1. Compile-time → Method Overloading

  2. Run-time → Method Overriding

आज हम दोनों को practical + GPS example में समझेंगे।


🟢 2. Method Overloading (Compile-Time Polymorphism)

एक ही नाम की कई methods,
लेकिन parameters अलग।

Example:

void Send(string msg) { } void Send(string msg, string number) { } void Send(string msg, string number, int type) { }

Call करते समय compiler decide करता है कौन सा चलेगा।


🌟 Real-Life Example (Billing Software)

User discount देता है:

  • Discount(%)

  • Discount(amount)

  • Discount(coupon)

एक method → तीन forms.


🟣 3. Method Overriding (Run-Time Polymorphism)

Child class parent वाले method को override करती है।

Parent:

class GPSDevice { public virtual void ProcessCommand(string cmd) { Console.WriteLine("Processing command in base device"); } }

Child: G17

class G17 : GPSDevice { public override void ProcessCommand(string cmd) { Console.WriteLine("G17 command processed"); } }

Child: S15

class S15 : GPSDevice { public override void ProcessCommand(string cmd) { Console.WriteLine("S15 specific command processed"); } }

🔥 4. Real-Life Example: आपकी GPS Server की सबसे Important Concept

जब आपका server command जारी करता है:

  • "REBOOT"

  • "LOCATION"

  • "STOP"

  • "FIND VEHICLE"

तो हर device अलग तरीके से response देता है।
लेकिन आप server पर हमेशा यही call करते हो:

device.ProcessCommand("REBOOT");

Server को यह नहीं पता होता कि device:

  • G17 है

  • S15 है

  • A20 है

  • TR06 है

फिर भी सही method run होता है!
क्यों? → Polymorphism


🔧 How Server Uses 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 की ताकत।


🧠 5. Real-Life Use in GPS Tracking Systems

✔ Common command

Server कहता है:
“Give me battery”

✔ Device-specific response

  • G17 कहता है: “Battery=83%”

  • S15 कहता है: “B:82;Status=Normal”

  • LK209 कहता है: “BAT:8.2V”

लेकिन code:

device.GetBattery();

Inside each class:

public override void GetBattery() { … }

💼 6. Real-Life Use in Big Companies

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)


🔚 Conclusion

Polymorphism असल में आपके code को देता है:

✔ Flexibility
✔ Power
✔ Scalability
✔ Clean Architecture
✔ Future-proofing

GPS, Billing, School, E-commerce —
हर बड़े system में polymorphism foundation है।

Post a Comment

0 Comments

Translate

Close Menu