Composition vs inheritance

22-minute lesson · module-inheritance-03

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Composition vs inheritance

22-minute lesson · module-inheritance-03

  • Distinguish a "has-a" relationship (composition) from an "is-a" relationship (inheritance).
  • Build a class that holds other objects as attributes.
  • Delegate work to contained objects from the outer class.
  • Choose composition or inheritance for a given modeling problem.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Inheritance / Composition vs Inheritance

Adaptation notes. Reading rewritten in our voice. A Course "has-a" list of Student objects; methods delegate to the contained students. Hidden tests check that the container aggregates over its parts (total enrolled credits, roster).

License notes. Original chapter is GNU FDL 1.3 (Brad Miller and David Ranum). No verbatim text from the original is reproduced here.