Bird
Raised Fist0

Analyze the following code and find the error:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
Analyze the following code and find the error:
abstract class Instrument {
    public abstract void Play();
}
class Piano : Instrument {
    public override void Play() {
        System.Console.WriteLine("Playing piano");
    }
}
class Guitar : Instrument {
    public void Play() {
        System.Console.WriteLine("Playing guitar");
    }
}
APiano cannot override an abstract method.
BGuitar's Play method must use the override keyword.
CInstrument class cannot have abstract methods.
DGuitar class cannot inherit from Instrument.
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract method requirements

    Any subclass must override abstract methods with the override keyword.
  2. Step 2: Inspect Guitar class

    Guitar defines Play without override, which is invalid.
  3. Final Answer:

    Guitar's Play method must use the override keyword. -> Option B
  4. Quick Check:

    Abstract methods require override in all subclasses [OK]
Quick Trick: Override keyword mandatory for abstract method implementations [OK]
Common Mistakes:
MISTAKES
  • Omitting override keyword on abstract method implementations
  • Misunderstanding abstract class inheritance rules
  • Assuming abstract methods can be implemented without override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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