Overriding vs Overloading - CSharp Custom Type

CSharp examples for Custom Type:virtual

Description

Overriding vs Overloading

Demo Code

using System;//from ww  w  .j  a v a  2 s  .c o m
public class Animal
{
   public virtual void Move(short distance)
   {
      Console.WriteLine("Animal.Move(short). Distance: {0}", distance);
   }
   public virtual void Move(double distance, string direction)
   {
      Console.WriteLine("Animal.Move(double, string). Distance: {0} Direction: {1}",
      distance, direction);
   }
}
public class Dog : Animal
{
   public void Move(int distance)
   {
      Console.WriteLine("Dog.Move(int). Distance: {0}", distance);
   }
   public void Move(byte distance)
   {
      Console.WriteLine("Dog.Move(byte). Distance: {0}", distance);
   }
}
class Tester
{
   public static void Main()
   {
      Dog fido = new Dog();
      int myInt = 45;
      short myShort = 25;
      double myDouble = 5.6;
      fido.Move(myInt);
      fido.Move(myDouble, "North");
      fido.Move(myShort);
   }
}

Result


Related Tutorials