Lecture Notes on Object-Oriented Programming

The Quality of Classes and OO Design

This collection of notes on OOP was never meant to stand alone. It also represents a view of OO circa early to mid 1990s. Some people still find them useful, so here they are, caveat emptor. Special thanks to Gilbert Benabou for taking to time to compile the first printable version of this document and inspiring us to provide it.
Printable Version

We're going to look at the way we define the relationships between classes so we know how objects can relate to each other.

Consider the differences and similarities between the classes of the following objects: pets, cats, siberian huskies, poodles, tails, owners.

We see the following relationships:

  • a cat is a kind of pet
  • a dog is a (different) kind of pet
  • Siberian huskies and poodles are both kinds of dogs
  • a tail is a part of both dogs and cats
  • owners feed pets, pets please owners
  • owners use money to register pets

Depending on which book you read, or which tool you use, there are at least the following sorts of relationships between classes:

  • aggregation
  • inheritance
  • using
  • association
  • instantiation

The first two are the most essential to OO design. Interestingly they are also sometimes equally valid design alternatives to solve a given problem.

Inheritance is the most distinctively OO form of relationship, but not necessarily the most important, and certainly not always the right choice.

Class relationships are of course visible in source code, but can also be defined graphically with a modeling language such as UML.