Inheritance मतलब Parent से Child Classes को features मिलना।
C# और OOP में यह सबसे powerful concept है।
इस blog में हम inheritance को बेहद आसान तरीके + आपके real GPS Device project के example से समझेंगे।
Inheritance =
एक class दूसरे class की properties + methods inherit कर लेती है।
Parent Class → Base
Child Class → Derived
class Animal
{
public void Eat() { }
}
class Dog : Animal
{
public void Bark() { }
}
Dog को Eat() अपने आप मिल गया।
✔ Code repeat नहीं होता
✔ Common logic एक जगह रहता है
✔ Child classes clean बनती हैं
✔ Maintain करना आसान हो जाता है
Real-world में यही होता है—
एक company एक base design बनाती है, और multiple models उसी design से निकलते हैं।
आपके पास कई devices हैं:
G17
S15
A20
LK209
TR06
हर device का data structure थोड़ा अलग है,
लेकिन common fields same:
IMEI
Latitude
Longitude
Battery
LastUpdated
DeviceType
Status
ये सब एक 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 void UpdateLocation(double lat, double lon)
{
Latitude = lat;
Longitude = lon;
LastUpdated = DateTime.Now;
}
}
यह आपका common parent है।
G17 में कुछ extra fields होते हैं:
ACC Status
GPS Validity
GSM Signal
Speed
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 हो गया।
S15 में extra fields:
Strong Magnets
Sleep Mode
Voice Monitor
Shock Alert
class S15 : GPSDevice
{
public bool ShockAlert { get; set; }
public bool SleepMode { get; set; }
}
Same parent, अलग features।
Without inheritance →
हर device के लिए code पूरा नया बनाना पड़ेगा।
With inheritance →
Common fields parent में
Unique fields child में
Packet parsing आसान
Database model clean
Code readable
Bugs कम
Multi-device support easy
अगर parent में method है →
child को अपने आप मिल जाता है।
GPSDevice d = new G17();
d.UpdateLocation(26.85, 80.94);
Even though object is G17 →
Method parent से मिला।
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
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 बनता है।
Inheritance की वजह से:
आपका GPS Server scalable बनता है
Multi-device support easy
Code clean
Parent-child architecture strong
Maintenance super easy
Inheritance असल में software की family tree जैसा है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav