Bird
0
0

You want to create a system where different devices can Start() and Stop() but each device does it differently. How should you use interfaces to design this?

hard🚀 Application Q15 of 15
C Sharp (C#) - Interfaces

You want to create a system where different devices can Start() and Stop() but each device does it differently. How should you use interfaces to design this?

ACreate a base class Device with Start and Stop methods and inherit it
BWrite Start and Stop methods directly in each device class without interface
CDefine an interface IDevice with Start and Stop methods, then implement it in each device class
DUse abstract classes only, no interfaces
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface purpose

    Interfaces define a contract for methods without implementation, perfect for different device behaviors.
  2. Step 2: Apply interface to devices

    Define IDevice with Start and Stop, then each device class implements these methods with its own details.
  3. Final Answer:

    Define an interface IDevice with Start and Stop methods, then implement it in each device class -> Option C
  4. Quick Check:

    Interface = shared method rules, different implementations [OK]
Quick Trick: Use interfaces for shared method names, different code [OK]
Common Mistakes:
MISTAKES
  • Using base class limits flexibility
  • Skipping interface loses contract benefits
  • Confusing abstract classes with interfaces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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