Runtime Polymorphism (Method Overriding) in C# with Examples
π Hey, C# Learner!
Ever wondered how a child class can change the behavior of a method from the parent class? π€
For example, think of a generic animal. It has a method called MakeSound()
. But wait! πΆ A dog barks, a cat meows, and a cow moos. Each animal overrides the same method differently!
This is exactly what Runtime Polymorphism in C# (also called Method Overriding) is all about! π
π What You Are Going to Learn in This Lesson
βοΈ What is Runtime Polymorphism in C#?
βοΈ Why is Method Overriding in C# important?
βοΈ How to use it with simple examples.
βοΈ A real-world example for better understanding.
βοΈ 3 different programming examples to make it crystal clear!
βοΈ A friendly code explanation with output.
π§ What is Runtime Polymorphism in C#?
Runtime Polymorphism (also called Method Overriding) means:
- The child class redefines a method from the parent class.
- The method signature remains the same, but the implementation changes.
- The method that gets executed is decided at runtime, not compile-time.
It helps make code flexible, maintainable, and reusable! π―
π Syntax of Method Overriding in C#
class Parent
{
public virtual void Show()
{
Console.WriteLine("Parent Show Method");
}
}
class Child : Parent
{
public override void Show()
{
Console.WriteLine("Child Show Method");
}
}
virtual
in the parent class and override
in the child class!
π» Simple Example of Method Overriding in C#
Letβs start with a super easy example!
using System;
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Calls the overridden method in Dog class
}
}
π― Output:
Dog barks
π§ Explanation:
1οΈβ£ The Animal
class has a MakeSound()
method marked as virtual
.
2οΈβ£ The Dog
class overrides this method using override
.
3οΈβ£ When we create an Animal
reference and assign it a Dog
object, it calls the Dogβs version of MakeSound()
.
4οΈβ£ This is Runtime Polymorphism in action! π
π Real-World Example of Method Overriding in C#
Imagine a banking system where different types of accounts have different interest rates.
β A normal savings account gives 4% interest.
β A fixed deposit account gives 7% interest.
Letβs implement this!
using System;
class BankAccount
{
public virtual void InterestRate()
{
Console.WriteLine("Bank Interest Rate: 4%");
}
}
class FixedDeposit : BankAccount
{
public override void InterestRate()
{
Console.WriteLine("Fixed Deposit Interest Rate: 7%");
}
}
class Program
{
static void Main()
{
BankAccount account1 = new BankAccount();
BankAccount account2 = new FixedDeposit();
account1.InterestRate(); // Calls BankAccount version
account2.InterestRate(); // Calls FixedDeposit version
}
}
π― Output:
Bank Interest Rate: 4%
Fixed Deposit Interest Rate: 7%
π§ Explanation:
1οΈβ£ Same method name InterestRate()
in both parent and child class.
2οΈβ£ The child class (FixedDeposit
) overrides the method.
3οΈβ£ The correct method is called at runtime, based on the object type.
π‘ This makes your code more dynamic and extensible! π―
π Another Example: Shape Drawing System
Letβs create a shape-drawing system where:
β A generic shape just draws a shape.
β A circle draws a circle.
β A square draws a square.
using System;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Circle");
}
}
class Square : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Square");
}
}
class Program
{
static void Main()
{
Shape myShape;
myShape = new Circle();
myShape.Draw();
myShape = new Square();
myShape.Draw();
}
}
π― Output:
Drawing a Circle
Drawing a Square
π‘ Why is Method Overriding Important?
- Code looks clean β No need for different method names like
DrawCircle()
,DrawSquare()
. - More flexibility β Behavior changes dynamically at runtime.
- Works with inheritance β Child classes can modify the parent class method.
- Makes code reusable β No need to rewrite the same logic again and again.
Β
π― Conclusion
Boom! π You just unlocked another powerful concept in C# β Runtime Polymorphism! π
β You learned how Method Overriding works to redefine a method from the parent class.
β You saw real-world examples like banking and shape drawing.
β You now know why it is important and how it makes code cleaner and more flexible.
So, next time you need to customize a methodβs behavior in a derived class, Method Overriding is your friend! π‘
Β
βοΈ Next What?
Youβre doing awesome! π Now, let’s take polymorphism even further! Up next, weβll explore Polymorphism with Interfaces and Abstract Classes in C#! Stay tuned! π