Saturday, 15 November 2025

What is Polymorphism?

Polymorphism in C# - Detailed Explanation

Polymorphism in C#

Polymorphism in C# allows methods to have different implementations depending on the object type. The same method call behaves differently based on the actual object at runtime or depending on overloads at compile time.

Simple definition: Polymorphism = One method → Many behaviors.

Types of Polymorphism in C#

1. Compile-Time Polymorphism (Method Overloading)

This happens when multiple methods share the same name but use different parameters.

// C# Method Overloading Example
class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double x, double y)
    {
        return x + y;
    }

    public string Add(string a, string b)
    {
        return a + b;
    }
}

Here the method Add() works differently depending on what type of arguments you pass.


2. Runtime Polymorphism (Method Overriding)

Achieved using:

  • virtual keyword (parent class)
  • override keyword (child class)

// C# Runtime Polymorphism Example
class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Animal sound");
    }
}

class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Dog barks");
    }
}

class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Cat meows");
    }
}
// Main Program
Animal a1 = new Dog();
Animal a2 = new Cat();

a1.Speak();  // Dog barks
a2.Speak();  // Cat meows

Even though the reference type is Animal, the overridden method in the child class is called.


Polymorphism With Abstract Classes

// Abstract Class Example
abstract class Shape
{
    public abstract void Draw();
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a Circle");
    }
}

class Square : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a Square");
    }
}

// Using Polymorphism
Shape s = new Circle();
s.Draw();  // Drawing a Circle

Using an abstract class ensures that every child MUST implement the method.


Polymorphism Using Interfaces

// Interface Polymorphism Example
interface IPayment
{
    void Pay();
}

class CreditCard : IPayment
{
    public void Pay()
    {
        Console.WriteLine("Paid using Credit Card");
    }
}

class PayPal : IPayment
{
    public void Pay()
    {
        Console.WriteLine("Paid using PayPal");
    }
}

// Using Interface Polymorphism
IPayment payment = new PayPal();
payment.Pay();  // Paid using PayPal

Real-World C# Example

// Real-world example: Notification System
abstract class Notification
{
    public abstract void Send(string message);
}

class Email : Notification
{
    public override void Send(string message)
    {
        Console.WriteLine("Sending Email: " + message);
    }
}

class SMS : Notification
{
    public override void Send(string message)
    {
        Console.WriteLine("Sending SMS: " + message);
    }
}
// Using Polymorphism
List<Notification> channels = new List<Notification>()
{
    new Email(),
    new SMS()
};

foreach (var channel in channels)
{
    channel.Send("Hello User!");
}

Each class implements Send() differently, but the method call stays the same.


Advantages of Polymorphism in C#

  • Makes code flexible and extendable
  • Supports loose coupling
  • Improves code readability and reusability
  • Allows the same interface for different implementations

Conclusion

Polymorphism in C# allows you to write generic code that works with different object types. The correct method is selected based on the actual object at compile time (overloading) or at runtime (overriding).

No comments:

Post a Comment