การสืบทอด Class จากแม่ สู่ ลูก parent : child
ถึงแม้เราจะสร้าง object ที่คลาสลูก แต่สามารถเรียกใช้ method จาก class แม่ได้ เพราะทั้ง Class Car สือบทอด Vehicle อยู่นั้นเอง
class Vehicle // base class (parent) { public string brand = "Ford"; // Vehicle field public void honk() // Vehicle method { Console.WriteLine("Peed!"); }}class Car : Vehicle // derived class (child) คลาสลูกสืบทอดคลาสแม่ Vehicle{ public string modelName = "Mustang"; // Car field}class Program{ static void Main(string[] args) { // Create a myCar object Car myCar = new Car(); // Call the honk() method (From the Vehicle class) on the myCar object myCar.honk(); // Console.WriteLine(myCar.brand + " " + myCar.modelName); }} // out put // Peed! // Ford Mustang
cr. www.w3schools.com