0
0
CsharpHow-ToBeginner · 4 min read

How to Use Override Keyword in C# - Simple Guide

In C#, the override keyword is used in a derived class to provide a new implementation of a method declared as virtual or abstract in the base class. It allows you to change or extend the behavior of inherited methods by replacing the base version with your own.
📐

Syntax

The override keyword is placed before the method name in the derived class. The base class method must be marked with virtual, abstract, or override to allow overriding.

  • virtual: Marks a method in the base class as overridable.
  • override: Replaces the base class method in the derived class.
csharp
class BaseClass
{
    public virtual void ShowMessage()
    {
        Console.WriteLine("Hello from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public override void ShowMessage()
    {
        Console.WriteLine("Hello from DerivedClass");
    }
}
💻

Example

This example shows a base class with a virtual method and a derived class that overrides it to change the message printed.

csharp
using System;

class BaseClass
{
    public virtual void ShowMessage()
    {
        Console.WriteLine("Hello from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public override void ShowMessage()
    {
        Console.WriteLine("Hello from DerivedClass");
    }
}

class Program
{
    static void Main()
    {
        BaseClass baseObj = new BaseClass();
        baseObj.ShowMessage();

        DerivedClass derivedObj = new DerivedClass();
        derivedObj.ShowMessage();

        BaseClass polymorphicObj = new DerivedClass();
        polymorphicObj.ShowMessage();
    }
}
Output
Hello from BaseClass Hello from DerivedClass Hello from DerivedClass
⚠️

Common Pitfalls

Common mistakes when using override include:

  • Trying to override a method that is not marked virtual, abstract, or already override in the base class.
  • Forgetting to use override keyword in the derived class, which causes a new method to hide the base method instead of overriding it.
  • Using new keyword instead of override, which hides the base method rather than overriding it.

Here is an example showing the wrong and right way:

csharp
class BaseClass
{
    public void ShowMessage()
    {
        Console.WriteLine("Base method");
    }
}

class WrongDerived : BaseClass
{
    // This hides the base method, does NOT override
    public new void ShowMessage()
    {
        Console.WriteLine("Wrong override");
    }
}

class CorrectDerived : BaseClass
{
    // Error: Cannot override non-virtual method
    // public override void ShowMessage() { }

    // Correct way: base method must be virtual
}

// Correct base class for override
class VirtualBase
{
    public virtual void ShowMessage()
    {
        Console.WriteLine("Virtual base method");
    }
}

class CorrectOverride : VirtualBase
{
    public override void ShowMessage()
    {
        Console.WriteLine("Correct override");
    }
}
📊

Quick Reference

  • virtual: Use in base class to allow method overriding.
  • override: Use in derived class to replace base method.
  • new: Use to hide base method without overriding.
  • Overriding requires exact method signature match.
  • Use base.MethodName() inside override to call base version.

Key Takeaways

Use override in a derived class to replace a virtual or abstract method from the base class.
The base method must be marked virtual, abstract, or override to allow overriding.
Forgetting override causes method hiding, not overriding, which can lead to unexpected behavior.
Use base.MethodName() inside an override to call the original base class method if needed.
Method signatures must match exactly when overriding.