«^»
7.8. Inheritance should be used for is-a relationships

Earlier, we used the class Date when constructing the class Person, and we have now used the class Person when constructing the class Student. We used inheritance to produce the class Student from the class Person, whereas the class Person contains a field (called oDateOfBirth) of type Date. This is called composition (or layering): the class Person is composed of a field of type Date.

Earlier, we said we used inheritance because the new class ‘is a more specialized form of another class’. It is best to use inheritance for is-a relationships and composition for has-a relationships. So, one test for deciding whether to use inheritance or composition is to see whether it makes sense to use the words is a or has a. For example, ‘every person is a date’ is nonsense whereas ‘every person has a date for his/her date of birth’ makes sense.