Demonstrates the use of a virtual method to override a base class method

image_pdfimage_print

   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Virtual.cs -- Demonstrates the use of a virtual method to override
//               a base class method.
//
//               Compile this program with the following command line:
//                   C:>csc Virtual.cs
namespace nsVirtual
{
    using System;
    
    public class VirtualclsMain
    {
        static public void Main ()
        {
            clsBase Base = new clsBase();
            clsFirst First = new clsFirst();
            clsSecond Second = new clsSecond();
            Base.Show();
            First.Show();
            Second.Show ();
        }
    }
    class clsBase
    {
        public void Show ()
        {
            Describe ();
        }
        virtual protected void Describe ()
        {
            Console.WriteLine ("Called the base class Describe() method");
        }
    }
    class clsFirst : clsBase
    {
        override protected void Describe ()
        {
            Console.WriteLine ("Called the derived class Describe() method");
        }
    }
    class clsSecond : clsBase
    {
    }
}