Skip to Content
All posts

Polymorphism In C#

2 min read ·  — #csharp-interview#junior#oop

Polymorphism In C#

Polymorphism, derived from the Greek words 'poly' (many) and 'morphs' (forms), is one of the four fundamental principles of OOP, the other three being inheritance, encapsulation, and abstraction.

At a basic level, polymorphism allows objects of different types to be treated as objects of a common, super type. This enables us to write more flexible and reusable code.

Polymorphism can be categorized into two types in C#:

  • Static/Compile-time Polymorphism
  • Dynamic/Run-time Polymorphism

Static/Compile-time Polymorphism

This type of polymorphism is checked during compile-time. There are two ways to achieve compile-time polymorphism:

Method Overloading: This is when multiple methods have the same name but different parameters (differing in type, number, or order of types). When you call an overloaded method, the compiler determines the most appropriate method to use by comparing the argument types you supplied with the parameter types declared in the definitions of the potential method to call.

Operator Overloading: Similar to method overloading, operator overloading allows an operator to have different meanings based on the context. This means you can redefine or overload most of the built-in operators available in C#.

Dynamic/Run-time Polymorphism

Run-time polymorphism is not decided until the program is running. The two main ways to achieve this in C# are:

Method Overriding: This is when a child class has the same method as declared in the parent class. Here, the method in the child class will override the method in the parent class. This requires the use of two keywords: virtual (in the base class method) and override (in the derived class method).

Abstract Classes and Methods: An abstract class cannot be instantiated and it's often used as a base class. It can contain abstract methods, which are declared in an abstract class and don't have a body within the base class. They must be implemented in any non-abstract class that directly inherits the abstract base class.

Example

Here is an example of polymorphism:


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

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

public class Dog : Animal
{
    public override void animalSound()
    {
        Console.WriteLine("The dog says: bow wow");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myAnimal = new Animal();
        Animal myPig = new Pig();
        Animal myDog = new Dog();

        myAnimal.animalSound();
        myPig.animalSound();
        myDog.animalSound();
    }
}

When you run this code, you'll see that each of the different classes provides its own implementation of the animalSound() method. When the method is called, the runtime system determines the type of the object and then calls the method that's specific to that type of object.

In summary, understanding and properly leveraging polymorphism is essential in creating flexible, reusable, and organized code in C#. It allows classes to be used interchangeably, reducing complexity and increasing efficiency in your code.

Reference