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.

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.

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:
- class Program
- {
- void show_method(int a, int b)
- {
- Console.WriteLine("addition of 2 numbers : " + a + b);
- }
- void show_method(string str1, string str2)
- {
- Console.WriteLine("addition of 2 numbers : " + str1 + str2);
- }
- static void Main(string[] args)
- {
- Program obj = new Program();
- obj.show_method(10, 20);
- obj.show_method("Rupesh ", "Kahane");
- Console.ReadLine();
- }
- }

Example
We can’t overload a function with the help of default argument.
- class Rules
- {
- void gun(int k)
- {
- // code
- }
- void gun(int k = 20)
- {
- // code
- }
- static void Main(string[] args)
- {
- Rules obj = new Rules();
- obj.gun(10);
- }
- }
We can’t overload a function with the help of different return type & same parameter list.
- Class Rules
- {
- // can not possible with its different return type & same parameter list
- int fun(int a, int b)
- {
- return a + b;
- }
- // can not possible with its different return type & same parameter list
- float fun(int x, int y)
- {
- return x + y;
- }
- static void Main(string[] args)
- {
- Rules obj = new Rules();
- obj.fun(10, 20);
- obj.fun(20, 30);
- Console.ReadLine();
- }
- }




Rupesh KahanePosted Feb 6, 2016, 1:18 PM
thanks a lot Nilesh Dhande
Nilesh DhandePosted Jan 26, 2016, 11:56 AM
Very easy to understand.nice rupesh
Rupesh KahanePosted Dec 4, 2015, 2:31 PM
Thanks kalu singh rao
kalu singh raoPosted Dec 4, 2015, 10:40 AM
Great one
Rupesh KahanePosted Dec 3, 2015, 10:47 PM
Thanks sreenivasa k
sreenivasa kPosted Dec 3, 2015, 4:56 PM
nice example
Rupesh KahanePosted Dec 1, 2015, 4:00 AM
Thanks Ajay Kadamj
Ajay KadamPosted Dec 1, 2015, 2:38 AM
great simple and excellent keep it up..
Rupesh KahanePosted Nov 11, 2015, 8:35 AM
Thanks to Sibeesh Venu & Santhakumar Munuswamy also
Santhakumar MunuswamyPosted Nov 11, 2015, 4:08 AM
Good one
Sibeesh VenuPosted Nov 11, 2015, 12:54 AM
Nice share
Rupesh KahanePosted Nov 10, 2015, 8:56 PM
Vincent Maverick Durano thanks a lot for your great suggestion
Vincent Maverick DuranoPosted Nov 10, 2015, 3:45 PM
great job! One thing you got a typo on your taxi image. I suppose it should be "Hello Rupesh I will be there" :)
Rupesh KahanePosted Nov 10, 2015, 9:08 AM
Thanks Davis Sylvester
Davis SylvesterPosted Nov 10, 2015, 7:21 AM
Nice tutorial
Rupesh KahanePosted Nov 10, 2015, 1:30 AM
Thanks to Humayun Kabir Mamun and Mukesh Kumar
Rupesh KahanePosted Nov 10, 2015, 1:28 AM
Ajay Gandhi thanks
Humayun Kabir MamunPosted Nov 10, 2015, 12:22 AM
Nice...
Mukesh KumarPosted Nov 10, 2015, 12:14 AM
Good Job
Ajay GandhiPosted Nov 9, 2015, 11:11 PM
Nice
Rupesh KahanePosted Nov 9, 2015, 10:13 PM
Thanks Talha Bin Afzal
Talha Bin AfzalPosted Nov 9, 2015, 12:42 PM
Great Explanation
Rupesh KahanePosted Nov 9, 2015, 12:11 PM
thanks Gowtham K
Gowtham KPosted Nov 9, 2015, 12:10 PM
Good one
Rupesh KahanePosted Nov 9, 2015, 10:00 AM
Thanks Rajeesh Menoth
Rajeesh MenothPosted Nov 9, 2015, 7:31 AM
Good Explanation!