Skip to main content

Command Palette

Search for a command to run...

Polymorphism in C#

Updated
3 min read
Polymorphism in C#

Polymorphism is a fundamental concept in object-oriented programming that allows different objects to be treated as if they were the same type. C# provides support for polymorphism through inheritance and interfaces.

In this article, we will explore what polymorphism is, how it works in C# and some examples of how it can be used in your code.

What is Polymorphism?

Polymorphism is the ability of an object to take on many forms. In object-oriented programming, this means that an object can be treated as if it is an instance of its class or any of its parent classes or interfaces. This allows for code reusability and flexibility, as objects can be used in different contexts without writing new code.

In C#, polymorphism is achieved through inheritance and interfaces. Inheritance allows a class to inherit properties and methods from its parent class, while interfaces define a contract that classes must adhere to. This means that any class that implements an interface can be treated as an instance of that interface.

C# Polymorphism

Polymorphism in C# is achieved through two mechanisms: inheritance and interfaces. Inheritance is used to create a hierarchy of classes, with each class inheriting properties and methods from its parent class. Interfaces define a contract that a class must adhere to, and any class that implements the interface can be treated as an instance of that interface.

Let’s take a look at an example of polymorphism using inheritance:

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

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat says meow");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog says woof");
    }
}

In this example, we have a base class called Animal with a virtual method called MakeSound(). We also have two derived classes, Cat and Dog, that override the MakeSound() method to provide their implementation. This means that we can treat both Cat and Dog objects as if they were instances of the Animal class.

Animal myAnimal = new Animal();
myAnimal.MakeSound(); // The animal makes a sound

Animal myCat = new Cat();
myCat.MakeSound(); // The cat says meow

Animal myDog = new Dog();
myDog.MakeSound(); // The dog says woof

We create instances of the Animal, Cat, and Dog classes and call the MakeSound() method on each object. As you can see, the implementation of the MakeSound() method depends on the type of object that is being used.

Let’s take a look at another example of polymorphism using 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");
    }
}

We have an interface called IShape with a method called Draw(). We also have two classes, Circle and Square, which implement the IShape interface and provide their implementation of the Draw() method.

IShape myCircle = new Circle();
myCircle.Draw(); // Drawing a circle

IShape mySquare = new Square();
mySquare.Draw(); // Drawing a square

In this example, we create instances of the Circle and Square classes and assign them to variables of type IShape. We then call the Draw() method on each object, which invokes the implementation of the Draw() method for the respective class.

Benefits of Polymorphism

Polymorphism in C# provides several benefits, including:

  1. Code reusability: Polymorphism allows for code to be reused in different contexts, reducing the amount of code that needs to be written.

  2. Flexibility: Polymorphism allows for objects to be used in different contexts, making code more flexible and adaptable.

  3. Simplified code: Polymorphism allows for code to be written in a more abstract and general way, making it simpler and easier to understand.

  4. Improved maintainability: Polymorphism makes it easier to make changes to code without affecting other parts of the program, improving maintainability.

More from this blog

G

Guillermo Valenzuela's blog

16 posts

Systems Engineer | .Net Developer | Content Creator | C# | .NET Core