C# Method Overriding vs. Method Hiding: A Clear Explanation

Understand the key differences between method overriding and method hiding in C#. This tutorial clarifies the behavior, purpose, and implications of each, highlighting polymorphism and inheritance concepts. Learn how to choose the right approach for extending and modifying class functionality in your C# projects.



Method Overriding vs. Method Hiding in C#

Introduction: Overriding and Hiding Methods

In object-oriented programming, inheritance allows a class (the *derived class*) to inherit members (fields and methods) from another class (the *base class*). Two important concepts related to inherited methods are *method overriding* and *method hiding*. Both involve a derived class interacting with a method from its base class, but they differ significantly in their behavior and purpose.

Method Overriding

Method overriding allows a derived class to provide a specific implementation for a method that already exists in its base class. This is a form of polymorphism, allowing you to have different implementations of the same method for objects of different classes. The method signature (name, return type, and parameters) must remain the same in both the base class and the derived class. It's a fundamental concept in object-oriented design that allows you to extend functionality in a structured and organized manner.

Method Overriding: Key Points

  • Inheritance: Occurs in the context of inheritance, allowing subclasses to customize inherited methods.
  • `override` Keyword: The `override` keyword is used in the derived class to explicitly override the base class method.
  • Polymorphism: Enables dynamic method dispatch (runtime polymorphism), allowing calls to be resolved based on the object's actual type.
  • Same Signature: The overriding method must have the same signature as the base class method.
Example C# Code

public class Animal {
    public virtual void MakeSound() { Console.WriteLine("Generic animal sound"); }
}

public class Dog : Animal {
    public override void MakeSound() { Console.WriteLine("Woof!"); }
}

Method Hiding (Shadowing)

Method hiding occurs when a derived class declares a new method with the same name as a method in its base class. Unlike overriding, method hiding doesn't involve changing the implementation of an existing method; instead it creates a *new* method in the derived class that obscures the base class method. The signatures don't need to be the same, and it doesn't support polymorphism.

Method Hiding: Key Points

  • No Polymorphism: Method calls are resolved at compile time based on the reference type, not the actual object type.
  • `new` Keyword: The `new` keyword in the derived class explicitly hides the base class method.
  • Compile-Time Binding: The method to be called is determined at compile time.
Example C# Code

public class Animal {
    public void MakeSound() { Console.WriteLine("Generic animal sound"); }
}

public class Dog : Animal {
    public new void MakeSound() { Console.WriteLine("Woof!"); }
}

Key Differences: Overriding vs. Hiding

Feature Method Overriding Method Hiding
Basic Functionality Provides a new implementation for an existing base class method. Creates a new method in the derived class with the same name as a base class method.
Inheritance Directly related to inheritance. Related to inheritance but doesn't replace the base class method.
Polymorphism Supports runtime polymorphism. Does not support polymorphism.
Keyword `override` `new`
Method Signature Must have the same signature as the base class method. Method signature can be different.
Binding Runtime (dynamic) binding. Compile-time binding.
Purpose Extending or modifying base class behavior. Adding new functionality without modifying the base class method.
Design Patterns Used in design patterns like the Template Method pattern. Less frequently used in design patterns due to lack of polymorphism.