Polymorphism
If an entity is appearing with the same name in a given context in the various formats then the entity is said to be exhibiting polymorphism.
Types of Polymorphism
Polymorphism can be categorised into the following 2 types:
- Static polymorphism (or) compile time polymorphism (or) Early Binding
- Dynamic polymorphism (or) runtime polymorphism (or) Late Binding
Examples of Polymorphism
- Overloading is the best example of static polymorphism.
- Overriding is the best example of dynamic polymorphism.
Overloading
Overloading can be done using methods and constructors.
Method Overloading: If a method has the same name and with multile signatures in various formats then the method is said to be overloaded ( Method Signature is a combination of method name and the parameter types).
Method Overloading sample code:
- public class Test
- {
- public static void Main(string[] args)
- {
- A a = new A();
- int r1= a.M(10, 20, 30);
- int r2 = a.M(10, 20);
- }
- }
- public class A
- {
- public int M(int x, int y)
- {
- return x + y;
- }
- public int M(int x, int y, int z)
- {
- return x + y + z;
- }
- }

During execution of a.M(10,20,30) the CLR will directly load the second method whose Mid=2. Since the C# compiler added a clear instruction to the CLR by adding a method Id value immediately after the code, the CLR will directly load the second method that has the Mid value equal to 2 into RAM for executing the code without any confusion. Since the C# compiler is giving clear instructions to the CLR about the method call, this type of polymorphism is called static polymorphism or Early Binding.
Gowtham RajamanickamPosted May 20, 2015, 1:25 AM
good one