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
- {
- public virtual void Display()
- {
- Console.WriteLine("GrandFather");
- }
- }
- class Father:GrandFather
- {
- public override void Display()
- {
- Console.WriteLine("Father");
- }
- }
- class Son : Father
- {
- public override void Display()
- {
- Console.WriteLine("Son");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- GrandFather a = new Son();
- a.Display();
- Console.ReadLine();
- }
- }
- class Father:GrandFather
- {
- public virtual void Display()
- {
- Console.WriteLine("Father");
- }
- }
Thus, always look for the last implementation of the function of the base class in the defined derived classes.
Nikhil SanganiPosted Aug 8, 2016, 7:44 AM
Good
Soumalya DasPosted Aug 7, 2016, 12:12 PM
Good example