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 polymorphism โ†’ Use Method Overriding.
  • If you want to define a new method with the same name but not override โ†’ Use 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

Share this Doc

Method Overriding vs Method Hiding

Or copy link