Demonstrates the use of a virtual method to override a base class method : Override Virtual « Class Interface « C# / C Sharp






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

Demonstrates the use of a virtual method to override a base class method
/*
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
    {
    }
}


           
       








Related examples in the same category

1.Polymorphism
2.Virtual keyword can be used to start a new inheritance ladder
3.Virtual and overloadVirtual and overload
4.Class hierarchy: override and virtual
5.Use virtual methods and polymorphismUse virtual methods and polymorphism
6.Demonstrate a virtual methodDemonstrate a virtual method
7.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
8.illustrates polymorphismillustrates polymorphism
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