Virtual Method

.Virtual Method


Virtual Method Program Output

.Virtual Method

"A C# program with a virtual base-class method. In one derived class the virtual base-class method is used. In a second derived class the virtual base-class method is overridden with a new implementation."

This program defines a base class (Shape) with virtual method (Area). The base class contains a default constructor which uses literals to set the value of two fields. A second constructor is defined to set the field values to the value of passed parameters. Two classes are derived from the base class. In one derived class (Rectangle) the base method is used. In another derived class (Circle) the base method is overridden with a new implementation . The following C# features are used in the sample code:

  1. virtual - A modifer used with methods, properties, indexers, or event declarations to allow it to be overridden in a derived class.
  2. Virtual Methods - The implementation of a virtual method can be superseded (overridden) by derived classes.
  3. override - A modifier required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
  4. Constructors - Constructors are methods which have the same name as the class or struct, and they usually initialize the data members of the new object.
  5. base - Keyword is used to access members of the base class from within a derived class.
  6. Standard Numeric Format Strings - Used to format common numeric types.

    Virtual Method

    namespace virtualMethod
    {
        class Shape
        {
            protected double height, width;

            // Default constructor
            public Shape()
            {
                height = 10;
                width = 10;
            }

            // Constructor with two arguments
            public Shape(double height, double width)
            {
                this.height = height;
                this.width = width;
            }

            public virtual double Area()
            {
                return width * height;
            }
        }

        class Rectangle : Shape
        {
            public Rectangle()
                : base()
            {
            }
            public Rectangle(double width, double height)
                : base(width, height)
            {
            }
        }

        class Circle : Shape
        {
            public Circle()
                : base()
            {
            }
            public Circle(double width, double height)
                : base(width, height)
            {
            }

            public override double Area()
            {
                return System.Math.PI * height * height;
            }
        }

        class Program
        {
            static void Header()
            {
                System.Console.WriteLine("*********************************************************");
                System.Console.WriteLine("*** Two Different Implementations of a Virtual Method ***");
                System.Console.WriteLine("*********************************************************");
            }

            static void Main()
            {
                Program.Header();

                System.Console.WriteLine("\n------------ Rectangle Area Implementation  ------------");
                // Default Rectangle
                Rectangle myRectangle = new Rectangle();
                System.Console.WriteLine("Default rectangle area is: {0:F2}", myRectangle.Area());

                // Rectangle with dimensions
                Rectangle myRectangle2 = new Rectangle(5, 5);
                System.Console.WriteLine("Rectangle 5x5 area is: {0:F2}", myRectangle2.Area());

                System.Console.WriteLine();

                System.Console.WriteLine("\n------------ Circle Area Implementation  ------------");
                // Default Circle
                Circle myCircle = new Circle();
                System.Console.WriteLine("Default Circle area is: {0:F2}", myCircle.Area());

                // Circle with dimensions
                Circle myCircle2 = new Circle(5, 5);
                System.Console.WriteLine("Circle 5 area is: {0:F2}\n", myCircle2.Area());
            }
        }
    }