Inheritance သည် class တစ်ခုက အခြား class တစ်ခုရဲ့ fields/properties/methods များကို ဆက်ခံအသုံးပြုနိုင်တဲ့ feature ပါ။ Common behavior တွေကို base class ထဲမှာရေးထားပြီး derived class တွေက ပြန်သုံးနိုင်တာကြောင့် repeated code ကိုလျော့စေပါတယ်။
csharp
class Vehicle
{
public string Brand = "Ford";
public void Start()
{
Console.WriteLine("Vehicle started");
}
}
class Car : Vehicle
{
public string ModelName = "Mustang";
}
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
Console.WriteLine(myCar.Brand + " " + myCar.ModelName);
myCar.Start();
}
}အလုပ်လုပ်ပုံ
Vehicleက base class ဖြစ်ပါတယ်။Car : Vehicleဆိုတာ Car က Vehicle ကိုဆက်ခံတယ်လို့ဆိုလိုပါတယ်။- Car object မှာ
Brandနဲ့Start()ကို Vehicle ကနေ ရရှိပါတယ်။
You should see
Ford Mustang Vehicle started