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 is Runtime Polymorphism in C#?

Runtime Polymorphism (also called Method Overriding) means:

  1. The child class redefines a method from the parent class.
  2. The method signature remains the same, but the implementation changes.
  3. 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");
    }
}
				
			
πŸš€ Use 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?

  1. Code looks clean β†’ No need for different method names like DrawCircle(), DrawSquare().
  2. More flexibility β†’ Behavior changes dynamically at runtime.
  3. Works with inheritance β†’ Child classes can modify the parent class method.
  4. 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! πŸš€

Leave a Comment

Share this Doc

Runtime Polymorphism (Method Overriding)

Or copy link