Virtual Methods in C#

 

A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived the class. It is used when a method's basic functionality is the same but sometimes more functionality is needed in the derived class. A virtual method is created in the base class that can be overriden in the derived class. We create a virtual method in the base class using the virtual keyword and that method is overriden in the derived class using the override keyword.

 

When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. The overriding method also provides more than one form for a method. Hence it is also an example for polymorphism.

 

When a method is declared as a virtual method in a base class and that method has the same definition in a derived class then there is no need to override it in the derived class. But when a virtual method has a different definition in the base class and the derived class then there is a need to override it in the derived class.

 

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.

 

Virtual Method

 

 1         By default, methods are non-virtual. We can't override a non-virtual method.

 2         We can't use the virtual modifier with the static, abstract, private or override modifiers.

 

Example below will explain you in detail..

 

using System;

namespace VirtualExample

{

    class Vehicle

    {

        public double distance = 0.0;

        public double hour = 0.0;

        public double fuel = 0.0;

 

        public Vehicle(double distance, double hour, double fuel)

        {

            this.distance = distance;

            this.hour = hour;

            this.fuel = fuel;

        }

        public void Average()

        {

            double average = 0.0;

            average = distance / fuel;

            Console.WriteLine("Vehicle Average is {0:0.00}", average);

        }

        public virtual void Speed()

        {

            double speed = 0.0;

            speed = distance / hour;

            Console.WriteLine("Vehicle Speed is {0:0.00}", speed);

        }

    }

    class Car : Vehicle

    {

        public Car(double distance, double hour, double fuel)

            : base(distance, hour, fuel)

        {

        }

        public void Average()

        {

            double average = 0.0;

            average = distance / fuel;

            Console.WriteLine("Car Average is {0:0.00}", average);

        }

        public override void Speed()

        {

            double speed = 0.0;

            speed = distance / hour;

 

            Console.WriteLine("Car Speed is {0:0.00}", speed);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            double distance, hour, fuel = 0.0;

            Console.WriteLine("Enter the Distance");

            distance = Double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Hours");

            hour = Double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Fuel");

            fuel = Double.Parse(Console.ReadLine());

            Car objCar = newCar(distance, hour, fuel);

            Vehicle objVeh = objCar;

            objCar.Average();

            objVeh.Average();

            objCar.Speed();

            objVeh.Speed();

            Console.Read();

        }

    }

}