Chapter 18. Subtyping in a Java Back-End App

Table of Contents

1. Subtyping in Java
2. Case Study 1: Eliminating a Class Hierarchy with Single Table Inheritance
2.1. Make the Java data model
2.2. New issues
2.3. Encode the model classes of the Java data model
2.4. Database schema for single table class hierarchy
2.5. Write the View and Controller Code
3. Case Study 2: Implementing a Class Hierarchy with Joined Table Inheritance
3.1. Make the Java data model
3.2. New issues
3.3. Encode the model classes of the Java data model
3.4. Write the View and Controller Code
4. Run the App and Get the Code

Whenever an app has to manage the data of a larger number of object types, there may be various subtype (inheritance) relationships between some of the object types. Handling subtype relationships is an advanced issue in software application engineering. It is often not well supported by application development frameworks.

In this chapter of our tutorial, we explain two case studies based on fragments of the information model of our running example, the Public Library app, shown above.

In the first case study, we consider the single-level class hierarchy with root Book shown in Figure 18.1 below, which is an incomplete disjoint segmentation. We use the Single Table Inheritance approach for mapping this class hierarchy to a single database table.

Figure 18.1. The object type Book as the root of a disjoint segmentation

The object type Book as the root of a disjoint segmentation

In the second case study, we consider the multi-level class hierarchy consisting of the Person roles Employee, Manager and Author, shown in Figure 18.2 below. We use the Joined Table Inheritance approach for mapping this class hierarchy to a set of database tables that are related with each other via foreign key dependencies.

Figure 18.2. The Person roles hierarchy

The Person roles hierarchy

In both cases we show

  1. how to derive a JPA entity class model,

  2. how to encode the JPA entity class model in the form of JPA entity classes (as model classes),

  3. how to write the view and controller code based on the model code.

1. Subtyping in Java

Java provides built-in support for subtyping with its extends keyword, but it does not support multiple inheritance. Consider the design model shown in Figure 18.3 below.

Figure 18.3. Student is a subclass of Person

Student is a subclass of Person

First we define the superclass Person. Then, we define the subclass Student and the subtyping relationship to Person by means of the extends keyword:

public class Person {
  private String firstName;
  private String lastName;
  ...
}
public class Student extends Person {
  private int studentNo;

  public Student( String first, String last, int studNo) {
    super( firstName, lastName);
    this.setStudNo( studNo);
  }
  ...
}

Notice that in the Student class, we define a constructor with all the parameters required to create an instance of Student. In this subclass constructor we use super to invoke the constructor of the superclass Person.