Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 7 · “Is-a” and “Has-a” Relationships

“Is-a” and “Has-a” Relationships

Inheritance describes is-a: a Student is a kind of User. Object collaboration describes has-a: a Course uses a Schedule and has enrollment links to students.

Read the symbols introduced here:

  • A hollow triangle points to the more general class.
  • A plain line shows an association.
  • A filled diamond shows exclusive composite ownership; it is a design constraint, not something Python references enforce automatically.
  • 1 means exactly one; 0..* means zero or more.
UserStudentProfessorCourseScheduleenrolls in0..*0..*owns11

Students and courses have independent lifetimes, so their connection is an association. The Schedule diamond expresses an exclusive-ownership design; the earlier code permits a supplied Schedule, so you would need to enforce that constraint or use a plain association instead.

Apply it: should a Library inherit from Book, or hold Book objects? Justify the choice using the operations each type should support.

These relationships describe structure. Polymorphism shows how different objects can respond to the same operation.