Overriding in C#

Method overriding in C# enables runtime polymorphism by allowing a derived class to provide a specific implementation of a method that is already defined in its base class. This is achieved using the virtual keyword in the base class and the override keyword in the derived class.

Overriding is essential for implementing dynamic polymorphism, where the method to be called is determined at runtime based on the actual object type rather than the reference type.

Syntax

Following is the syntax for method overriding using virtual and override keywords −

// Base class with virtual method
public virtual ReturnType MethodName() {
    // base implementation
}

// Derived class overriding the method
public override ReturnType MethodName() {
    // specific implementation
}

For abstract classes, the syntax is −

// Abstract method in base class
public abstract ReturnType MethodName();

// Implementation in derived class
public override ReturnType MethodName() {
    // specific implementation
}

Using Virtual Methods

Virtual methods allow base classes to define a default implementation that can be overridden by derived classes −

Virtual Method Overriding Animal virtual Speak() Base implementation Dog override Speak() Cat override Speak()

Example

using System;

class Animal {
    public virtual void Speak() {
        Console.WriteLine("The animal makes a sound");
    }
}

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("The dog barks");
    }
}

class Cat : Animal {
    public override void Speak() {
        Console.WriteLine("The cat meows");
    }
}

class Program {
    static void Main(string[] args) {
        Animal myAnimal;
        
        myAnimal = new Dog();
        myAnimal.Speak();
        
        myAnimal = new Cat();
        myAnimal.Speak();
        
        myAnimal = new Animal();
        myAnimal.Speak();
    }
}

The output of the above code is −

The dog barks
The cat meows
The animal makes a sound

Using Abstract Classes

Abstract classes define methods that must be implemented by derived classes, providing a contract for inheritance −

Example

using System;

abstract class Shape {
    public abstract int Area();
    
    public void Display() {
        Console.WriteLine("Area: " + Area());
    }
}

class Rectangle : Shape {
    private int length;
    private int width;

    public Rectangle(int a, int b) {
        length = a;
        width = b;
    }

    public override int Area() {
        Console.WriteLine("Rectangle class area calculation:");
        return (width * length);
    }
}

class Circle : Shape {
    private int radius;

    public Circle(int r) {
        radius = r;
    }

    public override int Area() {
        Console.WriteLine("Circle class area calculation:");
        return (int)(3.14 * radius * radius);
    }
}

class ShapeTest {
    static void Main(string[] args) {
        Shape shape1 = new Rectangle(10, 7);
        shape1.Display();
        
        Shape shape2 = new Circle(5);
        shape2.Display();
    }
}

The output of the above code is −

Rectangle class area calculation:
Area: 70
Circle class area calculation:
Area: 78

Key Rules for Overriding

  • The base class method must be marked as virtual or abstract.

  • The derived class method must use the override keyword.

  • The method signature (name, parameters, return type) must match exactly.

  • Abstract methods have no implementation in the base class and must be overridden.

  • Virtual methods have a default implementation that can be optionally overridden.

Virtual vs Abstract Methods

Virtual Methods Abstract Methods
Have a default implementation in the base class No implementation in the base class
Overriding is optional in derived classes Must be overridden in derived classes
Base class can be instantiated Base class cannot be instantiated
Use virtual keyword Use abstract keyword

Conclusion

Method overriding in C# enables runtime polymorphism through virtual and abstract methods. Virtual methods provide optional overriding with default implementations, while abstract methods enforce mandatory implementation in derived classes, making it a powerful mechanism for creating flexible and extensible class hierarchies.

Updated on: 2026-03-17T07:04:35+05:30

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements