📘 C# Blog Series – Blog 9 “C# Inheritance Deep Dive — Real-Life Example: GPS Device Models (G17, S15, Others) एक Common Parent कैसे Share करते हैं?”

📘 C# Blog Series – Blog 9

“C# Inheritance Deep Dive — Real-Life Example: GPS Device Models (G17, S15, Others) एक Common Parent कैसे Share करते हैं?”






Inheritance मतलब Parent से Child Classes को features मिलना
C# और OOP में यह सबसे powerful concept है।

इस blog में हम inheritance को बेहद आसान तरीके + आपके real GPS Device project के example से समझेंगे।


🟦 1. Inheritance क्या होता है?

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

  • Parent Class → Base

  • Child Class → Derived

Simple Example:

class Animal { public void Eat() { } } class Dog : Animal { public void Bark() { } }

Dog को Eat() अपने आप मिल गया।


🟢 2. Inheritance क्यों जरूरी है?

✔ Code repeat नहीं होता
✔ Common logic एक जगह रहता है
✔ Child classes clean बनती हैं
✔ Maintain करना आसान हो जाता है

Real-world में यही होता है—
एक company एक base design बनाती है, और multiple models उसी design से निकलते हैं।


3. Real-Life Example: GPS Tracking System (Your actual project!)

आपके पास कई devices हैं:

  • G17

  • S15

  • A20

  • LK209

  • TR06

हर device का data structure थोड़ा अलग है,
लेकिन common fields same:

  • IMEI

  • Latitude

  • Longitude

  • Battery

  • LastUpdated

  • DeviceType

  • Status

ये सब एक Base Class से आते हैं।


🧱 4. Base Class — GPSDevice

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 void UpdateLocation(double lat, double lon) { Latitude = lat; Longitude = lon; LastUpdated = DateTime.Now; } }

यह आपका common parent है।


🚀 5. Child Class Example: G17 Device

G17 में कुछ extra fields होते हैं:

  • ACC Status

  • GPS Validity

  • GSM Signal

  • Speed

Derived Class:

class G17 : GPSDevice { public bool ACC { get; set; } public int Speed { get; set; } public int Signal { get; set; } }

अब G17 को मिल गया:

✔ Latitude
✔ Longitude
✔ Battery
✔ LastUpdated

Parent से inherit हो गया।


🚀 6. Child Class Example: S15 Device (Your S15 Tracker)

S15 में extra fields:

  • Strong Magnets

  • Sleep Mode

  • Voice Monitor

  • Shock Alert

Derived:

class S15 : GPSDevice { public bool ShockAlert { get; set; } public bool SleepMode { get; set; } }

Same parent, अलग features।


🌟 7. Real-Life Benefit — आपके पूरे GPS Server का Architecture Clean हो जाता है

Without inheritance →
हर device के लिए code पूरा नया बनाना पड़ेगा।

With inheritance →

  • Common fields parent में

  • Unique fields child में

  • Packet parsing आसान

  • Database model clean

  • Code readable

  • Bugs कम

  • Multi-device support easy


🟣 8. Method Inheritance भी possible है

अगर parent में method है →
child को अपने आप मिल जाता है।

Example:

GPSDevice d = new G17(); d.UpdateLocation(26.85, 80.94);

Even though object is G17 →
Method parent से मिला।


🟠 9. Override — जब child को method अलग चाहिए

Parent method:

class GPSDevice { public virtual void PrintInfo() { Console.WriteLine("Base GPS Device"); } }

Child override:

class G17 : GPSDevice { public override void PrintInfo() { Console.WriteLine("G17 GPS Device"); } }

Use:

GPSDevice device = new G17(); device.PrintInfo(); // Output: G17 GPS Device

🔥 10. Real Example: Device Packet Parsing

Common parsing:

class GPSDevice { public virtual void ParsePacket(string pkt) { // Base parsing } }

But device-specific parsing:

class G17 : GPSDevice { public override void ParsePacket(string pkt) { // G17 specific packet logic } }

इसी से multi-brand GPS server बनता है।


🔚 Conclusion

Inheritance की वजह से:

  • आपका GPS Server scalable बनता है

  • Multi-device support easy

  • Code clean

  • Parent-child architecture strong

  • Maintenance super easy

Inheritance असल में software की family tree जैसा है।

Post a Comment

0 Comments

Translate

Close Menu