Designing a General Class Before Specific Ones
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 in the world 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 that.
What do you think happens?
A fire engine is a truck, and a truck is a vehicle. Without anyone stating it directly, is a fire engine also a vehicle?
Reveal answer
Yes
Membership in a category passes along a chain. Because the fire engine is a truck and the truck is a vehicle, the fire engine is also a vehicle, even though that relationship was never stated directly. A hierarchy records these links so that the conclusion can be read off the structure.
Tracing a Category Chain
The chain fire engine, truck, vehicle is the simplest case of a class hierarchy. Each step in the chain is a link from a more specific category to a more general one. Read it from the bottom up: a fire engine is a kind of truck, and a truck is a kind of vehicle. Read it from the top down and the same chain says that vehicle has a truck category inside it, and that truck has a fire engine category inside it.
The diagram keeps the most general class at the top and places more specific classes lower down. Following the links upward from the fire engine reaches the truck and then the vehicle, and that upward path is exactly what you will trace later when you find the ancestors of a class.
Derivation and the Shape of a Hierarchy
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. When a class is derived from another, it sits one level below that class in the hierarchy.
The word lattice describes why a hierarchy looks like a branching structure rather than a single straight line. A simple list has one line of items, but a lattice allows classes to sit at several levels at once and to branch out from one another. Two properties describe the shape. A hierarchy is deep when many levels separate the most general class from the most specific one. It is wide when a single class has many direct children. Real hierarchies can be both deep and wide, sometimes holding hundreds of classes.
The source's main semi-realistic example uses shapes on a screen. A smiley face is a kind of circle, and a circle is a kind of shape. Written as a diagram, the general class sits at the top and the specific classes sit beneath it. The diagram below shows this simplest form. A real shapes program would usually hold more classes at the same level as Circle, such as other kinds of shapes, but this chain is enough to show how the hierarchy grows downward from a general class toward specific ones.
Reading Arrows in an Inheritance Diagram
In a class hierarchy diagram, each line is drawn as an arrow, and each arrow stands for an inheritance relationship. The source states that class Circle is derived from class Shape. The arrow connects the derived class to the class it is derived from. This means an arrow always links a more specific class to a more general one, and the arrow points toward the general class.
Tracing the Ancestors of a Smiley Face
The ancestors of a class are the classes you reach by following its arrows upward toward the general class. Tracing them is a repeatable procedure: start at the class you are given, follow its arrow to the class it is derived from, record that class, and repeat until no arrow remains.
Finding every ancestor of the smiley face class
In the shapes hierarchy, a smiley face is a kind of circle, and a circle is a kind of shape. List the ancestor classes of the smiley face class, in the order you reach them.
- 1Start at the given class
The starting class is Smiley face. It is the most specific class in this chain, so it sits at the bottom of the diagram.
- 2Follow the first arrow up
The arrow from Smiley face points to Circle, because a smiley face is a kind of circle. Circle is the first ancestor.
- 3Follow the next arrow up
The arrow from Circle points to Shape, because a circle is a kind of shape. Shape is the second ancestor.
- 4Check for further arrows
Shape has no arrow leaving it in this chain, so the walk stops. Shape is the most general class here.
The ancestors of Smiley face, in order, are Circle and then Shape.
Notice that Shape is an ancestor of Smiley face even though no arrow connects them directly. The relationship passes along the chain, just as the fire engine was known to be a vehicle through the truck. Every class above a given class in the chain counts as an ancestor of it.
Mistakes When Reading Hierarchies
Common mistake Reading the arrow as pointing from the general class to the specific class
What it looks likeReading the arrow between Circle and Shape as showing that Shape is derived from Circle.
Why it failsEach arrow points from the derived class to the class it is derived from, so the arrow points toward the more general class.
Do this insteadFind the class at the tail of the arrow. That is the derived class, and the class at the head is its more general parent.
Common mistake Assuming only directly connected classes are related
What it looks likeConcluding that a smiley face is not a shape because no arrow joins them.
Why it failsMembership in a category passes along a chain, so a class inherits the category membership of every class above it.
Do this insteadFollow the arrows upward step by step and include every ancestor you reach, not just the one directly above.
Common mistake Stopping the trace too early in a deep hierarchy
Why it failsA deep hierarchy has many levels, and the most general class may be several steps above the class you started from.
Do this insteadKeep following arrows upward until no arrow leaves the current class.
Common mistake Treating a wide hierarchy as a single line of classes
Designing From General to Specific
Before writing any code for a hierarchy, the general class must be specified first. It defines the properties common to every member of the category, such as the properties common to every shape. More specific classes such as Circle are then derived from it. Thinking in this order, from general to specific, is a useful habit when designing a hierarchy, because each derived class builds on a base that already exists.
Name the most general class first, decide which properties every member must share, and only then add the specific classes below it. Sketching the chain as a diagram before coding makes the direction of each arrow explicit and lets you check the ancestor chain of every class.
Practice Tracing a Hierarchy
Using the shapes hierarchy, a smiley face is a kind of circle, and a circle is a kind of shape. A new class called Eye is derived from Circle. List the ancestors of Eye in order, moving from the specific class up to the most general class. Then state which class is the derived class in the arrow between Eye and Circle.
Hints
- Start at Eye and follow its arrow to the class it is derived from.
- Keep following arrows upward until no arrow leaves the current class.
- The derived class is the one at the tail of the arrow, which is the class that points toward the more general class.
Summary of the Hierarchy Chain
- 1A class hierarchy is a set of classes ordered in a lattice created by derivation, and it represents concepts that have hierarchical relationships.
- 2Each arrow in an inheritance diagram points from the derived class to the class it is derived from, so arrows always lead toward more general classes.
- 3Membership passes along a chain: if a smiley face is a circle and a circle is a shape, the smiley face is also a shape.
- 4To find the ancestors of a class, follow its arrows upward one step at a time until no arrow remains.
- 5Design a hierarchy from general to specific, defining the general class first and deriving specific classes from it.
At a glance
- 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 an inheritance diagram points from the derived class to the class it is derived from, so arrows lead toward more general classes.
- Ancestors are found by following arrows upward, and membership passes along the whole chain, not just one step.
- Design the general class first, then derive more specific classes from it.