Bird
0
0

You want to design a system where different devices can be turned on and off, but they do not share a common base class. How can interfaces help?

hard🚀 Application Q8 of 15
C Sharp (C#) - Interfaces
You want to design a system where different devices can be turned on and off, but they do not share a common base class. How can interfaces help?
ADefine an interface with TurnOn and TurnOff methods and implement it in each device class
BCreate a base class with TurnOn and TurnOff methods and inherit all devices
CUse abstract classes instead of interfaces for unrelated devices
DUse static methods to control devices without interfaces
Step-by-Step Solution
Solution:
  1. Step 1: Understand problem constraints

    Devices are unrelated and cannot share a base class, so inheritance is not an option.
  2. Step 2: Apply interfaces to define common behavior

    Interfaces allow unrelated classes to implement common methods like TurnOn and TurnOff.
  3. Final Answer:

    Define an interface with TurnOn and TurnOff methods and implement it in each device class -> Option A
  4. Quick Check:

    Interfaces enable common behavior without inheritance [OK]
Quick Trick: Use interfaces to share behavior across unrelated classes [OK]
Common Mistakes:
MISTAKES
  • Trying to use inheritance for unrelated classes
  • Confusing abstract classes with interfaces
  • Using static methods which lack polymorphism

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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