Type Hierarchy

  • Express the power of constructing type hierarchies.
  • Arrange simple types into a type hierarchy.

Classes allow us to define our data types. Indeed, Student and GradStudent are data types. More specifically, GradStudent is a subtype of Student. And by the same token, Student is the base type of GradStudent.

Since inheritance is transitive, it allows us to create a hierarchy of types. This makes object-oriented programming an instrumental paradigm for real-world modeling because the entities in real-world problems often naturally exhibit such a hierarchy.

Example: Fruits
Example: Animals

Exercise We are building a software solution for a organising a farm and the crops we grow on it. We have the following classes so far: Crop, Grain, Corn, Wheat, Lettuce. Arrange these classes into the following type hierarchy.

Solution

Exercise Come up with your own set of classes that could be arranged into the hierarchy shown above, or one similar to it.

Solution

Many possibilities, including the following: