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 hiding in C#?
In C#, method overriding and method hiding (also called shadowing) are two different mechanisms for redefining parent class methods in a derived class. Method overriding uses the override keyword and provides polymorphic behavior, while method hiding uses the new keyword and creates a separate method that shadows the parent method.
Syntax
Following is the syntax for method overriding using the override keyword −
public class BaseClass {
public virtual void Method() { }
}
public class DerivedClass : BaseClass {
public override void Method() { }
}
Following is the syntax for method hiding using the new keyword −
public class BaseClass {
public void Method() { }
}
public class DerivedClass : BaseClass {
public new void Method() { }
}
Method Overriding
Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its parent class. The parent method must be marked as virtual, abstract, or override.
Example of Method Overriding
using System;
public class Animal {
public virtual void MakeSound() {
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks");
}
}
public class Program {
public static void Main() {
Animal animal = new Dog();
animal.MakeSound();
Animal animal2 = new Animal();
animal2.MakeSound();
}
}
The output of the above code is −
Dog barks Animal makes a sound
Method Hiding
Method hiding allows a derived class to define a new method with the same name as a parent class method. The new keyword explicitly indicates that the method is hiding the parent method, not overriding it.
Example of Method Hiding
using System;
public class BaseClass {
public void Display() {
Console.WriteLine("Base class Display method");
}
}
public class DerivedClass : BaseClass {
public new void Display() {
Console.WriteLine("Derived class Display method");
}
}
public class Program {
public static void Main() {
BaseClass baseRef = new DerivedClass();
baseRef.Display();
DerivedClass derivedRef = new DerivedClass();
derivedRef.Display();
}
}
The output of the above code is −
Base class Display method Derived class Display method
Comparison Between Overriding and Hiding
| Aspect | Method Overriding | Method Hiding |
|---|---|---|
| Keyword | override |
new |
| Parent Method Requirement | Must be virtual, abstract, or override
|
Can be any access modifier |
| Polymorphism | Runtime polymorphism (dynamic dispatch) | Compile-time binding |
| Method Selection | Based on actual object type | Based on reference type |
| Inheritance Chain | Replaces method in inheritance chain | Creates new method, hides parent |
Accessing Base Methods
Example
using System;
public class Vehicle {
public virtual void Start() {
Console.WriteLine("Vehicle starting");
}
public void Stop() {
Console.WriteLine("Vehicle stopping");
}
}
public class Car : Vehicle {
public override void Start() {
base.Start();
Console.WriteLine("Car engine started");
}
public new void Stop() {
base.Stop();
Console.WriteLine("Car stopped with handbrake");
}
}
public class Program {
public static void Main() {
Vehicle vehicle = new Car();
vehicle.Start();
vehicle.Stop();
}
}
The output of the above code is −
Vehicle starting Car engine started Vehicle stopping
Conclusion
Method overriding provides runtime polymorphism where the actual object type determines which method is called, while method hiding creates a new method that shadows the parent method based on the reference type. Use overriding for true polymorphic behavior and hiding when you need to completely replace a parent method without polymorphism.
