Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 −
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 −
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.
