Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
Identify the error in this code snippet:
class Animal { public virtual void Speak() { Console.WriteLine("Animal"); } }
class Dog : Animal { public void Speak() { Console.WriteLine("Dog"); } }
ADog.Speak() should use 'override' keyword
BAnimal.Speak() should be static
CDog.Speak() cannot have same name as Animal.Speak()
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check method overriding rules

    Dog's Speak method hides Animal's virtual Speak but lacks 'override' keyword.
  2. Step 2: Identify correct fix

    To override virtual method, Dog.Speak() must declare 'override' keyword.
  3. Final Answer:

    Dog.Speak() should use 'override' keyword -> Option A
  4. Quick Check:

    Override keyword required to override virtual methods [OK]
Quick Trick: Use 'override' to override virtual methods [OK]
Common Mistakes:
MISTAKES
  • Omitting 'override' causes method hiding, not overriding
  • Making base method static incorrectly
  • Thinking same method name is disallowed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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