All challenges
Multiple ChoiceBeginner 45s

Composition over inheritance pitfalls

A class extends a base class to reuse a few utility methods, but does not share the base's real 'is-a' contract. What is the cleanest fix?
Options
  1. 1Add more utility methods to the base class so that reuse by subclasses becomes even easier
  2. 2Make the base class utility methods protected and keep inheriting to reuse them as before
  3. 3Extract the utilities into a collaborator and compose it, avoiding a fake subtype relationship
  4. 4Introduce a second level of inheritance so the shared utility code is better organized hierarchically

Think you can answer under pressure?

Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.

Play this in a 3-minute battle