Govt. Exams
Entrance Exams
Advertisement
Topics in C# Programming
What will be the output of the following code?
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
Correct Answer:
B. B
EXPLANATION
Due to polymorphism, the overridden method in class B is called, not the virtual method in class A. Output is 'B'.