Lesson 7 It’s time to start learning inheritance. Agenda Inheritance References Inheritance Base class: class Car { protected int carId; public Car(int id) { carId = id; } public string Color { get; set; } public int Year { get; set; } public string A() { return "Car"; } //The virtual keyword is used to allow a member for it to be overridden in a derived class public virtual string GetLogo() { return "Unrecognized logo"; } protected void Foo() { Console.WriteLine("Foo1"); } public virtual void PrintDetails() { Console.WriteLine("---"); Console.WriteLine($"Car ID is {carId}"); Console.WriteLine("---"); } Derived class: class Audi : Car { public Audi(int id) : base(id) { //ToDo: Try to uncomment that line and change carId member in Car class to be private. //carId = 0 } public string Number { get; set; } public override string GetLogo() { return "4 rings"; } public override void PrintDetails() { Console.WriteLine("---"); Console.WriteLine($"Car ID is {carId}"); Console.WriteLine("Manufactorer Audi"); Console.WriteLine("---"); } } Some other site accepting base class object as an argument, but derived class object can be passed as well. class Shop { public int GetPrice(Car car) { var price = 500; if (car.Color == "Red") { price += 1000; } if (car.Year > 2005) { price += 5000; } return price; } } Shop shop = new Shop(); Car car = new Car(123) { Year = 2002 }; Audi audi = new Audi(465) { Color = "Red", Year = 2010 }; Car audiCar = new Audi(978) { Year = 2010, Number = "AA3454CE" }; WriteLine("*** Logos ***"); WriteLine(car.GetLogo()); WriteLine(audi.GetLogo()); WriteLine(audiCar.GetLogo()); WriteLine("*** Prices ***"); WriteLine(shop.GetPrice(audi));// Audi object can be passed when Car object is expected. WriteLine(shop.GetPrice(audiCar)); WriteLine("*** Details ***"); audi.PrintDetails(); audiCar.PrintDetails(); References Inheritance 1 Inheritance 2 Inheritance 3 Inheritance 4 Protected access modifier