Skip to main content

Understanding Polymorphism

Understanding Polymorphism in C#

Polymorphism is one of the key concepts in object-oriented programming (OOP). It allows us to treat objects of a derived class as objects of its base class. This provides a lot of flexibility and power, particularly when combined with other OOP concepts like inheritance and encapsulation. In C#, there are two types of polymorphism: static polymorphism and dynamic polymorphism.

Static Polymorphism

Static polymorphism, also known as compile-time polymorphism, is achieved through method overloading and operator overloading.

Method Overloading

Method overloading is a technique that allows you to define multiple methods with the same name but different parameter lists. The correct method will be determined at compile time based on the arguments you pass. Here's an example:

class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}

public double Add(double a, double b)
{
return a + b;
}
}

MathOperations op = new MathOperations();
Console.WriteLine(op.Add(2, 3)); // Output: 5
Console.WriteLine(op.Add(2.2, 3.3)); // Output: 5.5

In the above example, we have two Add methods. One takes two integers and the other takes two doubles. When we call Add, the correct method is chosen based on the types of arguments.

Operator Overloading

Operator overloading lets you redefine the way existing operators work with your objects. This is a powerful feature that can make your code more intuitive and easy to read.

public class Box
{
public int length;
public int breadth;

public static Box operator +(Box a, Box b)
{
Box box = new Box();
box.length = a.length + b.length;
box.breadth = a.breadth + b.breadth;
return box;
}
}

Box box1 = new Box();
Box box2 = new Box();
Box box3 = box1 + box2;

In the above example, we overloaded the + operator to add two Box objects together.

Dynamic Polymorphism

Dynamic polymorphism, also known as runtime polymorphism, is achieved through method overriding and virtual methods.

Method Overriding

Method overriding allows a subclass to provide a different implementation for a method that is already defined in its superclass.

public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}

public class Pig : Animal
{
public override void MakeSound()
{
Console.WriteLine("The pig says: wee wee");
}
}

Animal myAnimal = new Animal();
Animal myPig = new Pig();

myAnimal.MakeSound();
myPig.MakeSound();

In the above example, MakeSound is a virtual method in the Animal class and is overridden in the Pig class. When MakeSound is called, the version in the Pig class is used.

Conclusion

Polymorphism is a powerful concept in OOP that allows you to create more flexible and reusable code. It's an integral part of C# and is used extensively in .NET Framework. The key to mastering polymorphism, and OOP in general, is practice. Try to come up with your own examples and experiment with different scenarios. Happy coding!