📘 C# Blog Series – Blog 31 **“C# Inheritance — Code को Reuse करके Smart Architecture कैसे बनाते हैं?

📘 C# Blog Series – Blog 31

**“C# Inheritance — Code को Reuse करके Smart Architecture कैसे बनाते हैं?






(Real-Life Example: G17, S15, A20 GPS Devices के लिए Base Class)”**

C# में Inheritance (विरासत) OOP का सबसे powerful concept है।
इससे आप code reuse, clean architecture, polymorphism और
scalable design बना सकते हो।

GPS Tracking Server जैसे systems में inheritance बहुत काम आता है क्योंकि
हर GPS device model (G17, S15, A20…) का logic 60-70% एक जैसा होता है
और बाकी 30% अलग होता है।

आज inheritance को super-simple तरीके से + आपके GPS server के real examples के साथ समझते हैं।


🟦 1. Inheritance क्या है? (Simple Definition)

Inheritance =
एक class दूसरी class की properties + methods inherit कर लेती है।

Example:

class Vehicle { } class Car : Vehicle { }

Car ने Vehicle की सारी चीज़ें पा लीं।


🟢 2. Base Class vs Derived Class

  • Base class = Parent

  • Derived class = Child

Example:

class GPSDevice // Base { public string IMEI { get; set; } } class G17Device : GPSDevice // Derived { }

अब G17 के पास IMEI automatically है।


3. Real-Life GPS Example: एक Base Class बनाते हैं

हर GPS device model में common चीजें होती हैं:

  • IMEI

  • Latitude

  • Longitude

  • Speed

  • Battery

  • LastUpdated

तो base class:

class GPSDevice { public string IMEI { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } public int Battery { get; set; } public DateTime LastUpdated { get; set; } public virtual void ParsePacket(string pkt) { Console.WriteLine("Parsing base packet"); } }

4. अब हर device model अपनी class inherit कर सकता है

G17:

class G17Device : GPSDevice { public override void ParsePacket(string pkt) { Console.WriteLine("G17 parsing logic"); } }

S15:

class S15Device : GPSDevice { public override void ParsePacket(string pkt) { Console.WriteLine("S15 parsing logic"); } }

A20:

class A20Device : GPSDevice { public override void ParsePacket(string pkt) { Console.WriteLine("A20 parsing logic"); } }

अब हर device type का parsing अलग होगा
लेकिन base properties + behavior same रहेगा।


🌟 5. Polymorphism — सबसे बड़ी inheritance superpower

You can do:

GPSDevice dev = new G17Device(); dev.ParsePacket(pkt);

Even though dev is G17 internally →
correct parser run होगा।

GPS server में यही concept packet parsing को super flexible बनाता है।


🟠 6. Real GPS Use-Case: Multi-Model Device Handling

List of devices:

List<GPSDevice> devices = new(); devices.Add(new G17Device()); devices.Add(new S15Device()); devices.Add(new A20Device());

Process all:

foreach (var d in devices) { d.ParsePacket(packet); }

हर device अपना logic खुद execute करेगा।
यानी polymorphism in action!


🔵 7. Real GPS Example: Base Class में Common Methods

Base में:

public void UpdateLocation(double lat, double lon) { Latitude = lat; Longitude = lon; LastUpdated = DateTime.Now; }

अब किसी derived device में बस call करो:

UpdateLocation(26.85, 80.94);

Same function सभी models में work करेगा → massive code reuse.


🟣 8. Constructor Inheritance

class GPSDevice { public GPSDevice(string imei) { IMEI = imei; } } class G17Device : GPSDevice { public G17Device(string imei) : base(imei) { } }

🔥 9. Multi-Level Inheritance (Advanced)

GPSDevice → AdvancedDevice → G17Device

Useful where:

  • Basic properties (base class)

  • Advanced sensors logic (next level)

  • Specific model logic (third level)


📌 10. Sealed Class (Inheritance रोकने के लिए)

अगर आप नहीं चाहते कोई class inherit करे:

sealed class GPSDevice { }

🧠 11. कब Inheritance use नहीं करना चाहिए?

❌ अगर classes अलग-अलग हैं
❌ अगर multiple behaviors हैं
✔ ऐसे cases में Interfaces better हैं (हम आगे सीखेंगे)


🔚 Conclusion

C# Inheritance आपके system को बनाता है:

✔ Clean
✔ Reusable
✔ Scalable
✔ Polymorphic
✔ Maintainable
✔ GPS-friendly architecture

GPS tracking servers में यह सबसे useful OOP feature है क्योंकि
हर model का logic अलग + common दोनों होता है।

Post a Comment

0 Comments

Translate

Close Menu