Lecture Notes on Object-Oriented Programming
The Quality of Classes and OO Design
table of contents
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.