Sometimes in programming we want to reuse a class. One of the most common ways to reuse a class is through something called inheritance.
A program can contain many classes. Some of these classes can be organized into ordered levels of functionality. When we have an ordered level of functionality, this is called a hierarchy. In fact, you are in such a hierarchical program at this moment:
So say we want to create a hierarchy of our own. How do we do it?
We begin by defining a class that contains methods and fields that are common to all classes in the hierarchy. Methods are the verbs and fields are the nouns. Fields come in two flavors: instance variables and class variables. Java Concept Of The Day has a great explanation of the difference between these two kinds of variables.
Then we define new classes at the next level of the hierarchy, which inherit the behavior and fields of the original class. Some books call these inheriting levels ‘lower levels’ but that is just… well… idiotic. What should be called lower level are the quarks, and the stars that inherit their behavior are higher level in the hierarchy.
Yes. I am openly defying whoever in computer science decided to invert what we call up and down. I think the guy should die and his followers too. In every other science the emergent properties are called higher, not lower.
Okay, back to the task.
In the new classes, we define additional fields and more specific methods.
One will not find writ in the Standard Model of Particle Physics a single mention of late 20th-Century German Socialism. And yet, the motions and thoughts of Günter Herburger and all the other highly specific actors of the human stage inherit the burden of obeying the Standard Model. We are made of its fields and play by its methods. …As in nature, so in Java.
The original class is called the superclass, and the new classes that inherit from the superclass are called subclasses. Biology inherits from chemistry and chemistry inherits from physics. Physics would be the superclass in an academic department. That is, until the mathematician gets word.
Some object oriented programming developers call a superclass the base class and call a subclass the derived class. As in science, a superclass can have multiple subclasses, and each subclass can be a superclass of other subclasses and so on. Physics has thermodynamics and biophysics, and these in turn spawn more specialized sub-disciplines. Thus, a class can be both a subclass and a superclass. In contrast to science, however, Java subclasses inherit directly from only one superclass.
A subclass can add fields and methods, some of which may override, or hide, a field or method inherited from a superclass. In real life, the best example of this is the Born Rule of quantum mechanics. Our observed reality is a subclass of the universal wavefunction which, by the Born Probabilities, masks the rest of the program (i.e. infinite madness).
So let’s look at an example. One of the most important things in the world (at least for those of us who want to be effective in the world) is to understand statistics. So lets use a statistics example.
An important concept in both the truth-seeking enterprise of science and the money-seeking enterprise of capitalism is statistical dispersion. There are several kinds of statistical dispersions used to extract insights from data. One kind of statistical dispersion is variance, and another is standard deviation. Standard deviation can be further subdivided into corrected-sample standard deviation and uncorrected-sample standard deviation.
So lets turn the statistical dispersion types into classes and construct a hierarchy. We define a StatisticalDispersion class as a superclass. We then define a Variance class that inherits from StatisticalDispersion. We also define a StandardDeviation class, which also inherits from StatisticalDispersion. We further refine our classes by defining an UncorrectedSampleStandardDeviation class and a CorrectedSampleStandardDeviation class, both of which inherit from the StandardDeviation class. The figure below depicts a subclass and superclass relation.
Arrows pointing from a subclass to a superclass indicate that the subclass refers to the superclass for some of its methods and fields. What is at bottom is StatisticalDispersion. It is from this class that all the other classes inherit code. Don’t allow the direction of the arrows to confuse you.
The entire hierarchy can be represented using a UML (Unified Modeling Language) diagram. The boxes below the class name are available for specifying instance variables and methods for each class. For simplicity, I will leave those boxes blank. Later, I will illustrate UML diagrams complete with fields and methods.
StatisticalDispersion Class Hierarchy
The Java Class Library contains many class hierarchies. At the root of all Java class hierarchies is the Object class, the superclass for all classes. Thus, all classes inherit from the Object class. The most important advantage to inheritance is that in a hierarchy of classes, we write the common code only once. After the common code has been tested, we can reuse it with confidence by inheriting it into the subclasses. And when that common code needs revision, we need to revise the code in only one place.