This content originally appeared on DEV Community and was authored by Renuka Patil
In C#, method hiding and method overriding are two different ways to provide a new implementation for a method in a derived class. Here’s a detailed comparison:
Method Overriding
Used when you want to modify the behavior of a method defined in a base class.
Requirements:
- The base method must be marked as
virtual
,abstract
, oroverride
. - The derived method must be marked with the
override
keyword.
Example:
class BaseClass
{
public virtual void Show()
{
Console.WriteLine("Base Show");
}
}
class DerivedClass : BaseClass
{
public override void Show()
{
Console.WriteLine("Derived Show");
}
}
Behavior:
- When you call the method using a base class reference, the derived class version is executed (runtime polymorphism).
- It supports dynamic dispatch.
Method Hiding
Used when you want to hide the base class method without modifying its behavior.
Requirements:
- The base method doesn’t need to be virtual.
- The derived method must be marked with the
new
keyword.
Example:
class BaseClass
{
public void Show()
{
Console.WriteLine("Base Show");
}
}
class DerivedClass : BaseClass
{
public new void Show()
{
Console.WriteLine("Derived Show");
}
}
Behavior:
- When you call the method using a base class reference, the base class version is executed.
- It does not support runtime polymorphism.
Key Differences Table:
Feature | Method Overriding | Method Hiding |
---|---|---|
Keyword Used | override |
new |
Base Method Required | Must be virtual , abstract , or override
|
Can be any (not necessarily virtual) |
Polymorphism | Yes (runtime) | No (compile-time only) |
Method Resolution | Based on object type at runtime | Based on reference type at compile-time |
Intent | Modify behavior | Hide original behavior |
Which One to Use?
- Use overriding when you want to extend or modify the behavior in a polymorphic way.
- Use hiding only when you want to hide the base method intentionally, and you’re aware of the compile-time behavior.
so when we use new for a function, that function gets hidden and execute the base class method?
When you use the new
keyword in C# to hide a method, here’s what happens:
Summary:
- If you call the method using a derived class reference, the derived class method is called.
- If you call the method using a base class reference, the base class method is called.
So yes, the base class method gets executed if you’re calling it from a base class reference, even though the derived class has a method with the same name.
Example to Understand:
class BaseClass
{
public void Show()
{
Console.WriteLine("Base Show");
}
}
class DerivedClass : BaseClass
{
public new void Show()
{
Console.WriteLine("Derived Show");
}
}
Test:
BaseClass obj1 = new BaseClass();
obj1.Show(); // Output: Base Show
DerivedClass obj2 = new DerivedClass();
obj2.Show(); // Output: Derived Show
BaseClass obj3 = new DerivedClass();
obj3.Show(); // Output: Base Show ❗
Why?
-
obj3
is of typeBaseClass
, so method hiding causes the base version to be called. - Even though
obj3
is actually holding aDerivedClass
object, since the method is not virtual, it doesn’t support runtime polymorphism.
Key Point:
If you want polymorphism (where the derived method runs even when using a base reference), use virtual
+ override
.
If you use new
, you’re telling the compiler:
“I know there’s a method in the base class with the same name, but I want to hide it in this derived class.”
Happy Coding!
This content originally appeared on DEV Community and was authored by Renuka Patil