What is Virtual Method

In object-oriented programming, a virtual method is a method that can be overridden in derived classes. It allows subclasses to provide their own implementation of the method while still maintaining the same method signature as the base class. Virtual methods play a crucial role in achieving polymorphism and facilitating dynamic method dispatch. Here's a detailed explanation with examples:

Virtual Method

To define a virtual method in C#, you use the virtual keyword in the method declaration within the base class. The derived classes can then override this method with their own implementation.

public class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape."); } } public class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle."); } }

In the above example, the Draw() method in the Shape class is declared as virtual. This allows derived classes, such as Circle, to override and provide their own implementation of the method using the override keyword.

Polymorphism and Dynamic Method Dispatch

What is Virtual Method C# vb.net asp.net

Virtual methods enable polymorphism, which means that a method call on a base class reference can be dynamically dispatched to the appropriate derived class method at runtime based on the actual object type.

Shape shape = new Circle(); shape.Draw(); // Calls the overridden Draw() method in the Circle class

In the above example, a Circle object is instantiated, but it is assigned to a Shape reference variable. When the Draw() method is called on the shape object, the overridden Draw() method in the Circle class is executed. This dynamic method dispatch ensures that the appropriate method implementation is invoked based on the actual object type.

Non-Virtual Methods

By default, methods in C# are non-virtual, meaning they cannot be overridden in derived classes unless explicitly marked as virtual. Non-virtual methods are resolved at compile-time, and the method called depends on the reference type rather than the actual object type.

public class Vehicle { public void StartEngine() { Console.WriteLine("Engine started."); } } public class Car : Vehicle { public void StartEngine() { Console.WriteLine("Car engine started."); } }
What is Virtual functions C# vb.net asp.net

In the above example, the StartEngine() method in the Vehicle class is not marked as virtual. If you have a reference of type Vehicle pointing to a Car object and call StartEngine(), the method in the Vehicle class will be executed, not the one in the Car class.

Vehicle vehicle = new Car(); vehicle.StartEngine(); // Calls the StartEngine() method in the Vehicle class

To enable method overriding and dynamic dispatch, the base class method should be declared as virtual, and the derived class should use the override keyword to provide its own implementation.

Conclusion

Virtual methods are essential for achieving polymorphism and enabling flexible behavior in object-oriented programming. They allow derived classes to override base class methods and provide their own specialized implementation, promoting code extensibility and modularity.