Java Inheritance Design

We say that an “is a” relationship exists between a subclass and a superclass; that is, a subclass object “is a” superclass object. If you have been following the series, in our example we could define a statistical divergence class hierarchy with a StatisticalDivergence superclass and derive a Variance subclass. A variance “is a” statistical dispersion, but actually a special type of statistical dispersion.


We could also define a King of the Anglo-Saxons class hierarchy with an EdwardTheElder superclass and derive ChildEdwardTheElder and LaterLifeEdwardTheElder.

ChildEdwardTheElder and LaterLifeEdwardTheElder are subclasses, because childhood and later life are both special types of Edward the Elder. To design classes for inheritance, our superclass should define fields and methods that will be common to all classes in the hierarchy.

If you want ChildEdwardTheElder and LaterLifeEdwardTheElder to have a sword, you code the sword into EdwardTheElder.

Each subclass will provide specialization by adding methods and fields. Where appropriate, subclasses can also provide new versions of inherited methods. Maybe you want ChildEdwardTheElder to have a wooden sword and LaterLifeEdwardTheElder to have an iron sword. This is called overriding methods.

Let’s get back to building our statistical divergence class hierarchy, which is much easier than building persons. We start by defining a generic StatisticalDispersion superclass. The StatisticalDispersion class will contain the fields and methods that are common to all kinds of statistical dispersion. Then we will define a Variance class that inherits from the StatisticalDispersion class. The Variance class will add instance variables and methods that specifically relate to the measurement of variance. The class hierarchy is shown in the UML diagram in the figure below.

Screen Shot 2018-07-01 at 2.39.16 PM


In this diagram, the instance variables are displayed in the box immediately below the class name and the methods in the next lower box. A “+” preceding a class member indicates that the member is public, while a “–” indicates that the member is private.

Each method’s signature is given with each parameter and its type within parentheses. The return type is not considered part of the signature and is placed after the colon.

The Object class has more methods than are indicated on the UML diagram. That is why this is used: (+…()).

Java Inheritance Part 2

The syntax for defining a subclass class that inherits from another class is to add an extends clause in the class header:

Screen Shot 2018-06-30 at 1.54.33 PM

The extends keyword specifies that the subclass inherits members of the superclass. That means that the subclass begins with a set of predefined methods and fields inherited from its hierarchy of superclasses.

JFrame allows us to create graphical applications. So we can use that to create our subclass of StatisticalDispersion and all of its inheriting subclasses.

Here, StatisticalDispersion is the subclass under JFrame. And it inherits from all the classes that JFrame inherits from, all the way back to the Object class.

Screen Shot 2018-06-30 at 3.24.57 PM

The StatisticalDispersion Class Hierarchy

We are coding a class named StatisticalDispersion that extends the JFrame class, so we use the following header:

Screen Shot 2018-06-30 at 3.44.19 PM

Because our StatisticalDispersion class extends JFrame, it inherits more than 300 methods and more than 30 fields. That’s because the JFrame class is a subclass of Frame, which is a subclass of Window, which is a subclass of Container, which is a subclass of Component, which is a subclass of Object.

A hierarchy is composed of subclasses which inherit methods and fields. Here, we make all of them available to the StatisticalDispersion class.

Subclasses do not necessarily need to use their inherited methods, but these are available if needed. The programmer does not need to write methods and define fields in classes which have already inherited them.

JFrame is the direct superclass of our StatisticalDispersion class. As you can see in the image, StatisticalDispersion refers to it. And as you can see in the code, JFrame follows the extends clause.

A class can have multiple direct subclasses but only one direct superclass. One can have many offspring but yet can only develop from one zygote.

Inheritance In Java

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.

Screen Shot 2018-06-29 at 8.56.14 PM

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.

Screen Shot 2018-06-29 at 9.55.27 PM

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.