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.
Let’s 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.