Overriding, Overloading, and Hiding
Question
Describe the differences between overriding, overloading, and shadowing (hiding).
Short interview answer
Overriding replaces a virtual inherited member for runtime dispatch. Overloading uses the same member name with different parameter lists. Hiding uses
newto introduce a member with the same name, and selection depends on the variable's compile-time type.
Detailed answer
override participates in polymorphism; overloads are selected by argument types and count; hiding depends on the variable’s compile-time type:
class Base
{
public virtual string Virtual() => "base";
public string Hidden() => "base";
}
class Derived : Base
{
public override string Virtual() => "derived";
public new string Hidden() => "derived";
}
Base value = new Derived();
Console.WriteLine(value.Virtual()); // derived
Console.WriteLine(value.Hidden()); // base
A hidden member does not override the base member. Hiding is usually less clear than a deliberate override or a different name.