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.
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: (+…()).