Polymorphism in C#
Introduction
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows methods to do different things based on the object it is acting upon. In C#, polymorphism is primarily achieved through method overriding and interfaces. It enables a single interface to be used for a general class of actions, making it easier to manage and extend code.
Types of Polymorphism
There are two main types of polymorphism in C#:
- Compile-Time Polymorphism (Static Binding): Achieved through method overloading and operator overloading.
- Run-Time Polymorphism (Dynamic Binding): Achieved through method overriding using inheritance and interfaces.
Compile-Time Polymorphism
Compile-time polymorphism is achieved through method overloading. Method overloading allows multiple methods in the same class to have the same name but different parameters.
// Example of Method Overloading
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(5, 3)); // Output: 8
Console.WriteLine(calc.Add(5.2, 3.3)); // Output: 8.5
}
}
Run-Time Polymorphism
Run-time polymorphism is achieved by method overriding. This allows a subclass to provide a specific implementation of a method that is already defined in its base class.
// Example of Method Overriding
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal makes a sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Output: Dog barks
}
}
Polymorphism with Interfaces
Interfaces in C# provide a way to achieve polymorphism. When a class implements an interface, it agrees to provide implementations for all of the interface's methods. This allows the same method to behave differently based on the object that invokes it.
// Example with Interfaces
public interface IShape
{
void Draw();
}
public class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Circle");
}
}
public class Square : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Square");
}
}
class Program
{
static void Main()
{
IShape shape = new Circle();
shape.Draw(); // Output: Drawing a Circle
shape = new Square();
shape.Draw(); // Output: Drawing a Square
}
}
Benefits of Polymorphism
Polymorphism provides several benefits in software development:
- Code Reusability: Write more generic and reusable code.
- Maintainability: Easier to manage and extend the code.
- Flexibility and Scalability: Ability to introduce new functionalities with minimal changes.
Conclusion
Polymorphism is a powerful concept in C# that enhances the flexibility and maintainability of code. By understanding and utilizing both compile-time and run-time polymorphism, developers can write more efficient and scalable applications.