Polymorphism In C# With Real Life Example

Polymorphism

Polymorphism is a way in which we can define multiple functions in a single name i.e. single name and multiple meaning.

Single name & multiple meaning

Polymorphism means assigning a single name but there can be multiple behaviors. It means the name of the function is same but its definitions are different.

Polymorphism means single name & multiple meaning & whenever we try to access that particular name there must be a binding between that call & between that function. So there are two types of binding to call that function.

  • Compile time binding
  • Run time binding

Compile Time Polymorphism

In case of this Polymorphism, function call & its definition bind at compile time.

There are two ways to achieve this Polymorphism:

  • Function Overloading
  • Operator Overloading

Run Time Polymorphism

In case of this Polymorphism, function call & its definition bind at run time. We can achieve this type of Polymorphism using Virtual concept. This type of Polymorphism is also called Late Binding.

Real life example

Suppose in this week I have to go to Mumbai for C#Corner Mumbai Chapter Meet on Nov 14, 2015. I have total 2 mobile numbers of cab drivers. So out of that, I called Sachin & told him we have to go Mumbai on Nov 14, at 5.00 AM. He told that he will come on sharply at 5.00 AM & will go. So, for this week I will come & attend the seminar.

Real life example

Now in the next month, I have to go to Delhi. So again I called Sachin & asked him whether he is free or not. He told me that he is having another tour so he is not free in the coming month. He gave me his associate office contact number & told that you can call them they will arrange another cab for you. So I called their office & told them the date time & my house address for pick up. The call attendee (office receptionist boy) asked me which cab do you need, he told different rates as per the cab facilities, such AC, Non-AC. After taking complete information from me he told that our drive will come on time at your home.

Real life example

In the first situation, I know that Sachin will definitely come i.e. Compile Time Binding. In second situation, I don’t know who will come (I don’t know the name of THE person) i.e. Run Time Binding.

Function Overloading

Function overloading means writing a function with the same name, but different definitions. Whenever we call this function we can call by using the same name.

Example

Simple function overloading example:

  1. class Program  
  2. {  
  3.     void show_method(int a, int b)  
  4.     {  
  5.         Console.WriteLine("addition of 2 numbers : " + a + b);  
  6.     }  
  7.     void show_method(string str1, string str2)  
  8.     {  
  9.         Console.WriteLine("addition of 2 numbers : " + str1 + str2);  
  10.     }  
  11.     static void Main(string[] args)  
  12.     {  
  13.         Program obj = new Program();  
  14.         obj.show_method(10, 20);  
  15.         obj.show_method("Rupesh ""Kahane");  
  16.         Console.ReadLine();  
  17.     }  
  18. }  
Output

output

Example

We can’t overload a function with the help of default argument.
  1. class Rules  
  2. {  
  3.     void gun(int k)  
  4.     {  
  5.         // code  
  6.     }  
  7.     void gun(int k = 20)  
  8.     {  
  9.         // code  
  10.     }  
  11.     static void Main(string[] args)  
  12.     {  
  13.         Rules obj = new Rules();  
  14.         obj.gun(10);  
  15.     }  
  16. }  
Example:

We can’t overload a function with the help of different return type & same parameter list.
  1. Class Rules  
  2. {  
  3.     // can not possible with its different return type & same parameter list  
  4.     int fun(int a, int b)  
  5.         {  
  6.             return a + b;  
  7.         }  
  8.         // can not possible with its different return type & same parameter list  
  9.     float fun(int x, int y)  
  10.     {  
  11.         return x + y;  
  12.     }  
  13.     static void Main(string[] args)  
  14.     {  
  15.         Rules obj = new Rules();  
  16.         obj.fun(10, 20);  
  17.         obj.fun(20, 30);  
  18.         Console.ReadLine();  
  19.     }  
  20. }  
Example

We can’t pass constant value directly to function:
  1. class Qulifiers  
  2. {  
  3.     // co not pass constant value directly to function  
  4.     void fun(const int i)  
  5.     {  
  6.         // code  
  7.     }  
  8.     void fun(int i)  
  9.     {  
  10.         // code  
  11.     }  
  12.     static void Main(string[] args)  
  13.     {  
  14.         Qulifiers obj = new Qulifiers();  
  15.         obj.fun(20);  
  16.         obj.fun(40);  
  17.     }  
  18. }  

 Roles of params parameters in Polymorphism

  1. Pass by value
  2. Pass by reference
  3. As an output parameter
  4. Using parameter arrays

Example Pass by reference

  1. class Roles_of_Parameters  
  2. {  
  3.     private string name = "Rupesh";  
  4.     public void Display_name()  
  5.     {  
  6.         Display_name(ref name);  
  7.         Console.WriteLine(name);  
  8.     }  
  9.     public void Display_name(ref string x)  
  10.     {  
  11.         Console.WriteLine(name);  
  12.         name = "Rupesh Kahane";  
  13.         Console.WriteLine(name);  
  14.     }  
  15.     static void Main(string[] args)  
  16.     {  
  17.         Roles_of_Parameters obj = new Roles_of_Parameters();  
  18.         obj.Display_name();  
  19.         Console.ReadLine();  
  20.     }  
  21. }  
Output

output

Example Using Parameter Array
  1. class Role_of_parameter_Using_Array  
  2. {  
  3.     private string str = "The Sun Infosystems";  
  4.     public void Display()  
  5.     {  
  6.         Console.WriteLine("You are in Display method");  
  7.         Display(100, "Rupesh");  
  8.         Console.WriteLine("------");  
  9.         Display(200, "Rupesh""Ajit");  
  10.         Console.WriteLine("------");  
  11.         Display(300, "Rupesh""Vaibhav""Sam");  
  12.     }  
  13.     public void Display(int a, params string[] parameterArray)  
  14.     {  
  15.         foreach(string str in parameterArray)  
  16.         Console.WriteLine(str + " " + a);  
  17.     }  
  18.     static void Main(string[] args)  
  19.     {  
  20.         Role_of_parameter_Using_Array obj = new Role_of_parameter_Using_Array();  
  21.         obj.Display();  
  22.         Console.ReadLine();  
  23.     }  
  24. }  
Output

output

Interview Questions
  1. Can we use two dimensional array or multidimensional array as a parameter?

    Function Redefinition
    1. class Function_Redefinition  
    2. {  
    3.     void fun(int i)  
    4.     {  
    5.         // code  
    6.     }  
    7. }  
    8. class Derived_red_fun: Function_Redefinition  
    9.     {  
    10.         void fun(int i, int j)  
    11.         {  
    12.             // code  
    13.         }  
    14.     }  
    15.     // in above example it is not a function overloading it is redefinition of function in derived class.  

Run Time Polymorphism

Example

  1. class Virtual_Example  
  2. {  
  3.     public class Base  
  4.     {  
  5.         public virtual void show()  
  6.         {  
  7.             Console.WriteLine("You are in base class");  
  8.         }  
  9.     }  
  10.     public class Derived: Base  
  11.     {  
  12.         public override void show()  
  13.         {  
  14.             Console.WriteLine("You are in derived class");  
  15.         }  
  16.     }  
  17.     static void Main(string[] args)  
  18.     {  
  19.         Base obj1 = new Base();  
  20.         obj1.show();  
  21.         Derived obj = new Derived();  
  22.         obj.show();  
  23.         Console.ReadLine();  
  24.     }  
  25. }  
Output

output

For more understanding you can download the code

Summary

This article will help fresher candidates to understand Polymorphism in C# with real life example.


Similar Articles