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.

Edward_the_Elder_-_MS_Royal_14_B_VI

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.