Run Time Polymorphism In C#

Since the beginning of the Object Oriented Pogramming language era, people are confused about the run time of Polymorphism. People can explain the run time of Polymorphism behavior until the first level of Inheritance, but fail when it comes to explaining this behaviour in the multi-level run time Polymorphism.

Consider the following example:

class GrandFather

{

publicvirtualvoid Display()

{

Console.WriteLine("GrandFather");

}

}

class Father:GrandFather

{

publicoverridevoid Display()

{

Console.WriteLine("Father");

}

}

class Son : Father

{

publicoverridevoid Display()

{

Console.WriteLine("Son");

}

}

class Program

{

staticvoid Main(string[] args)

{

GrandFather a = new Son();

a.Display();

Console.ReadLine();

}

}

Anybody who is familiar with OOPS will tell the output of the code snippet as SON. Consider the code snippet, given below:

class Father:GrandFather

{

publicvirtualvoid Display()

{

Console.WriteLine("Father");

}

}

In this case, a person may get confused between Father & Son whereas the output of the code will be GrandFather as there is no implementation of Display method of GrandFather class in its derived classes.

Thus, always look for the last implementation of the function of the base class in the defined derived classes.