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 run-time.
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
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">
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.