Bird
Raised Fist0

Examine the following code snippet:

medium📝 Debug Q6 of Q15
C Sharp (C#) - Interfaces
Examine the following code snippet:
abstract class Appliance {
    public abstract void TurnOn();
}
class Blender : Appliance {
    public void TurnOn(int speed) { Console.WriteLine($"Blender started at speed {speed}"); }
}

What is the main issue with this code?
AThe base class should not have abstract methods
BAbstract classes cannot have methods with parameters
CThe derived class does not correctly override the abstract method TurnOn()
DThe derived class cannot have methods with parameters
Step-by-Step Solution
Solution:
  1. Step 1: Check abstract method signature

    The base class declares an abstract method TurnOn() with no parameters.
  2. Step 2: Check derived class method

    The derived class defines TurnOn(int speed), which is an overload, not an override.
  3. Step 3: Understand override requirement

    To implement the abstract method, the derived class must provide a method with the exact signature: public override void TurnOn().
  4. Final Answer:

    The derived class does not override the abstract method correctly -> Option C
  5. Quick Check:

    Method signatures must match for overrides [OK]
Quick Trick: Override abstract methods with exact signature [OK]
Common Mistakes:
MISTAKES
  • Assuming method overloading satisfies abstract method implementation
  • Thinking abstract methods can have parameters changed in derived classes
  • Believing abstract classes cannot have abstract methods with no parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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