Polymorphism in .NET
Polymorphism is one of the four core pillars of object-oriented programming (OOP), alongside encapsulation, abstraction, and inheritance. In .NET, polymorphism allows your code to take many forms — quite literally.
🔷 Introduction
The word comes from Greek: Poly means many, and Morph means form.
In programming, polymorphism lets you use a single interface or method to operate on different types of objects. It allows for more generic and reusable code, enabling the same method or action to behave differently based on the object it’s acting upon.
🔷 Real-World Analogy: The Remote Control
Imagine a universal remote control:
- You press the power button.
- It works for a TV, AC, or sound system.
- Each device reacts differently.
This is polymorphism in action — one interface, multiple behaviors.
🔷 Types of Polymorphism in .NET
.NET supports two primary types:
- Compile-time Polymorphism: Method Overloading
- Run-time Polymorphism: Method Overriding
1. Compile-Time Polymorphism (Method Overloading)
Occurs when multiple methods have the same name but different parameter lists:
class Calculator {
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
public int Add(int a, int b, int c) => a + b + c;
}
2. Run-Time Polymorphism (Method Overriding)
Uses virtual and override to provide different behaviors at runtime:
class Animal {
public virtual void Speak() => Console.WriteLine("The animal makes a sound");
}
class Dog : Animal {
public override void Speak() => Console.WriteLine("Dog says: Woof");
}
class Cat : Animal {
public override void Speak() => Console.WriteLine("Cat says: Meow");
}
// Usage
Animal myAnimal = new Dog();
myAnimal.Speak(); // Dog says: Woof
myAnimal = new Cat();
myAnimal.Speak(); // Cat says: Meow
🔷 Polymorphism with Interfaces
Interfaces provide a contract that different types can implement in their own way:
interface IShape {
double GetArea();
}
class Circle : IShape {
public double Radius { get; set; }
public double GetArea() => Math.PI * Radius * Radius;
}
class Square : IShape {
public double Side { get; set; }
public double GetArea() => Side * Side;
}
// Usage
List<IShape> shapes = new List<IShape> {
new Circle { Radius = 5 },
new Square { Side = 4 }
};
foreach (var shape in shapes) {
Console.WriteLine($"Area: {shape.GetArea()}");
}
✅ Benefits of Polymorphism
- Simplifies code: Avoids duplicate logic
- Promotes extensibility: New types can be added easily
- Supports loose coupling: Works with interfaces
- Improves reusability: Shared behaviors reused efficiently
🔍 Polymorphism vs Method Overloading vs Overriding
| Feature | Method Overloading | Method Overriding | Polymorphism |
|---|---|---|---|
| Type | Compile-time | Run-time | Run-time |
| Inheritance Required | No | Yes | Yes / Interface |
| Method Signature | Must differ | Must match | Matches interface/base |
| Use Case | Multiple versions | Subclass behavior | Generic operations |
🎯 Conclusion
Polymorphism is a powerful concept that enables you to write clean, flexible, and scalable code. Whether you're overloading methods for clarity or overriding them for customization, polymorphism helps future-proof your .NET applications.
Next: Learn about Abstraction in .NET or Encapsulation Techniques.
Comments
Post a Comment