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:

  1. class GrandFather  
  2.     {  
  3.         public virtual void Display()  
  4.         {  
  5.             Console.WriteLine("GrandFather");  
  6.         }  
  7.     }  
  8.     class Father:GrandFather  
  9.     {  
  10.         public override void Display()  
  11.         {  
  12.             Console.WriteLine("Father");  
  13.         }  
  14.     }  
  15.     class Son : Father  
  16.     {  
  17.         public override void Display()  
  18.         {  
  19.             Console.WriteLine("Son");  
  20.         }  
  21.     }  
  22.         class Program  
  23.     {  
  24.         static void Main(string[] args)  
  25.         {  
  26.             GrandFather a = new Son();  
  27.             a.Display();  
  28.   
  29.             Console.ReadLine();  
  30.         }  
  31.     }  
Anybody who is familiar with OOPS will tell the output of the code snippet as SON. Consider the code snippet, given below:
  1. class Father:GrandFather  
  2.    {  
  3.        public virtual void Display()  
  4.        {  
  5.            Console.WriteLine("Father");  
  6.        }  
  7.    }  
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.