To improve flexibility in leveraging types, a type may establish these kinds of relationships with other types:
- Composition, where a value of one type is incorporated within (or referrred to by) a value of another type.
- Inheritance, where one type reuses state or methods defined by another type.
- Subtype, where a value of one type may be safely substituted in where another type is called for.
We have already seen composition at play in the typed fields of a struct, the elements of an array, and the value type pointed at by some reference type. It is now time to explore how one might define and exploit inheritance and subtyping to improve code versatility and reuse.