Virtual and overload : Override Virtual « Class Interface « C# / C Sharp






Virtual and overload

Virtual and overload
/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class MyMainClass12 {
    static void Main(string[] args)
    {
      //The function called is based
      //upon the type called by new.
      B MyB = new C();
      D MyD = new D();

      MyB.Display();    //Calls C Display
      MyD.Display();    //Calls D Display
    }
  }
  public interface A
  {
    void Display();
  }
  class B: A
  {
    public virtual void Display()
    {
      Console.WriteLine("Class B's Display Method");
    }
  }
  class C: B
  {
    public override void Display()
    {
      Console.WriteLine("Class C's Display Method");
    }
  }
  class D: C
  {
    public override void Display()
    {
      Console.WriteLine("Class D's Display Method");
    }
  }

}

           
       








Related examples in the same category

1.Polymorphism
2.Virtual keyword can be used to start a new inheritance ladder
3.Class hierarchy: override and virtual
4.Use virtual methods and polymorphismUse virtual methods and polymorphism
5.Demonstrate a virtual methodDemonstrate a virtual method
6.When a virtual method is not overridden, the base class method is usedWhen a virtual method is not overridden, 
   the base class method is used
7.illustrates polymorphismillustrates polymorphism
8.Demonstrates the use of a virtual method to override a base class methodDemonstrates the use of a virtual method to override a base class method
9.Demonstrates the use of a virtual property to override a base class propertyDemonstrates the use of a virtual property to override a base class property
10.Method override 3Method override 3
11.Test Polymorphism Virtual Functions