Open In App

Method Overriding in C#

Last Updated : 16 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Method overriding in C# allows a derived class to provide a new implementation of a method that is already defined in its base class. It is a key feature of runtime polymorphism.

Example: Program to demonstrate Method Overriding in detail.

C#
using System;

class Animal {
  
    // Base class
    public virtual void Move()
    {
        Console.WriteLine("Animal is moving.");
    }

    public void Eat()
    {
        Console.WriteLine("Animal is eating.");
    }
}

class Dog : Animal {
  
    // Overriding the Move method from the base class
    public override void Move()
    {
        Console.WriteLine("Dog is running.");
    }

    public void Bark()
    {
        Console.WriteLine("Dog is barking.");
    }
}

class Geeks {
    static void Main()
    {
        Dog d = new Dog();
        d.Move();
        d.Eat();
        d.Bark();
    }
}

Output
Dog is running.
Animal is eating.
Dog is barking.

Method Overriding Example Flow

Methodoverriding
Method Overriding example flow
  • Animal class defines two methods: Move() (virtual) and Eat().
  • Dog class inherits from Animal and overrides Move() with its own implementation.
  • Bark() is a new method specific to Dog.
  • Move(): executes the overridden version (Dog is running.).
  • eat(): executes the base class method (Animal is eating.).
  • Bark(): executes the method defined in Dog.

Keywords for Method Overrriding

In C#, three keywords are used for Method Overriding which are listed below:

1. virtual Keyword

This keyword is used in the base class to define a method that can be overridden in a derived class.

Example 1: Method overriding using virtual keyword.

C#
using System;

class Animal
{
    // Base class method marked as virtual
    public virtual void Move()
    {
        Console.WriteLine("Animal is moving.");
    }
}

class Dog : Animal
{
    // Overriding the base class method using 'override'
    public override void Move()
    {
        Console.WriteLine("Dog is running.");
    }
}

class Geeks
{
    static void Main()
    {   
        // Creating an object of the Dog class
        Dog dog = new Dog();  
        dog.Move();  
    }
}

Output
Dog is running.

Explanation: The Move() method in the Animal class is marked as virtual, which allows it to be overridden in the Dog class using the override keyword.

Example 2: Method overriding using virtual and override modifiers.

C#
using System;

class baseClass {

	// show() is 'virtual' here
	public virtual void show(){
		Console.WriteLine("Base class");
	}
}

// 'derived' class inherits from 'baseClass'
class derived : baseClass{	
	//'show()' is 'override' here
	public override void show(){
		Console.WriteLine("Derived class");
	}
}

class Geeks{
	
	public static void Main(){
		
		baseClass obj;

		// 'obj' is the object class 'baseClass'
		obj = new baseClass();
		
		// it invokes 'show()' of class 'baseClass'
		obj.show();
		
		// the same object 'obj' is now the object of class 'derived'
		obj = new derived();
		
		// it invokes 'show()' of class 'derived' 'show()' of class 'derived' is overridden for 'override' modifier
		obj.show();		
	}
}

Output
Base class
Derived class

Explanation:

  • The base class defines show() as virtual, which means it can be overridden.
  • The derived class provides its own implementation using override.
  • When obj refers to a baseClass object, the base version of show() is executed.
  • When obj refers to a derived object, the overridden version is executed.

2. override keyword

This keyword is used in the derived class to provide a specific implementation of a method defined in the base class.

Example: Method overriding using override keyword.

C#
using System;

class Animal{
  
    // Base class method marked as virtual
    public virtual void Move(){
        Console.WriteLine("Animal is moving.");
    }
}

class Dog : Animal {
  
    // Overriding the base class method using 'override'
    public override void Move(){
        Console.WriteLine("Dog is running.");
    }
}

class Geeks {
    static void Main(){

        // Creating an object of the Dog class
        Dog dog = new Dog();
        dog.Move();
    }
}

Output
Dog is running.

Explanation: The Move() method in the Dog class uses the override keyword to change the behavior of the Move() method defined in the Animal class.

3. Base Keyword

This method is used to access members(methods, properties or constructors) of the base class from within a derived class.

Use of Base Keyword:

  • Call methods or functions of base class from derived class.
  • Call constructor internally of base class at the time of inheritance.

