Polymorphism in C# Programming


Introduction

Polymorphism is one of the key concepts of object-oriented programming (OOP). It allows objects of different types to be treated as objects of a common base type. In C#, polymorphism enables the same method or operation to behave differently based on the object that invokes it.

There are two main types of polymorphism in C#:

  • Compile-time polymorphism (Method Overloading)
  • Runtime polymorphism (Method Overriding)

Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism, also known as method overloading, occurs when multiple methods with the same name exist in the same class but have different parameters (number, type, or order of parameters). The compiler determines which method to call at compile time based on the arguments passed to the method.

Syntax for Method Overloading

    class ClassName
    {
        public returnType MethodName(parameter1, parameter2) { }
        public returnType MethodName(parameter1, parameter2, parameter3) { }
    }
        

Example: Method Overloading

Let's define a class Calculator that has overloaded methods for adding numbers. One method will add two integers, while another will add two doubles.

    using System;
   class Calculator
    {
        // Method to add two integers
        public int Add(int a, int b)
        {
            return a + b;
        }
       // Overloaded method to add two doubles
        public double Add(double a, double b)
        {
            return a + b;
        }
    }
   class Program
    {
        static void Main()
        {
            Calculator calc = new Calculator();
           // Calling the Add method with integers
            Console.WriteLine("Sum of integers: " + calc.Add(10, 20));
           // Calling the Add method with doubles
            Console.WriteLine("Sum of doubles: " + calc.Add(10.5, 20.5));
        }
    }
        

Output:

    Sum of integers: 30
    Sum of doubles: 31
        

Runtime Polymorphism (Method Overriding)

Runtime polymorphism, also known as method overriding, occurs when a method in a base class is redefined (overridden) in a derived class. The decision on which method to call is made at runtime based on the type of object being referred to, rather than the type of reference variable. To enable method overriding, the base class method must be marked as virtual, and the derived class method must be marked as override.

Syntax for Method Overriding

    class BaseClass
    {
        public virtual returnType MethodName() { }
    }
   class DerivedClass : BaseClass
    {
        public override returnType MethodName() { }
    }
        

Example: Method Overriding

Let's define a base class Animal with a method MakeSound and a derived class Dog that overrides this method to provide its own implementation.

    using System;
   class Animal
    {
        // Virtual method
        public virtual void MakeSound()
        {
            Console.WriteLine("Animal makes a sound");
        }
    }
   class Dog : Animal
    {
        // Overriding the MakeSound method
        public override void MakeSound()
        {
            Console.WriteLine("Dog barks");
        }
    }
   class Program
    {
        static void Main()
        {
            Animal animal = new Animal();
            animal.MakeSound();  // Output: Animal makes a sound
           Dog dog = new Dog();
            dog.MakeSound();     // Output: Dog barks
           // Using a base class reference to point to a derived class object
            Animal myDog = new Dog();
            myDog.MakeSound();   // Output: Dog barks
        }
    }
        

Output:

    Animal makes a sound
    Dog barks
    Dog barks
        

Advantages of Polymorphism

  • Flexibility: Polymorphism allows for flexible and extensible code. New classes can be added without modifying existing code.
  • Code Reusability: With polymorphism, you can write more generic and reusable code. The same method name can be used to perform different operations depending on the object type.
  • Decoupling: It reduces the dependency between classes. The code that uses polymorphism is unaware of the concrete class it is working with.

Conclusion

In this tutorial, we covered the two main types of polymorphism in C#: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding). We also provided examples to demonstrate how both types of polymorphism work in practice:

  • Compile-time polymorphism: Method overloading allows you to define multiple methods with the same name but different parameters.
  • Runtime polymorphism: Method overriding allows a derived class to provide its own implementation of a method defined in a base class, with the method to be invoked determined at runtime.

Polymorphism is an essential feature of object-oriented programming, helping to create more flexible and reusable code. By understanding both compile-time and runtime polymorphism, you can design and implement more robust applications in C#.




Advertisement