Complete C# Tutorial

Method Overriding vs. Method Hiding in C# – A Friendly Guide

🎯 Introduction

Hey there! 👋 Have you ever been confused about Method Overriding and Method Hiding in C#? You’re not alone! Many beginners mix them up because they look pretty similar. But trust me, once you get the hang of it, it’s super easy.

In this lesson, we’ll break things down with simple examples, real-world scenarios, and clear explanations. By the end, you’ll know exactly when to use overriding and when to use hiding.

So, are you ready? Let’s dive in! 🚀

🎭 What is Method Overriding in C#?

Method Overriding allows a child class to provide a new implementation for a method that is already defined in the parent class. It is done using the override keyword.

📢 Key Points:

  • The method signature (name and parameters) must be the same.
  • The base method must be marked as virtual or abstract.
  • The overridden method must have the override keyword.

 

✅ Example – Method Overriding

				
					using System;

class Parent
{
    public virtual void ShowMessage()  
    {
        Console.WriteLine("Hello from Parent class!");
    }
}

class Child : Parent
{
    public override void ShowMessage()  
    {
        Console.WriteLine("Hello from Child class! (Overridden)");
    }
}

class Program
{
    static void Main()
    {
        Parent obj1 = new Parent();
        obj1.ShowMessage(); // Calls Parent method

        Parent obj2 = new Child();
        obj2.ShowMessage(); // Calls Child method (Overriding in action)
    }
}
				
			
🔍 Output:
				
					Hello from Parent class!
Hello from Child class! (Overridden)
				
			

🛠️ Explanation:

  • The ShowMessage() method in the Child class overrides the one in the Parent class.
  • Even though obj2 is of type Parent, it still calls the Child’s overridden method because of runtime polymorphism.

Pretty cool, right? 😎

🎭 What is Method Hiding in C#?

Method Hiding allows a child class to define a new method with the same name as the one in the parent class, but it doesn’t override it. Instead, it hides the parent method using the new keyword.

📢 Key Points:

  • The base method does not need to be virtual.
  • The new method must have the new keyword.
  • The parent method is not replaced; it’s just hidden when accessed via the child class.

 

✅ Example – Method Hiding

				
					using System;

class Parent
{
    public void ShowMessage()
    {
        Console.WriteLine("Hello from Parent class!");
    }
}

class Child : Parent
{
    public new void ShowMessage()
    {
        Console.WriteLine("Hello from Child class! (Hiding)");
    }
}

class Program
{
    static void Main()
    {
        Parent obj1 = new Parent();
        obj1.ShowMessage(); // Calls Parent method

        Child obj2 = new Child();
        obj2.ShowMessage(); // Calls Child method

        Parent obj3 = new Child();
        obj3.ShowMessage(); // Calls Parent method (Hiding in action)
    }
}
				
			
🔍 Output:
				
					Hello from Parent class!
Hello from Child class! (Hiding)
Hello from Parent class!
				
			

🛠️ Explanation:

  • The ShowMessage() method in Child hides the one in Parent.
  • When obj3 is declared as Parent but assigned a Child object, it still calls Parent’s method.
  • Unlike overriding, method hiding does not use runtime polymorphism.

Did you notice the difference? This is a common gotcha for beginners! 🤯

⚔️ Method Overriding vs. Method Hiding – Key Differences

FeatureMethod OverridingMethod Hiding
Keyword Usedoverridenew
Parent Method TypeMust be virtual or abstractCan be any method
Replaces Parent Method?✅ Yes (Runtime Polymorphism)❌ No (Only Hidden)
Calls with Base TypeCalls Child MethodCalls Parent Method
  •  If you want polymorphismUse Method Overriding.
  • If you want to define a new method with the same name but not overrideUse Method Hiding.

🌎 Real-World Example

Let’s say you have a basic calculator (Parent) and an advanced calculator (Child).

 

✅ Overriding Example – Advanced Calculator with Override

				
					class Calculator
{
    public virtual void ShowResult()
    {
        Console.WriteLine("Performing basic calculation...");
    }
}

class AdvancedCalculator : Calculator
{
    public override void ShowResult()
    {
        Console.WriteLine("Performing advanced calculation...");
    }
}
				
			
🔍 Output:
				
					Performing basic calculation...
Performing advanced calculation...
				
			

📝 Explanation:

  1. The ShowResult() method in Calculator is virtual, meaning it can be overridden.
  2. The AdvancedCalculator class overrides this method using override.
  3. When calling ShowResult() on an instance of AdvancedCalculator, it executes the overridden method from AdvancedCalculator, not Calculator.
  4. This works because of runtime polymorphism—even though calc2 is of type Calculator, it executes the overridden method in AdvancedCalculator.

Hiding Example – Advanced Calculator with new

				
					using System;

class Calculator
{
    public void ShowResult()
    {
        Console.WriteLine("Performing basic calculation...");
    }
}

class AdvancedCalculator : Calculator
{
    public new void ShowResult()
    {
        Console.WriteLine("Performing advanced calculation...");
    }
}

class Program
{
    static void Main()
    {
        Calculator calc1 = new Calculator();
        calc1.ShowResult(); // Calls base class method

        AdvancedCalculator calc2 = new AdvancedCalculator();
        calc2.ShowResult(); // Calls child class method

        Calculator calc3 = new AdvancedCalculator();
        calc3.ShowResult(); // Calls base class method (not overridden!)
    }
}
				
			
🔍 Output:
				
					Performing basic calculation...
Performing advanced calculation...
Performing basic calculation...
				
			

📝 Explanation:

  1. The ShowResult() method in Calculator is not virtual, so it cannot be overridden.
  2. Instead, AdvancedCalculator hides the parent method using the new keyword.
  3. calc2.ShowResult(); calls AdvancedCalculator’s version of ShowResult().
  4. However! If we store an AdvancedCalculator object inside a Calculator reference (calc3), it calls the parent method instead.
  5. This is because hiding does not support polymorphism—C# still treats it as the Calculator class, not AdvancedCalculator.

📌 Overriding is useful when you want to extend an existing functionality, while hiding is useful when you want to define a completely new version of a method.

 

🎯 Why is This Important?

✅ Overriding helps in polymorphism, making code more flexible and reusable.
✅ Hiding is useful when you don’t want to modify the parent class but need a similar method.
✅ Understanding this will help you write cleaner, maintainable, and bug-free code.

 

🔮 Next What?

Congratulations! 🎉 You just mastered Method Overriding vs. Method Hiding in C#. Now, you can confidently decide when to override and when to hide methods.

🚀 Up Next: Sealed Classes and Methods in C#
(You’ll learn how to prevent further inheritance and method overriding!)

See you in the next lesson! Keep coding and have fun! 🎯💡

Leave a Comment

16 + nineteen =

Share this Doc

Method Overriding vs Method Hiding

Or copy link