override virtual method - CSharp Custom Type

CSharp examples for Custom Type:virtual

Description

override virtual method

Demo Code

using System;/*from  w  w  w  .j  a  v a2  s .  c  om*/
class Car
{
   private uint odometer = 0;
   protected uint Odometer
   {
      set
      {
         odometer = value;
      }
      get
      {
         return odometer;
      }
   }
   public virtual void MoveForward()
   {
      Console.Write("Moving forward... ");
      odometer += 1;
      Console.WriteLine("Odometer reading: {0}", odometer);
   }
}
class RacingCar : Car
{
   public override void MoveForward()
   {
      Console.Write("Moving dangerously fast forward... ");
      Odometer += 30;
      Console.WriteLine("Odometer in racing car: {0}", Odometer);
   }
}
class FamilyCar : Car
{
   public override void MoveForward()
   {
      Console.Write("Moving slowly but safely forward...");
      Odometer += 5;
      Console.WriteLine("Odometer in family car: {0}", Odometer);
   }
}
class CarTester
{
   public static void Main()
   {
      RacingCar myRacingCar = new RacingCar();
      FamilyCar myFamilyCar = new FamilyCar();
      myRacingCar.MoveForward();
      myFamilyCar.MoveForward();
   }
}

Result


Related Tutorials