Polymorphism in C#: Exploring Dynamic and Static Polymorphism

Introduction

Greetings! In today's lesson, we'll unravel the concept of polymorphism in C#'s Object-Oriented Programming (OOP). Grasping polymorphism enables us to use a single entity (a method, class, or interface) to represent different types in various scenarios. Let's proceed.

Seeing Polymorphism in Action

Polymorphism, a pillar of OOP, allows one object to embody multiple forms. Visualize a button in software; depending on its type (for instance, a submit button or a radio button), the action resulting from pressing it varies. This dynamic encapsulates the spirit of polymorphism!

C# supports various types of polymorphism. Run-time polymorphism, also known as dynamic polymorphism, occurs during runtime and leverages method overriding. Let's observe dynamic polymorphism in action within a simple application involving shapes. The base Shape class has an Area method, which calculates the area for shapes. This method is uniquely implemented in the subclasses Rectangle and Circle.

C#
using System;

abstract class Shape
{
    public abstract double Area();
}

class Rectangle : Shape
{
    private double length;
    private double width;

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public override double Area()
    {
        return length * width;
    }
}

class Circle : Shape
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public override double Area()
    {
        return Math.PI * radius * radius;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Shape rectangle = new Rectangle(2, 3);
        Console.WriteLine(rectangle.Area()); // Prints: 6.0

        Shape circle = new Circle(5);
        Console.WriteLine(circle.Area()); // Prints: 78.53981633974483
    }
}

Here, polymorphism shines as the Area() method takes on multiple forms while using the same Shape abstract class. It behaves differently depending on whether the object is a Rectangle or a Circle.

Method Overloading in C#: Static Polymorphism

The other type of polymorphism that C# supports is compile-time polymorphism, also known as static polymorphism, which operates during compile time and involves method overloading. Let's look at the example of static polymorphism:

C#
using System;

class MathOperations
{
    // Overloaded method for rectangle area
    public double Area(double length, double width)
    {
        return length * width;
    }

    // Overloaded method for circle area
    public double Area(double radius)
    {
        return Math.PI * radius * radius;
    }
}

class Program
{
    static void Main(string[] args)
    {
        MathOperations mathOps = new MathOperations();
        Console.WriteLine(mathOps.Area(5, 2)); // Prints: 10
        Console.WriteLine(mathOps.Area(3));    // Prints: 28.27
    }
}

In this example, polymorphism is demonstrated via method overloading. The Area method has multiple forms, accepting different parameter lists to compute the area of either a rectangle or a circle based on the provided arguments. If one argument is provided, the area method for the circle is called; if two arguments are provided, the area method for the rectangle is called.

This is a clear example of static polymorphism, where the correct method is determined at compile time.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal