Introduction
In C#, a virtual method is a method that can be overridden in a derived class. When a method is declared as virtual in a base class, it allows a derived class to provide its own implementation of the method.
To declare a method as virtual in C#, the "virtual" keyword is used in the method declaration in the base class. For example.
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}
In the derived class, the method can be overridden by using the "override" keyword in the method declaration. For example.
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat meows");
}
}
When an instance of the derived class is created and the overridden method is called, the implementation in the derived class will be executed instead of the implementation in the base class.
Using virtual methods can be useful in situations where you want to provide a base implementation in a base class, but allow derived classes to modify or extend the behavior of that method.
Virtual Method in C#
- By default, methods are non-virtual. We can't override a non-virtual method.
- We can't use the virtual modifier with the static, abstract, private, or override modifiers.
Difference between virtual and non-virtual methods
We have two classes; one is a "Vehicle" class, and another is a "Cart" class. The "Vehicle" class is the base class that has two methods; one is a virtual method, "Speed()," and another is a non-virtual method "Average()". So the base class virtual method "Speed()" is overrioverriddene sub class. We have one more class, "Program" (the execution class), that has an entry point where we create an instance of sub class "Cart" and that instance is assigned to the base class "Vehicle" type. When we call virtual and non-virtual methods by both class's instaninstancesaccording to the run type the i, instance virtual method implementation is invoked; in other words, both class's instances invoke the subclass override method, and the non-virtual method invoked is determined based on the instance of the class.
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 = new Car(distance,hour,fuel);
Vehicle objVeh = objCar;
objCar.Average();
objVeh.Average();
objCar.Speed();
objVeh.Speed();
Console.Read();
}
}
}


Niloy NiloyPosted Apr 30, 2023, 5:57 AM
You are such a stupid Read what you have shared
RATANJEET SINGHPosted Jun 11, 2021, 7:52 AM
Nicely described
Hemalatha SunnapuPosted Nov 1, 2018, 12:16 AM
Can we call a virtual method with a value type
LEFOPHANA GODFREYPosted Apr 10, 2018, 4:03 AM
I need to understand the difference between virtual function and abstract function and why it is important to use them.in which situation we need to use them
suraj panjwaniPosted Jun 28, 2017, 2:56 AM
Hi , the example given in this article is wrong. You have declared Vehicle as base class. In that base class , you have declared a non-Virtual method Average. Then you have created Derived class Car from baseclass Vehicle. In that derived class , you are again using Average method. This Average method is declared as non-virtual method is the base-class , so we can't use this non-virtual Average method in the derived class . When you try to use this , it will throw an compile time error saying Car.Average() hides inherited member 'Vehicle.Average()' Use the new Keyword if hiding was intended . Please anyone correct me if i am wrong.
SubashPosted Mar 14, 2017, 8:59 PM
Nice this is useful share for me thank you
Pravin KolhePosted Jul 24, 2015, 9:24 AM
How making a method 'Virtual' useful in Mock's Setup method?
mohamed ragabPosted Jan 11, 2013, 8:33 PM
nice