Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Inheritance
What will be the output of the following code?
class Parent {
    public virtual void Display() { Console.WriteLine("Parent"); }
}
class Child : Parent {
    public sealed override void Display() { Console.WriteLine("Child"); }
}
class GrandChild : Child {
    public override void Display() { Console.WriteLine("GrandChild"); }
}

Parent obj = new GrandChild();
obj.Display();
ACompilation error
BGrandChild
CParent
DChild
Step-by-Step Solution
Solution:
  1. Step 1: Identify sealed override effect

    The method 'Display' in 'Child' is sealed, so it cannot be overridden in 'GrandChild'.
  2. Step 2: Check 'GrandChild' override

    'GrandChild' tries to override 'Display', which causes a compilation error.
  3. Final Answer:

    Compilation error -> Option A
  4. Quick Check:

    Sealed methods cannot be overridden further [OK]
Quick Trick: Sealed override methods block further overrides [OK]
Common Mistakes:
MISTAKES
  • Assuming 'GrandChild' override works
  • Expecting runtime output instead of compile error
  • Ignoring sealed keyword effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes