Introduction
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class rather than their actual class. Polymorphism enables a single interface to represent different underlying forms (data types). In C#, polymorphism is primarily achieved through method overriding and interfaces.
Types of Polymorphism
- Compile-Time Polymorphism (Method Overloading)
- Run-Time Polymorphism (Method Overriding)
1. Compile-Time Polymorphism (Method Overloading)
Compile-time polymorphism is achieved through method overloading and operator overloading. It is also known as static polymorphism.
Method Overloading
Method overloading allows multiple methods in the same class to have the same name but different parameters (number, type, or both).
Example
using System;
namespace PolymorphismExample
{
public class Calculator
{
// Method to add two integers
public int Add(int a, int b)
{
return a + b;
}
// Overloaded method to add three integers
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Overloaded method to add two doubles
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
Console.WriteLine("Sum of 5 and 3: " + calc.Add(5, 3));
Console.WriteLine("Sum of 5, 3, and 2: " + calc.Add(5, 3, 2));
Console.WriteLine("Sum of 5.5 and 3.3: " + calc.Add(5.5, 3.3));
}
}
}
Output
Sum of 5 and 3: 8
Sum of 5, 3, and 2: 10
Sum of 5.5 and 3.3: 8.8
2. Run-Time Polymorphism (Method Overriding)
Run-time polymorphism is achieved through method overriding. It allows a derived class to provide a specific implementation of a method that is already defined in its base class. This is also known as dynamic polymorphism.
Method Overriding
Method overriding requires the base class method to be marked with the virtual
keyword and the derived class method with the override
keyword.
Example
using System;
namespace PolymorphismExample
{
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.");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Cat meows.");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal;
myAnimal = new Dog();
myAnimal.MakeSound(); // Outputs: Dog barks.
myAnimal = new Cat();
myAnimal.MakeSound(); // Outputs: Cat meows.
}
}
}
Output
Dog barks.
Cat meows.
Using Interfaces for Polymorphism
Interfaces can also be used to achieve polymorphism. An interface defines a contract that classes can implement. Methods defined in an interface can be implemented differently by different classes.
Example
using System;
namespace PolymorphismExample
{
public interface IShape
{
void Draw();
}
public class Circle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Circle.");
}
}
public class Rectangle : IShape
{
public void Draw()
{
Console.WriteLine("Drawing a Rectangle.");
}
}
class Program
{
static void Main(string[] args)
{
IShape shape;
shape = new Circle();
shape.Draw(); // Outputs: Drawing a Circle.
shape = new Rectangle();
shape.Draw(); // Outputs: Drawing a Rectangle.
}
}
}
Output
Drawing a Circle.
Drawing a Rectangle.
Benefits of Polymorphism
- Flexibility: Polymorphism allows for flexibility and the ability to change behavior at runtime.
- Maintainability: Code is easier to maintain and extend because changes in the derived class do not affect the base class or other derived classes.
- Reusability: Code can be reused, reducing redundancy and increasing efficiency.
- Readability: Polymorphism enhances code readability by allowing you to use a consistent interface for different underlying forms.
Conclusion
Polymorphism in C# allows methods to perform different tasks based on the object that calls them. It can be achieved through method overloading (compile-time polymorphism) and method overriding (run-time polymorphism). Additionally, interfaces can be used to implement polymorphism, providing flexibility and enhancing code reusability and maintainability. Understanding polymorphism is crucial for designing robust and scalable object-oriented applications.