Bird
Raised Fist0

Identify the issue in this code snippet:

medium📝 Debug Q7 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
Identify the issue in this code snippet:
abstract class Processor {
    public abstract void Process();
}
class TaskProcessor : Processor {
    public void Process() {
        System.Console.WriteLine("Processing task");
    }
}
AThe class Processor cannot have abstract methods.
BThe method Process() in TaskProcessor must be marked with override.
CThe method Process() should not have a body in TaskProcessor.
DTaskProcessor should be declared abstract.
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract method implementation

    When a derived class implements an abstract method, it must use the 'override' keyword.
  2. Step 2: Check the code

    TaskProcessor's Process() method lacks the 'override' keyword, which causes a compilation error.
  3. Step 3: Evaluate other options

    Processor can have abstract methods (B is wrong). Process() in TaskProcessor must have a body (C is wrong). TaskProcessor does not need to be abstract if it implements all abstract methods (D is wrong).
  4. Final Answer:

    The method Process() in TaskProcessor must be marked with override. -> Option B
  5. Quick Check:

    Override required for abstract method implementation [OK]
Quick Trick: Override keyword is mandatory for abstract method implementation [OK]
Common Mistakes:
MISTAKES
  • Omitting override keyword
  • Thinking abstract methods can be implemented without override
  • Declaring derived class abstract unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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