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 You Are Going to Learn in This Lesson
โ๏ธ What is Method Overriding?
โ๏ธ What is Method Hiding?
โ๏ธ Key differences between the two
โ๏ธ How to use them with real-life examples
๐ญ 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 theChild
class overrides the one in theParent
class. - Even though
obj2
is of typeParent
, 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 inChild
hides the one inParent
. - When
obj3
is declared asParent
but assigned aChild
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
Feature | Method Overriding | Method Hiding |
---|---|---|
Keyword Used | override | new |
Parent Method Type | Must be virtual or abstract | Can be any method |
Replaces Parent Method? | โ Yes (Runtime Polymorphism) | โ No (Only Hidden) |
Calls with Base Type | Calls Child Method | Calls 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:
- The
ShowResult()
method inCalculator
is virtual, meaning it can be overridden. - The
AdvancedCalculator
class overrides this method usingoverride
. - When calling
ShowResult()
on an instance ofAdvancedCalculator
, it executes the overridden method fromAdvancedCalculator
, notCalculator
. - This works because of runtime polymorphismโeven though
calc2
is of typeCalculator
, it executes the overridden method inAdvancedCalculator
.
โ
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:
- The
ShowResult()
method inCalculator
is not virtual, so it cannot be overridden. - Instead,
AdvancedCalculator
hides the parent method using thenew
keyword. calc2.ShowResult();
calls AdvancedCalculator’s version ofShowResult()
.- However! If we store an
AdvancedCalculator
object inside aCalculator
reference (calc3
), it calls the parent method instead. - This is because hiding does not support polymorphismโC# still treats it as the
Calculator
class, notAdvancedCalculator
.
๐ 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! ๐ฏ๐ก