Curriculum
Polymorphism in C# is one of the four fundamental pillars of Object-Oriented Programming (OOP). Polymorphism in C# allows objects to perform different actions through the same interface or method name. Polymorphism in C# helps developers build flexible, reusable, maintainable, and scalable applications. Every professional .NET developer uses Polymorphism in C# while developing ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Mobile Applications, Cloud Solutions, and Enterprise Software Systems.
Understanding Polymorphism in C# is essential because it enables software to handle different types of objects through a common interface while allowing each object to exhibit its own behavior.
The word Polymorphism comes from two Greek words:
Poly = Many
Morph = Forms
Polymorphism means:
One Interface, Multiple Forms
In software development, Polymorphism in C# allows the same method or interface to behave differently depending on the object being used.
Example:
Animal
Different animal types:
Dog
Cat
Cow
Method:
MakeSound()
Output:
Dog -> Bark
Cat -> Meow
Cow -> Moo
The same method produces different behaviors.
This is the essence of Polymorphism in C#.
Polymorphism in C# helps developers:
Modern enterprise applications rely heavily on polymorphic behavior.
Polymorphism in C# is divided into two categories:
Also called:
Static Polymorphism
Achieved through:
Method Overloading
Operator Overloading
Also called:
Dynamic Polymorphism
Achieved through:
Method Overriding
Virtual Methods
Both types are widely used in .NET applications.
Compile-Time Polymorphism occurs when the compiler determines which method should execute.
The most common approach is Method Overloading.
Method Overloading allows multiple methods to have the same name but different parameter lists.
Example:
public 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;
}
}
Usage:
Calculator calculator =
new Calculator();
Console.WriteLine(
calculator.Add(10,20));
Console.WriteLine(
calculator.Add(10,20,30));
Output:
30
60
The compiler selects the appropriate method based on parameters.
This is Compile-Time Polymorphism.
Methods must differ in:
Example:
Display(int age)
Display(string name)
Both methods are valid.
Return type alone cannot differentiate overloaded methods.
Runtime Polymorphism occurs when the method to execute is determined during program execution.
This is achieved through:
Runtime Polymorphism is one of the most powerful features of OOP.
A Virtual Method can be overridden in a derived class.
Example:
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine(
"Animal Sound");
}
}
The keyword:
virtual
allows overriding.
Method Overriding allows child classes to provide their own implementation.
Example:
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine(
"Bark");
}
}
Example:
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine(
"Meow");
}
}
Each child class provides unique behavior.
Example:
Animal animal;
animal = new Dog();
animal.MakeSound();
animal = new Cat();
animal.MakeSound();
Output:
Bark
Meow
The method executed depends on the object assigned at runtime.
This demonstrates Runtime Polymorphism in C#.
A parent class reference can point to child class objects.
Example:
Animal animal =
new Dog();
This is called:
Upcasting
Benefits:
This concept is heavily used in enterprise software.
Consider a Payment System.
Base Class:
Payment
Derived Classes:
CreditCardPayment
UPIPayment
NetBankingPayment
Method:
ProcessPayment()
Each payment type processes transactions differently.
The application uses a common interface while supporting multiple implementations.
This is a practical example of Polymorphism in C#.
Banking System:
Base Class:
Account
Derived Classes:
SavingsAccount
CurrentAccount
LoanAccount
Method:
CalculateInterest()
Each account calculates interest differently.
Polymorphism makes this implementation efficient.
| Virtual | Override |
|---|---|
| Defined in Parent Class | Defined in Child Class |
| Allows Overriding | Provides New Implementation |
| Optional | Required for Overriding |
| Base Behavior | Child Behavior |
Both work together to enable Runtime Polymorphism.
Method Hiding uses the:
new
keyword.
Example:
public class Animal
{
public void Display()
{
Console.WriteLine(
"Animal");
}
}
public class Dog : Animal
{
public new void Display()
{
Console.WriteLine(
"Dog");
}
}
Output:
Dog
However, Method Overriding is generally preferred over Method Hiding.
Reuse common interfaces.
Add new implementations easily.
Changes affect fewer components.
Applications grow efficiently.
Reduces complexity.
These advantages make Polymorphism essential in enterprise software development.
Account
SavingsAccount
CurrentAccount
LoanAccount
Payment
UPI
Card
Wallet
Person
Student
Teacher
Admin
Employee
Doctor
Nurse
Receptionist
Polymorphism helps manage multiple object types efficiently.
Without virtual, overriding is not possible.
Prefer overriding whenever appropriate.
Base references are key to Runtime Polymorphism.
Keep inheritance structures simple.
Polymorphism in C# is heavily used in:
A strong understanding of Polymorphism in C# is necessary for advanced Object-Oriented Programming and professional .NET development.
Polymorphism allows the same interface or method name to behave differently for different objects.
Compile-Time Polymorphism and Runtime Polymorphism.
Method Overloading allows multiple methods with the same name but different parameter lists.
Method Overriding allows child classes to replace parent class behavior.
The virtual keyword allows a method to be overridden in derived classes.
Polymorphism improves flexibility, maintainability, scalability, and code reusability.
WhatsApp us