Case 1:
public class BaseClass
{
public virtual void Print(int i)
{
Console.WriteLine("BaseClass Print(int)");
}
}
public class DerivedClass : BaseClass
{
public override void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
DerivedClass objDerivedClass = new DerivedClass();
int i = 10;
objDerivedClass.Print(i);
}
Output is DerivedClass Print(object).
Case 2:
public class SomeClass
{
public void Print(int i)
{
Console.WriteLine("DerivedClass Print(int)");
}
public void Print(object obj)
{
Console.WriteLine("DerivedClass Print(object)");
}
}
static void Main(string[] args)
{
SomeClass objSomeClass = new SomeClass();
int i = 10;
objSomeClass.Print(i);
}
Output is DerivedClass Print(int).
After calling objDerivedClass.Print(i); method, the output is DerivedClass Print(object). I don't understand why the method Print(object obj) is being called instead of Print(int i).
If DerivedClass does not inherit BaseClass class then output is DerivedClass Print(int).
Please explain.......