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
Overriding Vs Shadowing in C#
In C#, overriding and shadowing are two different mechanisms for changing method behavior in derived classes. Overriding provides true polymorphism using virtual methods, while shadowing (method hiding) creates a new method that hides the base class method without polymorphic behavior.
Overriding
Overriding allows a subclass to provide a specific implementation of a method that is already defined in its base class. It requires the base class method to be marked as virtual or abstract, and the derived class method to use the override keyword.
Syntax
Following is the syntax for method overriding −
// Base class
public virtual ReturnType MethodName() {
// base implementation
}
// Derived class
public override ReturnType MethodName() {
// overridden implementation
}
Example
using System;
abstract class Shape {
public abstract int area();
}
class Rectangle : Shape {
private int length;
private int width;
public Rectangle(int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area() {
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}", a);
}
}
The output of the above code is −
Rectangle class area : Area: 70
Shadowing (Method Hiding)
Shadowing, also known as method hiding, allows a derived class to create a new method with the same name as a method in the base class. The new keyword is used to explicitly hide the base class method, creating a completely separate method rather than overriding it.
Syntax
Following is the syntax for method shadowing −
// Base class
public ReturnType MethodName() {
// base implementation
}
// Derived class
public new ReturnType MethodName() {
// new implementation (hides base method)
}
Example
using System;
class Demo {
public class Parent {
public string Display() {
return "Parent Class!";
}
}
public class Child : Parent {
public new string Display() {
return "Child Class!";
}
}
static void Main(String[] args) {
Child child = new Child();
Console.WriteLine(child.Display());
Parent parent = new Child();
Console.WriteLine(parent.Display());
}
}
The output of the above code is −
Child Class! Parent Class!
Overriding vs Shadowing Comparison
| Feature | Overriding | Shadowing |
|---|---|---|
| Keyword Used | override |
new |
| Base Method Requirement | Must be virtual or abstract
|
Any method (no special requirement) |
| Polymorphic Behavior | Yes - runtime method resolution | No - compile-time method resolution |
| Base Reference Behavior | Calls overridden method | Calls base class method |
Demonstrating the Difference
Example
using System;
class BaseClass {
public virtual void VirtualMethod() {
Console.WriteLine("BaseClass VirtualMethod");
}
public void RegularMethod() {
Console.WriteLine("BaseClass RegularMethod");
}
}
class DerivedClass : BaseClass {
public override void VirtualMethod() {
Console.WriteLine("DerivedClass Override");
}
public new void RegularMethod() {
Console.WriteLine("DerivedClass New (Shadowing)");
}
}
class Program {
static void Main() {
DerivedClass derived = new DerivedClass();
BaseClass baseRef = new DerivedClass();
Console.WriteLine("=== Direct Reference ===");
derived.VirtualMethod();
derived.RegularMethod();
Console.WriteLine("=== Base Reference ===");
baseRef.VirtualMethod();
baseRef.RegularMethod();
}
}
The output of the above code is −
=== Direct Reference === DerivedClass Override DerivedClass New (Shadowing) === Base Reference === DerivedClass Override BaseClass RegularMethod
Conclusion
Overriding provides true polymorphism where the derived class method is called even through base class references, while shadowing hides the base method without polymorphic behavior. Use overriding for polymorphic scenarios and shadowing when you need to completely replace a base class method without inheritance relationship.
