Method overriding and virtual/override keywords

In object-oriented programming, the concept of method overriding plays a significant role in polymorphism and inheritance. It allows a derived class to provide its own implementation of a method that is already defined in its base class. This enables us to modify the behavior of inherited methods, tailor-made to suit the specific requirements of the derived class.

C# provides a mechanism for method overriding through the use of the virtual and override keywords. Let's explore how these keywords work and how they enable method overriding.

The virtual keyword

To declare a method as overridable in the base class, we need to mark it with the virtual keyword. This indicates that the method can be overridden by a derived class. Here's an example:

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

In the above code snippet, the Draw method in the Shape class is marked as virtual, making it eligible for overriding.

The override keyword

To provide a specific implementation for the overridden method in a derived class, we use the override keyword. The overridden method in the derived class must match the signature of the base class method. Let's see an example:

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

In the Circle class, we override the Draw method from the Shape class and provide our own implementation to draw a circle.

How method overriding works

When we create an instance of a derived class and call an overriden method, the runtime system determines the actual type of the object and invokes the appropriate implementation. This mechanism is known as dynamic method dispatch. Consider the following code snippet:

Shape shape = new Circle();
shape.Draw(); // Output: Drawing a circle.

Even though the variable shape is declared as Shape, at runtime, the Draw method of the Circle class gets executed. This is because the Draw method is marked as virtual in the base class and overridden in the derived Circle class.

Abstract and sealed classes

It is worth noting that abstract classes cannot be marked as virtual. Abstract methods are, by default, designed for overriding. For methods in abstract classes, there is no need to use the virtual keyword, but the override keyword is required when defining the implementation in the derived class.

On the other hand, if we want to prevent a method from being further overridden in a derived class, we can use the sealed keyword. Once a method is marked as sealed, it becomes the final implementation and cannot be overridden anymore.

Conclusion

Method overriding and the virtual and override keywords are essential features of the C# programming language. They allow us to extend and customize the behavior of an inherited method in a derived class. By understanding and utilizing these keywords effectively, we can leverage the power of polymorphism and inheritance in our code.


noob to master © copyleft