Example 1: Method overriding using Base keyword.

C#
using System;

class Animal {
  
    // Base class method marked as virtual
    public virtual void Move(){
        Console.WriteLine("Animal is moving.");
    }
}

class Dog : Animal {
  
    // Derived class method that calls the base class method using 'base'
    public override void Move(){
        // Calls the Move() method from the Animal class
        base.Move();
        Console.WriteLine("Dog is running.");
    }
}

class Geeks {
    static void Main(){
        Dog dog = new Dog();
        dog.Move();
    }
}

Output
Animal is moving.
Dog is running.
  • The Animal class defines a virtual method Move().
  • The Dog class inherits from Animal and overrides the Move() method.
  • Inside the overridden method, base.Move() is used to call the parent class implementation.
  • After calling the base method, the derived class adds its own behavior (Dog is running.).
  • In Main(), creating a Dog object and calling Move() executes both the base method and the derived method.

Example 2: How the base Keyword specifies the calling of base-class constructor from the derived class, when derived class instances are created.

C#
using System;

public class A {
    int n1, n2;

    // Default constructor
    public A() {
        Console.WriteLine("Default Constructor of A Invoked");
    }

    // Parameterized constructor
    public A(int i, int j) {
        n1 = i;
        n2 = j;
        Console.WriteLine("Parameterized Constructor of A Invoked");
        Console.WriteLine("Values are: " + n1 + " and " + n2);
    }
}

public class DerivedClass : A {
    // Default constructor calls base class default constructor implicitly
    public DerivedClass() : base() {
        Console.WriteLine("Default Constructor of DerivedClass Invoked");
    }

    // Parameterized constructor calls base class parameterized constructor explicitly
    public DerivedClass(int i, int j) : base(i, j) {
        Console.WriteLine("Parameterized Constructor of DerivedClass Invoked");
    }
}

class Geeks {
    static void Main() {
        // Invoke no argument constructor
        DerivedClass d1 = new DerivedClass();

        Console.WriteLine();

        // Invoke parameterized constructor
        DerivedClass d2 = new DerivedClass(10, 20);
    }
}

Output:

output
Output

Example 3: How base keyword specifies the base-class constructor called from derived class and calling a method using the base keyword from the derived class.

C#
using System;

public class A {
    public int n1, n2;

    public A() {
        Console.WriteLine("In A 'no argument constructor' invoked");
    }

    // parameterized constructor
    public A(int i, int j) {
        // construct values
        n1 = i;
        n2 = j;
        Console.WriteLine("In A 'parameterized constructor' invoked");
        Console.WriteLine("The invoked values are " + n1 + " and " + n2);
        Console.WriteLine();
    }

    public virtual void swap() {
        Console.WriteLine("Swap function of base class (A) invoked");
        Console.WriteLine("Before swap: num1 = {0} and num2 = {1}", n1, n2);

        // swapping
        int t = n1;
        n1 = n2;
        n2 = t;
        Console.WriteLine("After swap: num1 = {0} and num2 = {1}", n1, n2);
    }
}

// derived class
public class DerivedClass : A {

    // This constructor will instantiate A using base keyword
    public DerivedClass() : base() { }

    // This constructor will instantiate A using base keyword
    public DerivedClass(int i, int j) : base(i, j) { }

    public override void swap() {
        // it accesses the swap function of A using base keyword
        base.swap();
        
        Console.WriteLine();

        Console.WriteLine("Swap function of derived class invoked");
        Console.WriteLine("Before swap: num1 = {0} and num2 = {1}", n1, n2);

        int t = n1;
        n1 = n2;
        n2 = t;
        Console.WriteLine("After swap: num1 = {0} and num2 = {1}", n1, n2);
    }
}

class Program {
    static void Main() {
        // invoke no argument constructor
        DerivedClass d1 = new DerivedClass();
        
        Console.WriteLine();

        // invoke parameterized constructor
        DerivedClass d2 = new DerivedClass(10, 20);
        
        // calling swap function
        d2.swap();
    }
}

Output:

Output
Output

Key Points

  • Method overriding is possible only in derived classes. Because a method is overridden in the derived class from the base class.
  • A non-virtual or a static method can’t be overridden.
  • Both the override method and the virtual method must have the same access level modifier.

Explore