Polymorphism means one thing exists in many forms of its own with each form having different implementation of its own.
Polymorphism can be compile time or runtime.
Compile time polymorphism includes:-
1.) Function Overloading
2.) Operator Overloading
Function Overloading:-
Function overloading depends on:-
1.) Variable Number of parameters to function
2.) Order of parameters to function
3.) Data type of parameters
Function overloading doesn’t depend on:-
1.) Return type of function
2.) Name of parameters
We will consider various scenarios for function overloading in the next Code samples.
Let’s first consider cases on which function overloading does not depend on:-
1. Return Type of Function
public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public int foo()
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}
The code sample will generate the following error:-
Type ‘MyBaseClass' already defines a member called 'foo' with the same parameter types
2. Name of parameters
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("Hi I am original");
}
public int foo(int j)
{
Console.WriteLine("Hi I am Copy");
return 1;
}
}
The above code will give the following error:-
Type 'MyBaseClass' already defines a member called 'foo' with the same parameter types.
Now we will consider cases on which function overloading depends on:-
3. Variable Number of parameters to function
public class MyBaseClass
{
public void foo()
{
Console.WriteLine("Hi I am original");
}
public void foo(int i)
{
Console.WriteLine("Hi I am overloaded and here is the value of i " + i.ToString());
}
}
In this example, number of parameters are different so this will compile fine and now when you create object of this class you will see two overloaded version of foo.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo();
obj.foo(10);
Here is the output:-
Hi I am original
Hi I am overloaded and here is the value of i 10
4. Order of parameters to function
public class MyBaseClass
{
public void foo(int i , string s)
{
Console.WriteLine("The parameter order is int and string.The value of i is {0} and that of s is {1}",i,s);
}
public void foo(string s, int i)
{
Console.WriteLine("The parameter order is string and int.The value of i is {0} and that of s is {1}", i, s);
}
}
In the example above, number of parameters are same but since the order is different so this will compile fine.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo(1,”shakti”);
obj.foo(“Test”,2);
Here is the output:-
The parameter order is int and string.The value of i is 1 and that of s is shakti
The parameter order is string and int.The value of i is 2 and that of s is Test
5. Data type of parameters
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("The is one parameter function which takes int as parameter and value of i is {0}.",i);
}
public void foo(string s )
{
Console.WriteLine("The is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}
Here the number of parameters are same but data types are different so this will compile fine.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo(10);
obj.foo("Test");
Output:-
The is one parameter function which takes int as parameter and value of i is 10.
The is one parameter function which takes string as parameter and value of s is Test.
6. Overloading in case of inheritance
public class MyBaseClass
{
public void foo(int i)
{
Console.WriteLine("The is one parameter function which takes int as parameter and value of i is {0}.",i);
}
}
public class MyChildClass : MyBaseClass
{
public void foo(string s)
{
Console.WriteLine("The is one parameter function which takes string as parameter and value of s is {0}.", s);
}
}
Here overloading is done on a function present in base class this is different from method hiding in way that hiding hides the implementation of base class while in this case both the implementations will co-exist in child class.
Lets now create object of this class and call both the versions:-
MyBaseClass obj = new MyBaseClass();
obj.foo(10);
MyChildClass obj2 = new MyChildClass();
obj2.foo("Test");
Here is the output:-
The is one parameter function which takes int as parameter and value of i is 10.
The is one parameter function which takes string as parameter and value of s is Test.