My first post on MB Coding starts with this small piece of code which tries to explain polymorphism concept.
As you can see
display function is in both classes , so when we call it from object of type
father()
it invokes
display function of
father() & when that object tries to get assigned to
children() , it
still invoke
display function from
father() . Why ? because father is father & children is children.
Children is a derived class hence when display function is called it calls display function of father only.
So if you want to call display function check this code below :
class father
{
public father()
{
Console.WriteLine("In Father Constructor");
}
public virtual void display()
{
Console.WriteLine("In Father Class");
}
}
class children : father
{
public children()
{
Console.WriteLine("In Children Constructor");
}
public override void display()
{
Console.WriteLine("In Children Class");
}
}
Polymorphism is just like eye , it observe or sees what is shown to it...or say it can understand
or interpret different objects.