Blog Görseli

Exploring Polymorphism

30.01.2025 | Hasamuddin Afzali

Polymorphism is another fundamental concept of Object-Oriented Programming. It comes from the Greek words “poly” (meaning many) and “morph” (meaning form), so it literally means “many forms.”

In programming, polymorphism allows objects to take on multiple forms. More specifically, it allows one interface or method to be used for different data types or classes. In OOP, polymorphism is often implemented through method overriding and method overloading.

Types of Polymorphism:

  1. Static Polymorphism (Compile-Time Polymorphism): This occurs when the method or function is determined at compile time.
  • Method Overloading: This is the ability to define multiple methods with the same name but different parameters. The appropriate method is chosen during compilation based on the method’s signature.
public class Calculator
{
    // Overloaded method
    public int Add(int a, int b) 
    {
        return a + b;
    }
    
    public double Add(double a, double b)
    {
        return a + b;
    }
}

public class Program
{
    public static void Main()
    {
        Calculator calc = new Calculator();
        Console.WriteLine(calc.Add(3, 4));     // Output: 7
        Console.WriteLine(calc.Add(3.5, 4.2)); // Output: 7.7
    }
}

In this example, the Add() method is overloaded to handle both integers and floating-point numbers. The correct version of the method is selected based on the data types of the parameters at compile time.

2. Dynamic Polymorphism (Run-Time Polymorphism): This occurs when the method or function to be called is determined at runtime.

  • Method Overriding: In this case, a method in a base class is overridden by a method in a derived class. At runtime, the appropriate method from the derived class is invoked.
public class Animal
{
    // Base class method
    public virtual void Sound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    // Overriding the base class method
    public override void Sound()
    {
        Console.WriteLine("Bark");
    }
}

public class Program
{
    public static void Main()
    {
        Animal myAnimal = new Animal();
        myAnimal.Sound();  // Output: Some generic animal sound
        
        Animal myDog = new Dog();
        myDog.Sound();  // Output: Bark
    }
}

In this example:

  • The Sound() method is overridden in the Dog class.
  • Even though we have an Animal reference (myDog), the program will call the Sound() method from the Dog class during runtime because of dynamic polymorphism.

Why Polymorphism is Important:

  • Code Reusability: Polymorphism allows one function to handle multiple data types, reducing code duplication and increasing code reuse.
  • Extensibility: It allows new functionality to be added to existing systems without changing the existing code, making it more maintainable.
  • Flexibility: It provides the ability to use objects of different classes in the same way, increasing flexibility in your application design.