Bird
0
0

Identify the error in this C# code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Inheritance
Identify the error in this C# code snippet:
class Animal {
  private void Speak() {
    System.Console.WriteLine("Animal speaks");
  }
}
class Dog : Animal {
  public void Speak() {
    System.Console.WriteLine("Dog barks");
  }
}
ACannot override private method
BMissing override keyword
CMethod Speak must be public in base
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check method accessibility and overriding rules

    Private methods are not visible to derived classes and cannot be overridden.
  2. Step 2: Analyze the derived class method

    The Dog class defines a new method Speak, but it does not override the base method because the base method is private.
  3. Final Answer:

    Cannot override private method -> Option A
  4. Quick Check:

    Private methods are not overridable [OK]
Quick Trick: Private methods are hidden, not overridden [OK]
Common Mistakes:
MISTAKES
  • Trying to override private methods
  • Forgetting override keyword
  • Assuming private methods are inherited

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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