Skip to content

Instantly share code, notes, and snippets.

@r00k
Created August 12, 2012 20:27
Show Gist options
  • Save r00k/3334214 to your computer and use it in GitHub Desktop.
Save r00k/3334214 to your computer and use it in GitHub Desktop.
Composition vs Inheritance

My favorite composition vs. inheritance metaphor:

There are only two types of relationships in an object model: composition and inheritance. Both have analogs in a family tree. A composition relation is like a marriage between objects. It is dynamic, it happens during the participating objects' lifetimes, and it can change. Objects can discard partners and get new partners to collaborate with. Inheritance relations are more like births into the family. Once it happens, it is forever. Just as both marriage and ancestry appear in the same family tree, composition and inheritance coexist in a single object model.

(from Object Design - Roles, Responsibilities and Collaborations)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment