c sharp

What is the Override Modifier in C#

In circumstances involving object-oriented programming, we frequently need to offer our own implementation of a method that is already specified in the underlying class or interface. The C# override modifier is useful in this situation. The override modifier allows us to modify the behavior of an inherited method or property. This article will discuss the use of the override modifier in C# and how it can be used to modify the behavior of inherited members.

What is the Override Modifier?

In C#, you can create a new implementation of a method or property that is already declared in the base class or interface by using the override modifier. It enables us to change a derived class’s inherited member’s behavior. When we override a method or property, we can provide our own implementation for that member, which will replace the implementation provided by the base class or interface.

How to Use the Override Modifier?

To use the override modifier, we need to follow the steps below:

  • Inherit the base class or interface that contains the method or property we want to override.
  • In the derived class, declare a new method or property with the same name and signature.
  • Use the override keyword before the method or property declaration to indicate that we are overriding the inherited member.

To help you comprehend this in more depth, here is an example.

Consider a base class called “Vehicle” that has the function “Start” in its name. The Start method simply prints a message to the console saying that the vehicle has started. Now, let’s say I want to create a new class called “Car” that inherits from the Vehicle class but provides its own implementation for the Start method. Here’s how we can achieve this using the override modifier:

using System;

class Vehicle
{
    public virtual void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

class Car : Vehicle
{
    public override void Start()
    {
        Console.WriteLine("Car started.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Vehicle vehicle = new Vehicle();
        vehicle.Start(); // Output: Vehicle started.

        Car car = new Car();
        car.Start(); // Output: Car started.
    }
}

Here I have created a new class called “Car” that inherits from the “Vehicle” class. I have also declared a new method called “Start” in the “Car” class, with the same name and signature as the method in the “Vehicle” class. I have used the override modifier to indicate that we are overriding the inherited method.

In the Main, I have created instances of both the Vehicle and Car classes and called the Start method on them. When I call the Start method on the Vehicle object, it prints “Vehicle started” to the console. When I call the Start method on the Car object, it prints “Car started” to the console. This demonstrates that we have successfully overridden the Start method in the derived class:

Conclusion

The override modifier in C# is a powerful tool that allows us to modify the behavior of an inherited member. It is used to provide our own implementation for a method or property that is already defined in the base class or interface. We have discussed how to use the override modifier in C# and demonstrated its use with a simple example. By using the override modifier, we can create more specialized classes that inherit from a base class but provide their own unique behavior.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.