๐๏ธ C# Polymorphism โ One Interface, Multiple Behaviors
๐งฒ Introduction โ Why Learn Polymorphism in C#?
Polymorphism is a key principle of object-oriented programming that allows you to treat different objects in a uniform way while enabling them to behave differently. In C#, polymorphism increases flexibility, maintainability, and scalabilityโespecially in large systems and frameworks.
๐ฏ In this guide, youโll learn:
- What polymorphism means in C#
- Types of polymorphism: compile-time and runtime
- How to use virtual/override and interfaces
- Practical examples and use cases
๐ Core Concept โ What is Polymorphism?
Polymorphism allows different types to be treated as the same base type, yet respond differently to the same method call. It supports the concept of “one interface, many implementations.”
๐ง Types of Polymorphism in C#
Type | Description | Example Keyword |
---|---|---|
Compile-time (Static) | Method overloading (same name, different args) | Overload |
Runtime (Dynamic) | Method overriding via inheritance | virtual/override |
๐ Compile-Time Polymorphism โ Method Overloading
class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
๐ Use Case: Multiple methods with same name but different signatures.
๐ Runtime Polymorphism โ Method Overriding
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal speaks");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks");
}
}
Animal pet = new Dog();
pet.Speak(); // Output: Dog barks
๐ Use Case: Allow derived classes to override base functionality.
๐งฉ Polymorphism with Interfaces
interface IShape
{
void Draw();
}
class Circle : IShape
{
public void Draw() => Console.WriteLine("Drawing Circle");
}
class Square : IShape
{
public void Draw() => Console.WriteLine("Drawing Square");
}
IShape shape = new Circle();
shape.Draw(); // Output: Drawing Circle
๐ Use Case: Define behavior contract without implementation.
๐ Comparison Table โ Overloading vs Overriding
Feature | Overloading | Overriding |
---|---|---|
Type | Compile-time | Runtime |
Signature | Must differ | Must match |
Use Case | Multiple ways to perform similar task | Modify inherited behavior |
Keyword | None (overload by signature) | virtual / override |
๐ก Best Practices & Tips
๐ก Tip: Use polymorphism to write extensible, loosely-coupled code.
โ ๏ธ Pitfall: Avoid excessive use of inheritance for polymorphism; prefer interfaces or abstract classes.
๐ Best Practice: Always mark overridden methods with the override
keyword to avoid bugs and confusion.
๐ ๏ธ Real-World Use Cases
- ๐งพ Invoice calculation systems (
Invoice โ RetailInvoice
,WholesaleInvoice
) - ๐ฎ Game objects (
Enemy
,Player
,NPC
) implementingUpdate()
- ๐ฆ UI rendering components (
Control โ Button
,Label
, etc.) - ๐ Strategy design pattern (e.g., different payment methods)
๐ Summary โ Recap & Next Steps
๐งต Key Takeaways:
- Polymorphism enables common interface, varied behavior.
- Use
virtual
andoverride
for runtime polymorphism. - Use method overloading for compile-time polymorphism.
โ๏ธ Real-world relevance: C# polymorphism powers framework extensibility, design patterns, and interface-driven APIs.
โ FAQ Section
โ Whatโs the difference between inheritance and polymorphism?
โ
Inheritance lets one class use code from another; polymorphism lets it modify or extend behavior dynamically.
โ Can I override a method not marked as virtual
?
โ
โ No. Only virtual
, abstract
, or override
methods can be overridden.
โ Is overloading a type of polymorphism?
โ
Yes. Overloading is compile-time polymorphism, while overriding is runtime.
โ Can interfaces be used for polymorphism?
โ
Yes. Interfaces are the most flexible way to implement polymorphism across unrelated classes.
โ What is the benefit of polymorphism?
โ
It allows code flexibility, maintainability, and the use of design patterns like Strategy, Factory, and Dependency Injection.
Share Now :