1 / 4
2 / 4
3 / 4
4 / 4

Wednesday, 9 July 2025

Understanding Polymorphism in Object-Oriented Programming with Example

In Object-Oriented Programming (OOP), polymorphism is one of the four core principles (along with inheritance, encapsulation, and abstraction). In C#, polymorphism allows you to treat different types of objects in the same way, which makes your code more flexible and extensible.

Let’s break it down simply and see how it works in real-world applications and in actual C# code.





What is polymorphism?

"Poly" means "many."

"Morphism" comes from the Greek word "morphḗ" + "ism." The meaning of morphḗ is "form/shape."

There are two main types of polymorphism in C#:

  1. Compile-Time Polymorphism (Static) – Achieved through method overloading.

  2. Run-Time Polymorphism (Dynamic) – Achieved through method overriding using inheritance.

Compile-time polymorphism (static polymorphism):
This type of polymorphism is resolved at compile time. This includes method overloading, where a class can have multiple methods with the same name but different parameter lists. The correct way is selected during compilation based on the number or type of arguments.

Runtime Polymorphism (Dynamic Polymorphism):
The resolution of runtime polymorphism is done at runtime. This can be done through method overriding, where a subclass provides a specific implementation for a method already defined in its superclass. The proper method is determined during runtime based on the actual type of the object.

Advantages of Polymorphism:

Polymorphism provides several benefits in software development:

Code Reusability: Polymorphism allows us to write generic code that can work with different types of objects, promoting code reusability and reducing redundant code.

Flexibility and extensibility: With polymorphism, new classes can be added without affecting the existing codebase. It promotes a flexible and easily extendable architecture.

Maintainability and Scalability: Polymorphic code is easy to maintain and update, making it ideal for large-scale applications.


 Real-Time Example of Polymorphism

Imagine you run a payment processing system. You have different types of payment methods: CreditCard, PayPal, and UPI. Each payment method processes the payment differently, but from the user's perspective, they just call a MakePayment() function.

This is polymorphism: different classes implement the same method in their own way.

Coding Example in C# 

Compile-Time Polymorphism (Method Overloading)

public class Calculator
{
    public int Add(int a, int b) => a + b;

    public double Add(double a, double b) => a + b;
}

 Run-Time Polymorphism

using System;


namespace PolymorphismExample

{

    // Base class

    public class Payment

    {

        public virtual void MakePayment()

        {

            Console.WriteLine("Processing generic payment...");

        }

    }


    // Derived class 1

    public class CreditCard : Payment

    {

        public override void MakePayment()

        {

            Console.WriteLine("Processing payment through Credit Card.");

        }

    }


    // Derived class 2

    public class PayPal : Payment

    {

        public override void MakePayment()

        {

            Console.WriteLine("Processing payment through PayPal.");

        }

    }


    // Derived class 3

    public class UPI : Payment

    {

        public override void MakePayment()

        {

            Console.WriteLine("Processing payment through UPI.");

        }

    }


    class Program

    {

        static void Main(string[] args)

        {

            Payment[] payments = new Payment[]

            {

                new CreditCard(),

                new PayPal(),

                new UPI()

            };


            foreach (Payment payment in payments)

            {

                payment.MakePayment();  // Calls the appropriate method depending on object type

            }

        }

    }

}

Output:-
Processing payment through Credit Card.
Processing payment through PayPal.
Processing payment through UPI.

How It Works

  • The base class Payment defines a virtual method MakePayment().

  • Derived classes override the method with their own specific implementations.

  • At runtime, C# decides which MakePayment()method to call based on the object type — this is run-time polymorphism.


Why Use Polymorphism?

  • Makes code more maintainable and scalable

  • Encourages code reuse through inheritance

  • Helps you follow SOLID principles, especially the Open/Closed Principle

  • Useful when working with interfaces or base classes to handle a variety of behaviors



Conclusion:

Polymorphism is a key concept in object-oriented programming that promotes code flexibility, reusability, and maintainability. By adopting polymorphism, developers can create elegant and scalable solutions that adapt to changing needs and future expansion. Understanding this powerful concept opens the door to more sophisticated software design, making it an essential skill for every OOP developer.



No comments:

Post a Comment

If you have any doubts please let me know