Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
What will be the output of this code?
class Base {
    public virtual string Show() => "Base";
}
class Derived : Base {
    public new string Show() => "Derived";
}
class Program {
    static void Main() {
        Base obj = new Derived();
        System.Console.WriteLine(obj.Show());
    }
}
ADerived
BBase
CCompilation error
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Identify method hiding vs overriding

    The Derived class uses new keyword, which hides the base method instead of overriding it.
  2. Step 2: Analyze method call on base reference

    Calling Show on a Base reference calls the base class method, ignoring the hidden derived method.
  3. Final Answer:

    Base -> Option B
  4. Quick Check:

    Method hiding calls base method via base reference [OK]
Quick Trick: new hides method; base reference calls base method [OK]
Common Mistakes:
MISTAKES
  • Confusing new with override
  • Expecting derived method call
  • Assuming new enables polymorphism

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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