Illustrates member accessibility in subclass : Derived Class « Class « C# / CSharp Tutorial






using System;

public class Car
{
  private   string make;
  protected string model;

  public Car(string make, string model)
  {
    this.make = make;
    this.model = model;
  }

  public virtual void Start()
  {
    startCar();
    System.Console.WriteLine("Vehicle started");
  }

  private void startCar()
  {
    System.Console.WriteLine("Turning starter motor...");
  }

}

public class MyFirstCar : Car
{
  public MyFirstCar(string make, string model) : base(make, model)
  {
  }

  public override void Start()
  {
    Console.WriteLine("Starting " + model);  
    base.Start();  
  }
}

class MainClass
{
  public static void Main()
  {
    MyFirstCar myCar = new MyFirstCar("Toyota", "MR2");
    myCar.Start();
  }
}
Starting MR2
Turning starter motor...
Vehicle started








7.21.Derived Class
7.21.1.A base class reference can refer to a derived class object
7.21.2.Illustrates member hiding
7.21.3.Illustrates member accessibility in subclass
7.21.4.base and derived classes