24 Oct 2023
The word "polymorphism" came from two Greek words: 'poly' meaning "many," and 'morphos' meaning "forms."
Polymorphism is a key concept in object-oriented programming (OOP) that allows objects to take on different forms or behave in different ways depending on the context in which they are used. This is achieved through the use of inheritance, interfaces, and method overriding.
Polymorphism in programming comes in two main types:
- Compile-time (Static) Polymorphism:
- Method Overloading: In compile-time polymorphism, it is achieved through method overloading. Method overloading allows you to define multiple methods in the same class with the same name but different parameters (number, type, or order).
- Compile-Time Resolution: The appropriate method to be called is determined at compile time by the compiler, based on the number and types of arguments provided in the method call.
- Example: If you have multiple methods with the same name but different parameter lists, the compiler decides which method to invoke based on the method call's arguments. For example, you might have different
printmethods for different data types likeint,float, andstring.
using System;
class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
MathOperations math = new MathOperations();
int result1 = math.Add(5, 7); // Calls the int version of Add
double result2 = math.Add(3.5, 2.3); // Calls the double version of Add
Console.WriteLine($"Result 1: {result1}");
Console.WriteLine($"Result 2: {result2}");
}
}
In the above example, we have two methods named Add in the MathOperations class with different parameter types (int and double). The appropriate method is determined at compile time based on the argument types when calling the Add method.
- Runtime (Dynamic) Polymorphism:
- Method Overriding: In runtime polymorphism, it is achieved through method overriding. Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
- Dynamic Binding: The method to be executed is determined during runtime based on the actual object's type, not at compile time. This is also known as dynamic binding or late binding.
-Example: Suppose you have a base class
Shapewith a methodcalculateArea(). Subclasses likeCircleandRectanglecan override thecalculateArea()method to provide their own specific implementations. When you callcalculateArea()on an object, the specific implementation associated with the object's class is executed.
using System;
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
class Program
{
static void Main(string[] args)
{
Shape shape = new Circle();
shape.Draw(); // Calls the Draw method of the Circle class at runtime
shape = new Shape();
shape.Draw(); // Calls the Draw method of the Shape class at runtime
}
}
In this example, we have a base class Shape with a virtual method Draw, which is overridden in the derived class Circle. At runtime, the appropriate Draw method is called based on the actual object's type, demonstrating dynamic polymorphism.
In summary, compile-time (static) polymorphism is about method overloading and method resolution at compile time, while runtime (dynamic) polymorphism is about method overriding and late binding, with the method to be executed being determined at runtime based on the object's actual type. These concepts are essential in object-oriented programming for achieving flexibility and extensibility in your code.