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();
- }
- }
Output
Example
We can’t overload a function with the help of default argument.
- class Rules
- {
- void gun(int k)
- {
-
- }
- void gun(int k = 20)
- {
-
- }
- static void Main(string[] args)
- {
- Rules obj = new Rules();
- obj.gun(10);
- }
- }
Example:
We can’t overload a function with the help of different return type & same parameter list.
- Class Rules
- {
-
- int fun(int a, int b)
- {
- return a + b;
- }
-
- 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();
- }
- }
Example
We can’t pass constant value directly to function:
- class Qulifiers
- {
-
- void fun(const int i)
- {
-
- }
- void fun(int i)
- {
-
- }
- static void Main(string[] args)
- {
- Qulifiers obj = new Qulifiers();
- obj.fun(20);
- obj.fun(40);
- }
- }
Roles of params parameters in Polymorphism
- Pass by value
- Pass by reference
- As an output parameter
- Using parameter arrays
Example Pass by reference
- class Roles_of_Parameters
- {
- private string name = "Rupesh";
- public void Display_name()
- {
- Display_name(ref name);
- Console.WriteLine(name);
- }
- public void Display_name(ref string x)
- {
- Console.WriteLine(name);
- name = "Rupesh Kahane";
- Console.WriteLine(name);
- }
- static void Main(string[] args)
- {
- Roles_of_Parameters obj = new Roles_of_Parameters();
- obj.Display_name();
- Console.ReadLine();
- }
- }
Output
Example Using Parameter Array
- class Role_of_parameter_Using_Array
- {
- private string str = "The Sun Infosystems";
- public void Display()
- {
- Console.WriteLine("You are in Display method");
- Display(100, "Rupesh");
- Console.WriteLine("------");
- Display(200, "Rupesh", "Ajit");
- Console.WriteLine("------");
- Display(300, "Rupesh", "Vaibhav", "Sam");
- }
- public void Display(int a, params string[] parameterArray)
- {
- foreach(string str in parameterArray)
- Console.WriteLine(str + " " + a);
- }
- static void Main(string[] args)
- {
- Role_of_parameter_Using_Array obj = new Role_of_parameter_Using_Array();
- obj.Display();
- Console.ReadLine();
- }
- }
Output
Interview Questions - Can we use two dimensional array or multidimensional array as a parameter?
Function Redefinition
- class Function_Redefinition
- {
- void fun(int i)
- {
-
- }
- }
- class Derived_red_fun: Function_Redefinition
- {
- void fun(int i, int j)
- {
-
- }
- }
-
Run Time Polymorphism
Example
- class Virtual_Example
- {
- public class Base
- {
- public virtual void show()
- {
- Console.WriteLine("You are in base class");
- }
- }
- public class Derived: Base
- {
- public override void show()
- {
- Console.WriteLine("You are in derived class");
- }
- }
- static void Main(string[] args)
- {
- Base obj1 = new Base();
- obj1.show();
- Derived obj = new Derived();
- obj.show();
- Console.ReadLine();
- }
- }
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.