Concepts / Base Classes and Their Role

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.

  • Programming

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.

  • Yes

  • No

  • Only if it is stated directly

Reveal answer
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.

Vehiclemost generalTruckparent of fire engineFire Enginemost specificis a kind ofis a kind of
Which categories sit above the fire engine, and in what order do you reach them when you move upward one link at a time?

What a Class Hierarchy Is

Definition

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.

Shapebase classCirclederived from ShapeSmiley Facederived from Circleis derived fromis derived from
Which class is derived from which, and which way does each arrow point?
Key point

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.

In the real world

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.

Worked example

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.

  1. 1
    Find the direct parent

    The arrow leaving Smiley Face points to Circle, so Circle is the first ancestor.

  2. 2
    Move up again

    The arrow leaving Circle points to Shape, so Shape is the next ancestor.

  3. 3
    Check 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.

Result

The ancestors of Smiley Face are Circle, then Shape.

Smiley Facestarting classCirclefirst ancestorShapetop of chainderived fromderived from
Starting from Smiley Face, what is the order of classes you pass as you move up to the most general class?

Mistakes When Reading a Hierarchy

  • Common mistake

    Reading the arrow in the wrong direction

    What it looks like

    Seeing an arrow from Shape to Circle and concluding that Shape is the more specific class.

    Why it fails

    Each arrow points from the derived class to the class it is derived from, so the arrow always goes from specific toward general.

    Do this instead

    Find 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 like

    Concluding that a fire engine is not a vehicle because the diagram shows no line between them.

    Why it fails

    Membership in a category passes along a chain, so ancestry extends through every intermediate class.

    Do this instead

    Follow the chain one link at a time until no further parent exists.

  • Common mistake

    Designing the specific class before the general one

    Why it fails

    A 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 instead

    Specify 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

EASY

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.

Related concepts

More concepts starting with B