C# Polymorphism Explained: Method Overloading and Overriding
Polymorphism in C# is an Object-Oriented Programming (OOP) concept that means “one name, many forms.” It allows methods, objects, or operators to behave differently depending on the context.
In simple terms, polymorphism lets you use a single interface or method in multiple ways.
1. What is Polymorphism in C#?
Polymorphism allows:
• One method to have multiple behaviors
• Same method name but different implementations
• Flexible and reusable code design
2. Types of Polymorphism in C#
C# supports two main types:
| Type | Description |
|---|---|
| Compile-time Polymorphism | Method overloading (same method name, different parameters) |
| Run-time Polymorphism | Method overriding (base class method changed in derived class) |
3. Compile-time Polymorphism (Method Overloading)
Method overloading means:
• Same method name
• Different parameter list
Example:
using System;
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(2, 3));
Console.WriteLine(calc.Add(2, 3, 4));
}
}
4. Run-time Polymorphism (Method Overriding)
Method overriding allows a derived class to change a base class method.
Example:
using System;
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal makes a sound");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Dog();
myAnimal.Speak();
}
}
Output:
Dog barks
5. Real-World Example
Think of polymorphism like a remote control:
| Concept | Real-World Example | Explanation |
|---|---|---|
| Polymorphism | Remote Control | Same button performs different actions depending on the device |
| Method (Button) | Power Button | Same action name, but behavior changes based on device |
| Object 1 | TV | Power button turns TV ON/OFF |
| Object 2 | Air Conditioner | Power button controls cooling system |
| Object 3 | Fan | Power button starts or stops rotation |
6. Why Use Polymorphism?
Advantages:
• Code flexibility
• Easier maintenance
• Reusability
• Scalable system design
• Reduces duplication
7. When to Use Polymorphism?
Use polymorphism when:
• You want different behaviors for the same method
• You are designing extensible systems
• You are working with inheritance hierarchies
8. Common Mistakes
• Forgetting virtual keyword in base class
• Forgetting override in derived class
• Confusing overloading vs overriding
• Using polymorphism where simple methods are enough
9. Method Overloading vs Overriding
| Feature | Overloading | Overriding |
|---|---|---|
| Type | Compile-time | Run-time |
| Method Name | Same | Same |
| Parameters | Different | Same |
| Inheritance Required | No | Yes |
10. Best Practices
• Use overriding for flexible system design
• Use overloading for convenience methods
• Keep method behavior consistent
• Avoid unnecessary complexity
• Always use clear naming and structure