This is based on another SO question about polymophism which I liked very much. So I played around with it and modified it to understand it. But then it rattled my understanding of ploymorphism.
I created an interface inside of a class, created a class "A," and implemented the interface method. I also created another class "B" that extends "A." And another class "C" that extends "B."
When you run it, the output is:
BA-> AEP
Notice that BA->
part comes from B : A
class while AEP
is the default param from different method in A : E
How is it that two methods are called when I'm doing
`A instance = new C();
Console.WriteLine(instance.GetName());`
Why?
class Program
{
interface E
{
string GetName(string s);
}
public class A : E
{
public virtual string GetName(string s="AEP")
{
return s;
}
}
public class B : A
{
public override string GetName(string s="BAP")
{
return "BA-> " + s;
}
}
public class C : B
{
public new string GetName()
{
return "CB->";
}
}
static void Main()
{
A instance = new C();
Console.WriteLine(instance.GetName());
Console.ReadLine();
}
}