Case study: combining inheritance and composition

25-minute lesson · module-inheritance-04

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

Case study: combining inheritance and composition

25-minute lesson · module-inheritance-04

  • Model a small domain using both inheritance and composition together.
  • Use a base class with subclasses that override a key method (polymorphism).
  • Build a container that has-a collection and delegates polymorphically.
  • Read a simple class design and implement it faithfully.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Inheritance / A Case Study

Adaptation notes. Reading rewritten in our voice as an original worked design. The domain is an Iowa State plan-of-study: a CourseItem base with Lecture and Lab subclasses (inheritance + polymorphic credit rules), aggregated by a Semester container (composition). Hidden tests check polymorphic credit totals across mixed item types.

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