Base Classes and Their Role
A class hierarchy is a set of classes ordered in a lattice created by derivation, and it is used to represent concepts that have hierarchical relationships.
Why Programs Need Categories Inside Categories
Many things a program models sort naturally into categories that sit inside other categories. A vehicle can be a truck, and a truck can be a fire engine. A face drawn on a screen can be built from a circle, and that circle is itself a shape. When a program needs to represent this kind of nesting, it needs a way to record which category sits inside which. Class hierarchies are the tool for doing exactly that, and base classes are the starting points of those hierarchies.
What do you think happens?
If a fire engine is a truck, and a truck is a vehicle, is a fire engine a vehicle? Decide before reading on.
Reveal answer
Yes
Membership in a category passes along a chain. Once you know the fire engine is a truck and the truck is a vehicle, you can conclude the fire engine is a vehicle too, even though that link was never stated directly.
Tracing a Fire Engine Upward
Start with the most specific category, the fire engine, and move one step up at a time. Each step follows a single link to the category it belongs to. The first step reaches the truck. The second step reaches the vehicle. There is nowhere further up to go, so the walk stops at the most general category. The path you just walked is the chain of ancestors for the fire engine.
What a Class Hierarchy Is
A class hierarchy is a set of classes ordered in a lattice created by derivation, and it is used to represent concepts that have hierarchical relationships. Derivation is the act of defining a more specific class from a more general one, so each derived class is placed below the class it comes from.
The word lattice is worth a moment of thought. A simple list has one line of items, but a lattice lets classes sit at several levels at once and branch out from one another. This is why a hierarchy is shaped more like a branching structure than a straight line. A hierarchy can be deep, meaning many levels separate the most general class from the most specific one, and it can be wide, meaning a single class can have many direct children. Real hierarchies can grow large, sometimes holding hundreds of classes.
Base classes sit at the top of the structure. A base class defines the properties that are common to everything beneath it. The more specific classes are then derived from that base, inheriting its common foundation and adding what makes them distinct.
Reading the Arrows in a Shapes Hierarchy
A common semi-realistic example is a set of shapes on a screen. A smiley face is a kind of circle, and a circle is a kind of shape. Once this chain is drawn as a diagram, the structure becomes easy to read at a glance. The general class sits at the top and the most specific class sits at the bottom.
In a class hierarchy diagram, each line is drawn as an arrow, and each arrow stands for an inheritance relationship. The arrow points from the derived class to the class it is derived from. So an arrow always links a more specific class to a more general one. The class at the tail of the arrow is the child, and the class at the head is the parent.
A real shapes program would usually hold more classes at the same level as Circle, such as other kinds of shapes. Each of those would also have an arrow pointing up to Shape. The chain shown here is enough to show how the hierarchy grows downward from a general class toward specific ones.
Tracing Ancestors Through a Hierarchy
The ancestors of a class are every class you pass on the way up from it to the top of its chain. To find them, repeat one move: find the class this one is derived from, then repeat from that class until you reach a class that has no parent in the hierarchy. The ancestors are the classes you collected along the way, in the order you reached them.
Listing the ancestors of Smiley Face
Using the shapes hierarchy, where Circle is derived from Shape and Smiley Face is derived from Circle, list every ancestor of Smiley Face in order.
- 1Find the direct parent
The arrow leaving Smiley Face points to Circle, so Circle is the first ancestor.
- 2Move up again
The arrow leaving Circle points to Shape, so Shape is the next ancestor.
- 3Check for a further parent
Shape is the general class at the top of this hierarchy and has no arrow leaving it, so the walk stops.
The ancestors of Smiley Face are Circle, then Shape.
Mistakes When Reading a Hierarchy
Common mistake Reading the arrow in the wrong direction
What it looks likeSeeing an arrow from Shape to Circle and concluding that Shape is the more specific class.
Why it failsEach arrow points from the derived class to the class it is derived from, so the arrow always goes from specific toward general.
Do this insteadFind the tail of the arrow and call that class the derived one. The head of the arrow is the more general class.
Common mistake Assuming only directly drawn links count
What it looks likeConcluding that a fire engine is not a vehicle because the diagram shows no line between them.
Why it failsMembership in a category passes along a chain, so ancestry extends through every intermediate class.
Do this insteadFollow the chain one link at a time until no further parent exists.
Common mistake Designing the specific class before the general one
Why it failsA specific class such as Circle is derived from a general class that defines the properties common to every shape. Without that base defined first, the derivation has nothing to build on.
Do this insteadSpecify the general class first, then derive the more specific classes from it.
Common mistake Treating a wide hierarchy as a simple line, when a single class can have many direct children.
Practice Tracing a Vehicle Chain
A hierarchy has Vehicle at the top, Truck derived from Vehicle, and Fire Engine derived from Truck. Without looking back, write the ancestors of Fire Engine in order, then state which arrow in the diagram points from the base class toward the derived class.
Hints
- Start at Fire Engine and find the class it is derived from.
- Repeat the step with each class you find until nothing above remains.
- Remember the arrow points from the derived class to its base, so the reverse direction is the answer you are looking for in the second part.
Key Takeaways
- A class hierarchy is a set of classes ordered in a lattice created by derivation, and it represents concepts that have hierarchical relationships.
- Each arrow in a hierarchy diagram stands for an inheritance relationship and points from the derived class to the class it is derived from.
- Category membership passes along a chain, so a class's ancestors include every class you reach by repeatedly moving to its parent.
- Hierarchies can be deep and wide, so tracing ancestors one link at a time is the reliable way to read them.
- Good design defines the general base class first, then derives more specific classes from it.