One table per concrete class

If you choose this option, you will create one physical database table for each concrete class, in this example Cat and Dog. The abstract class will have no table of its own; instead, the abstract fields will be replicated on each of the concrete tables.

You must provide the following implementation classes (listed in dependency order):

These classes are described in detail below. The concrete (Cat and Dog) implementation classes are reasonably straightforward, but the abstract (Animal) classes are more complex.

AnimalImpl

Figure 1. One table per concrete class - implementation of abstract base class
package curam.inheritance;

import curam.inheritance.Animal;
import curam.util.persistence.helper.SingleTableEntityImpl;
import curam.util.type.DeepCloneable;

abstract class AnimalImpl<DTLS_STRUCT extends DeepCloneable>
    extends SingleTableEntityImpl<DTLS_STRUCT> implements Animal {

  public void printName() {
    System.out.println("My name is " + getName());
  }

}

You may provide this implementation if there is any common behavior between your concrete classes which is identical.

Note: Although the behavior of attribute getters and setters for the base class is conceptually identical for all Animal instances, technically they differ since:
  • Cat instances will store their Animal attributes on the Cat table; and
  • Dog instances will store their Animal attributes on the Dog table.

Hence the implementation of Animal getters and setters cannot be implemented in a central place.

The class is parameterized with the name of the Dtls struct, to be supplied by the implementing subclass.

The class is package-protected and marked abstract. In this example the subclasses will be placed in the same code-package; if you require some of your subclasses to be in a different package, you will need to mark your abstract implementation class public.

If there is no common implementation logic, you may omit this class, and instead concrete classes will inherit from SingleTableEntityImpl (or some other suitable class) directly.