MapQuest Interview Question: Explain the reasoning behind ... | Glassdoor

Interview Question

Senior Software Engineer Interview Denver, CO

Explain the reasoning behind the OO practice of favoring

  composition over inheritance. When would you use one or the other?
design, software, object oriented

Interview Answer

1 Answer


Composition means that an object's behaviors are specified by other objects it contains. For instance, a Dragon class might contain a Movement object that specifies flight. Inheritance, as is usually used in OO, means that behavior is specified by an object's position in a class heirarchy. A Dragon would inherit from a FlyingObject class, for instance, and flight behavior would be coded there.

Inheritance makes refactoring behavior tricky, because changing a behavior (e.g. a dragon that can fly in SPACE) implies changing that behavior for all objects that inherit from a superclass. With a design based on composition, the space dragon is as simple as creating a new Movement object that details how to move through space.

Interview Candidate on Sep 14, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.