Method Hiding - Redefining Inherited Methods in C#
Vaibhav • September 10, 2025
In the previous article, we explored method overriding - how derived classes can customize inherited behavior
using the virtual
and override
keywords. Now, we turn to a related but distinct
concept: method hiding. While overriding replaces a base class method in a polymorphic way,
method hiding redefines it in a way that breaks polymorphism.
Method hiding allows a derived class to define a new method with the same name as a method in the base class. This can be useful in certain scenarios, but it also introduces subtle behavior differences that can lead to confusion if not used carefully. In this article, we’ll explore how method hiding works, how it differs from overriding, and when (or if) you should use it.
What is Method Hiding?
Method hiding occurs when a derived class defines a method with the same name and signature as a method in its
base class, but without using the override
keyword. Instead, the derived method uses the
new
keyword to explicitly hide the base method.
public class Animal
{
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public new void Speak()
{
Console.WriteLine("Dog barks");
}
}
In this example, Dog.Speak()
hides Animal.Speak()
. The new
keyword tells
the compiler that this is intentional. However, the behavior depends on the type of the reference used to call
the method.
Dog d = new Dog();
d.Speak(); // Output: Dog barks
Animal a = new Dog();
a.Speak(); // Output: Animal speaks
Even though a
refers to a Dog
object, it calls the base class method because
Speak()
is not virtual and not overridden. This is the key difference from method overriding.
Method hiding is not polymorphic. The method that gets called depends on the compile-time type of the reference, not the runtime type of the object.
Why Use the new
Keyword?
The new
keyword is not required to hide a method, but it is recommended. If you hide a method
without using new
, the compiler will issue a warning to alert you of the potential confusion.
public class Cat : Animal
{
// Hides Speak() without 'new' - compiler warning
public void Speak()
{
Console.WriteLine("Cat meows");
}
}
To make your intent clear and avoid warnings, always use new
when hiding a method:
public new void Speak()
{
Console.WriteLine("Cat meows");
}
This tells the compiler (and other developers) that you are intentionally hiding the base method.
Method Hiding vs Method Overriding
Let’s compare the two side by side to understand the difference:
public class Base
{
public virtual void Show() => Console.WriteLine("Base.Show()");
public void Display() => Console.WriteLine("Base.Display()");
}
public class Derived : Base
{
public override void Show() => Console.WriteLine("Derived.Show()");
public new void Display() => Console.WriteLine("Derived.Display()");
}
Base b = new Derived();
b.Show(); // Output: Derived.Show() - overridden
b.Display(); // Output: Base.Display() - hidden
The Show()
method is overridden, so the derived version is called even when accessed through a base
reference. The Display()
method is hidden, so the base version is called when accessed through a
base reference.
Overriding supports polymorphism. Hiding does not. Always prefer overriding when you want polymorphic behavior.
Hiding Properties and Fields
You can also hide properties and fields using the new
keyword. The same rules apply - the member
that gets accessed depends on the reference type.
public class Base
{
public int Value = 10;
}
public class Derived : Base
{
public new int Value = 20;
}
Derived d = new Derived();
Console.WriteLine(d.Value); // Output: 20
Base b = d;
Console.WriteLine(b.Value); // Output: 10
This can be very confusing and is generally discouraged. Avoid hiding fields unless absolutely necessary.
When to Use Method Hiding
Method hiding is rarely needed, but it can be useful in specific scenarios:
- When you want to redefine behavior in a derived class without affecting base class consumers.
- When the base class method is not
virtual
and cannot be overridden. - When you want to provide a more specific version of a method for derived class use only.
However, method hiding can lead to subtle bugs and should be used with caution. If you need polymorphism, use method overriding instead.
Avoiding Pitfalls
Method hiding can introduce confusion if not used carefully. Here are some tips to avoid problems:
- Always use the
new
keyword when hiding a method - never rely on implicit hiding. - Document hidden methods clearly to avoid surprises for other developers.
- Avoid hiding methods that are already
virtual
- override them instead. - Be cautious when hiding fields - it’s almost always better to avoid it.
Summary
Method hiding allows a derived class to redefine a method from its base class using the new
keyword. Unlike method overriding, hiding is not polymorphic - the method that gets called depends on the
compile-time type of the reference. While method hiding can be useful in certain scenarios, it is generally less
flexible and more error-prone than overriding.
In this article, we explored how method hiding works, how it differs from overriding, how to use the
new
keyword, and how to avoid common pitfalls. We also looked at hiding properties and fields, and
discussed when (and when not) to use this feature.
As you continue working with inheritance in C#, use method hiding sparingly and only when it serves a clear purpose. In the next article, we’ll explore Constructor Inheritance - how constructors behave in inheritance hierarchies and how to chain them effectively.