What is polymorphism in C#

What is polymorphism and how to implement polymorphism


Category: C# Tags: C#


Polymorphism

    If any entity is appearing multiple times in different forms is called polymorphism. For example Carbon(c), carbon present in many forms in our environment, like coal, diamond, graphite etc.

Types of polymorphism :-

  1. Static polymorphism or early binding or compile time polymorphism.
  2. Dynamic polymorphism or late binding or run time polymorphism.

1. Static polymorphism or early binding or compile time polymorphism: This is called overloading. This happens in same class, where we create more then one method with same name but different signature.

Signature: is combination of method name and parameter list (No. of parameters and sequence of parameters and types of parameters).

Ex :-

public class A
{
   public int M()
   {
      return 0;
   }
   public int M(int x)
   {
      return ++x;
   }
   public int M(int x, int y)
   { 
      return x+y;
   }
}

    In above example we have 3 overloads of a function M. that all are in same class. Some rules for overloading:

  • All members should be in same class.
  • All members should have different signature.

2. Dynamic polymorphism or late binding or run time polymorphism :- This is called overriding. Happens in different inherited classes. Method name and signature both should be same.

Ex :-

public class A
{
   public virtual int M(int x)
   {
      return ++x;
   }
}
public class B:A { public override int M(int x) { return x+x; } }

Virtual: is a keyword that makes a method as virtual, means that function can be overridden in inherited classes. we declare a method as virtual when we have possibilities to change method's functionality in future.

Override: is a keyword that gives the functionality to a method to get overridden from a virtual method and can change functionality.

        In above example we have two function with same name and same signature but fictionally different. Some rules for overriding:

  • Method name and signature both should be same.
  • Should followed by keywords virtual and override.
  • Should be in inherited and different classes.

Why overloading is static and overriding is dynamic :-

        When C# compiler compiles the code it creates a method table where method name and its signature is stored. Because we call overloaded methods using signature(compiler can differentiate those same name methods using their different parameter list) so compiler knows at compile time  which method has to be called. But in overriding both method name and method parameters are same so compiler doesn't know at the compile time which method has to be called(can not differentiate) so it decides that at the run time.


Let's see impacts of overloading and overriding when executing the code(using above classes) :-
Overloading(using 1st example in Page_Load event) :-

    protected void Page_Load(object sender, EventArgs e)
    {
        A a = new A();
        int x = a.M();
        Response.Write(x + "<br />");
        x = a.M(10);
        Response.Write(x + "<br />");
        x = a.M(10, 20);
        Response.Write(x);
    }

Output :-
0
11
30

Overriding(using 2nd example in Page_Load event) :-

    protected void Page_Load(object sender, EventArgs e)
    {
        A a = new A();
        int x = a.M(10);
        Response.Write(x + "<br />");
        B b = new B();
        x = b.M(10);
        Response.Write(x + "<br />");
        a = b;                         // downcasting
        x = a.M(10);
        Response.Write(x);
    }

Output :-
11
20
20

     Even after downcasting call will go to overridden method(if there are more then one override then call will go to last overridden).


Like 0 People
Last modified on 2 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

alex
Very good and clean article. Stilkl here I prefer Java, It extends class with intuitive word extends not the : symbol. I don't like so much this one approach, but someone pure Computer scientist would be angry with me :D
Like 1 Person on 29 March 2014

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x