Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
C Sharp (C#) - Inheritance
Identify the error in this code snippet:
sealed class Animal {
    public void Speak() { Console.WriteLine("Animal speaks"); }
}
class Dog : Animal {
    public void Speak() { Console.WriteLine("Dog barks"); }
}
AMethod Speak must be virtual in Animal.
BCannot declare method Speak in Dog class.
CCannot inherit from sealed class Animal.
DNo error, code runs fine.
Step-by-Step Solution
Solution:
  1. Step 1: Check sealed class inheritance rules

    A sealed class cannot be inherited by any other class.
  2. Step 2: Analyze Dog class inheritance

    Dog tries to inherit from sealed Animal, which causes a compilation error.
  3. Final Answer:

    Cannot inherit from sealed class Animal. -> Option C
  4. Quick Check:

    Sealed class blocks inheritance [OK]
Quick Trick: Sealed class cannot have child classes [OK]
Common Mistakes:
MISTAKES
  • Thinking method override causes error
  • Ignoring sealed class inheritance rule
  • Assuming code compiles fine

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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