Bird
Raised Fist0

Given these classes:

hard🚀 Application Q9 of Q15
C Sharp (C#) - Inheritance
Given these classes:
class A {
  public virtual string Info() => "A";
}
class B : A {
  public override string Info() => "B";
}
class C : B {
  public new string Info() => "C";
}
class Program {
  static void Main() {
    A obj = new C();
    System.Console.WriteLine(obj.Info());
  }
}

What is the output?
AB
BC
CA
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand 'new' keyword effect on method hiding

    The method Info in class C hides the base method instead of overriding it.
  2. Step 2: Analyze polymorphic call

    Variable obj is of type A but holds C instance. Virtual dispatch calls B.Info because C.Info is hidden, not overridden.
  3. Final Answer:

    B -> Option A
  4. Quick Check:

    'new' hides method, virtual calls base override [OK]
Quick Trick: 'new' hides method; virtual calls last override in hierarchy [OK]
Common Mistakes:
MISTAKES
  • Expecting C output due to instance type
  • Confusing 'new' with 'override'
  • Assuming compilation error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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