Bird
0
0

You want different classes like Car and Airplane to have a startEngine() method, but their implementations differ. How does using an interface help?

hard📝 Application Q15 of 15
PHP - Interfaces and Traits
You want different classes like Car and Airplane to have a startEngine() method, but their implementations differ. How does using an interface help?
AIt prevents Car and Airplane from having startEngine() methods
BIt shares the same startEngine() code between Car and Airplane automatically
CIt forces both classes to have a startEngine() method, ensuring they can be used interchangeably
DIt makes startEngine() method optional in both classes
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface role in method enforcement

    An interface declares startEngine() method that classes must implement, but each class writes its own code.
  2. Step 2: Recognize benefit for interchangeable use

    Because both classes have startEngine(), code using them can call this method without knowing class details.
  3. Final Answer:

    It forces both classes to have a startEngine() method, ensuring they can be used interchangeably -> Option C
  4. Quick Check:

    Interface = method contract, different code [OK]
Quick Trick: Interface ensures method presence, not shared code [OK]
Common Mistakes:
  • Thinking interface shares method code
  • Believing methods become optional
  • Confusing interface with inheritance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes