An Engine the Car Owns

Medium ⏱ 12 min 58% acceptance ★★★★☆ 4.3
Model composition: Engine has start() returning 'vroom'; Car receives an Engine instance in its constructor (dependency injection) and its own start() delegates to the engine, returning f'car: {engine sound}'. No inheritance anywhere — a Car *has* an Engine, it is not one.

Examples

Example 1
Input
Car(Engine()).start()
Output
'car: vroom'
Explanation

The car forwards to its injected component.

Constraints

  • Car must accept the engine as a parameter.
  • No subclassing.

Topics

OOPcomposition

Companies

Toyota ConnectedUberBosch

Hints

Hint 1

self.engine = engine stores the part.

Hint 2

Delegation is just calling through.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.