Welcome to The Coding College! In this tutorial, we’ll explore Polymorphism in C#, a crucial aspect of Object-Oriented Programming (OOP) that allows methods to behave differently based on the object invoking them. This feature adds flexibility and enhances code reusability.
What is Polymorphism in C#?
Polymorphism means “many forms.” In C#, it enables objects of different types to be treated as objects of a common base type. It’s achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
Types of Polymorphism in C#:
- Compile-Time Polymorphism (Static Binding):
- Achieved using method overloading.
- Methods share the same name but differ in the number or type of parameters.
- Runtime Polymorphism (Dynamic Binding):
- Achieved using method overriding.
- The method that gets invoked is determined at runtime based on the object’s type.
1. Compile-Time Polymorphism (Method Overloading)
Method overloading allows you to define multiple methods with the same name but different parameter lists.
Example:
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
public class Program
{
public static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(3, 5)); // Calls Add(int, int)
Console.WriteLine(calc.Add(4.2, 5.3)); // Calls Add(double, double)
Console.WriteLine(calc.Add(1, 2, 3)); // Calls Add(int, int, int)
}
}
Output:
8
9.5
6
2. Runtime Polymorphism (Method Overriding)
Method overriding occurs when a derived class provides a specific implementation for a method already defined in its base class. This requires the virtual
keyword in the base class and the override
keyword in the derived class.
Example:
public class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal makes a sound.");
}
}
public class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog barks.");
}
}
public class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Cat meows.");
}
}
public class Program
{
public static void Main()
{
Animal myAnimal;
myAnimal = new Dog();
myAnimal.Speak(); // Dog barks.
myAnimal = new Cat();
myAnimal.Speak(); // Cat meows.
}
}
Output:
Dog barks.
Cat meows.
Abstract Classes and Polymorphism
Abstract classes are often used to define a common interface for a group of related classes. These classes may implement or override abstract methods to provide specific functionality.
Example:
public abstract class Shape
{
public abstract void Draw();
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Circle.");
}
}
public class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a Rectangle.");
}
}
public class Program
{
public static void Main()
{
Shape myShape;
myShape = new Circle();
myShape.Draw(); // Drawing a Circle.
myShape = new Rectangle();
myShape.Draw(); // Drawing a Rectangle.
}
}
Output:
Drawing a Circle.
Drawing a Rectangle.
Interface and Polymorphism
Interfaces provide another way to achieve polymorphism in C#. A class implementing an interface must provide specific implementations for all its methods.
Example:
public interface IVehicle
{
void Drive();
}
public class Car : IVehicle
{
public void Drive()
{
Console.WriteLine("Car is driving.");
}
}
public class Bike : IVehicle
{
public void Drive()
{
Console.WriteLine("Bike is riding.");
}
}
public class Program
{
public static void Main()
{
IVehicle vehicle;
vehicle = new Car();
vehicle.Drive(); // Car is driving.
vehicle = new Bike();
vehicle.Drive(); // Bike is riding.
}
}
Output:
Car is driving.
Bike is riding.
Advantages of Polymorphism
- Code Reusability: Share base class functionality across derived classes.
- Scalability: Add new functionality without altering existing code.
- Extensibility: Adapt behavior dynamically at runtime.
- Simplified Code: Write cleaner and more readable code.
Best Practices for Using Polymorphism
- Use method overriding only when behavior needs to change in the derived class.
- Avoid deep inheritance hierarchies; keep the class structure simple.
- Use interfaces for defining common behavior when multiple unrelated classes need to implement it.
- Leverage polymorphism for designing scalable and modular applications.
Conclusion
Polymorphism in C# is a powerful tool that adds flexibility and modularity to your applications. By understanding and implementing method overloading, method overriding, abstract classes, and interfaces, you can write cleaner and more efficient code.
To learn more about C# and programming concepts, visit The Coding College. Enhance your coding skills with our step-by-step tutorials designed for learners of all levels. Happy coding! 🚀