Call Derived Class Constructors - CSharp Custom Type

CSharp examples for Custom Type:Constructor

Description

Call Derived Class Constructors

Demo Code

using System;/*  w  ww .  java2 s .c  o  m*/
class Car
{
   private string brandName;
   public Car(string initialBrandName)
   {
      brandName = initialBrandName;
   }
   public Car()
   {
      brandName = "unknown";
   }
   public string BrandName
   {
      get
      {
         return brandName;
      }
      set
      {
         brandName = value;
      }
   }
}
class RacingCar : Car
{
   private string engineName;
   public RacingCar() : base()
   {
      engineName = "unknown";
   }
   public RacingCar(string initialBrandName, string e) : base(initialBrandName)
   {
      engineName = e;
   }
   public string OnBoardCameraName
   {
      get
      {
         return engineName;
      }
      set
      {
         engineName = value;
      }
   }
}
class CarTester
{
   public static void Main()
   {
      Car myNoNameCar = new Car();
      Car myCar = new Car("Volvo");
      RacingCar myNoNameRacingCar = new RacingCar();
      RacingCar yourRacingCar = new RacingCar("Ferrari", "Sony");
      Console.WriteLine("The name of myNoNameCar: " + myNoNameCar.BrandName);
      Console.WriteLine("The name of myCar: " + myCar.BrandName);
      Console.WriteLine("The name of myNoNameRacingCar: " + myNoNameRacingCar.BrandName);
      Console.WriteLine("The camera name of myNoNameRacingCar: " + myNoNameRacingCar.OnBoardCameraName);
      Console.WriteLine("The name of yourRacingCar: " + yourRacingCar.BrandName);
      Console.WriteLine("The camera name of yourRacingCar: " + yourRacingCar.OnBoardCameraName);
   }
}

Result


Related Tutorials