What is the difference between overriding and shadowing in C#?

In C#, overriding and shadowing (also known as method hiding) are two different mechanisms for modifying inherited behavior. Overriding uses the virtual/override keywords to provide specific implementations of base class methods, while shadowing uses the new keyword to hide base class members entirely.

Syntax

Following is the syntax for method overriding −

// Base class
public virtual void MethodName() { }

// Derived class
public override void MethodName() { }

Following is the syntax for method shadowing/hiding −

// Base class
public void MethodName() { }

// Derived class
public new void MethodName() { }

Method Overriding

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. The base class method must be marked as virtual, abstract, or override

Method Overriding Base Class virtual Display() "Base Display" Derived Class override Display() "Derived Display" Runtime polymorphism Method resolved based on object type

Example

using System;

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

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

class Program {
   public static void Main() {
      Animal animal = new Animal();
      Animal dog = new Dog();
      
      animal.MakeSound();
      dog.MakeSound();  // Calls overridden method
   }
}

The output of the above code is −

Animal makes a sound
Dog barks

Method Shadowing (Hiding)

Method shadowing hides the base class method completely. The new keyword creates a new method that shadows the base class method. Which method gets called depends on the reference type, not the object type −

Method Shadowing Base Class Display() "Base Display" Derived Class new Display() "New Display" Compile-time binding Method resolved based on reference type

Example

using System;

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

class DerivedClass : BaseClass {
   public new void Display() {
      Console.WriteLine("Derived class Display method");
   }
}

class Program {
   public static void Main() {
      BaseClass baseRef = new DerivedClass();
      DerivedClass derivedRef = new DerivedClass();
      
      baseRef.Display();     // Calls base class method
      derivedRef.Display();  // Calls derived class method
   }
}

The output of the above code is −

Base class Display method
Derived class Display method

Comparison

Aspect Overriding Shadowing
Keyword override new
Base Method Requirement Must be virtual, abstract, or override No special requirement
Polymorphism Runtime polymorphism Compile-time binding
Method Resolution Based on object type Based on reference type
Purpose Provide specific implementation Hide base class method

Accessing Base Class Methods

In overriding, you can access the base class method using the base keyword. In shadowing, the base method is completely hidden −

Example

using System;

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

class Car : Vehicle {
   public override void Start() {
      base.Start();  // Call base method
      Console.WriteLine("Car engine started");
   }
}

class Program {
   public static void Main() {
      Car car = new Car();
      car.Start();
   }
}

The output of the above code is −

Vehicle starting...
Car engine started

Conclusion

Overriding enables runtime polymorphism and allows derived classes to provide specific implementations of base class methods. Shadowing hides base class methods entirely and uses compile-time binding. Use overriding for true polymorphic behavior and shadowing when you need to completely replace inherited functionality.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements