Polymorphism in C#: Perform the Same Action in Different Ways

Polymorphism is one of the four key principles of Object-Oriented Programming (OOP) in C#, along with Encapsulation, Inheritance, and Abstraction.
The word Polymorphism comes from the Greek words poly (many) and morph (forms), meaning “many forms.”

In simple terms, polymorphism allows one action to behave differently based on the object that performs it.
Let’s explore this important concept step by step.


🎯 What Is Polymorphism?

Polymorphism in C# means that a single method, operator, or object can take on different forms or behaviors depending on the situation.

Example:
A teacher and a student both can “introduce” themselves — but each does it differently.
This is the power of polymorphism — same method name, different implementation!


⚙️ Types of Polymorphism in C#

C# supports two main types of polymorphism:

  1. Compile-time polymorphism (also called Method Overloading)
  2. Runtime polymorphism (also called Method Overriding)

Let’s understand both with examples.


🧩 1. Compile-Time Polymorphism (Method Overloading)

Compile-time polymorphism happens when multiple methods in the same class share the same name but have different parameter lists (different number, type, or order of parameters).

The compiler decides which method to call at compile-time.

✅ Example:

class MathOperation
{
    public void Add(int a, int b)
    {
        Console.WriteLine("Sum of 2 numbers: " + (a + b));
    }

    public void Add(int a, int b, int c)
    {
        Console.WriteLine("Sum of 3 numbers: " + (a + b + c));
    }
}

class Program
{
    static void Main()
    {
        MathOperation math = new MathOperation();
        math.Add(10, 20);
        math.Add(5, 15, 25);
    }
}

📘 Output:

Sum of 2 numbers: 30  
Sum of 3 numbers: 45

Here, both methods are named Add() but work differently based on their parameters — that’s method overloading.


🧠 2. Runtime Polymorphism (Method Overriding)

Runtime polymorphism occurs when a derived class overrides a method of its base class.
This is achieved using the virtual and override keywords.
The correct method to execute is determined at runtime.

✅ Example:

class Person
{
    public virtual void PrintInfo()
    {
        Console.WriteLine("This is a person.");
    }
}

class Teacher : Person
{
    public override void PrintInfo()
    {
        Console.WriteLine("This is a teacher.");
    }
}

class Student : Person
{
    public override void PrintInfo()
    {
        Console.WriteLine("This is a student.");
    }
}

class Program
{
    static void Main()
    {
        Person p1 = new Teacher();
        Person p2 = new Student();

        p1.PrintInfo(); // Output: This is a teacher.
        p2.PrintInfo(); // Output: This is a student.
    }
}

📘 Here, both Teacher and Student override the same method PrintInfo(), but each provides its own version of behavior — that’s runtime polymorphism!


🧱 C# Keywords Used in Polymorphism

KeywordDescription
virtualDeclares a base class method that can be overridden in a derived class.
overrideModifies the inherited method to change its behavior.
newHides a base class method without overriding it. (Not true polymorphism)

🌟 Benefits of Polymorphism

✅ Makes code flexible and reusable
✅ Simplifies code maintenance and updates
✅ Allows extending behavior without modifying existing code
✅ Supports clean, modular, and scalable design


💡 Real-Life Analogy

Think of polymorphism like a “remote control.”
The same button (say, Power) performs the same action (turn on/off) — but behaves differently depending on the device (TV, AC, or Music System).
That’s polymorphism in real life — one action, many behaviors!


🏁 Conclusion

Polymorphism in C# helps developers write clean, reusable, and flexible code.
By using method overloading and method overriding, we can perform the same action in multiple ways depending on the object type.

If you’re learning C#, mastering polymorphism is a big step toward understanding the power of Object-Oriented Programming.

Leave a Comment