AnimalImpl

Figure 1. One table for the whole hierarchy - implementation of abstract base class
package curam.inheritance;

import curam.inheritance.Animal;
import curam.inheritance.struct.AnimalDtls;
import curam.util.persistence.helper.SingleTableEntityImpl;

abstract class AnimalImpl extends SingleTableEntityImpl<AnimalDtls>
    implements Animal {

  protected AnimalImpl() {
  }

  public String getName() {
    return getDtls().name;
  }

  public void setName(final String value) {
    getDtls().name = value;
  }

}

Class declaration

abstract class AnimalImpl extends SingleTableEntityImpl<AnimalDtls>
  implements Animal {

The implementation class extends the standard class SingleTableEntityImpl, parameterized with the Dtls struct from the single database table (AnimalDtls).

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.

The class implements the Animal interface; note that the class implements only some of the methods required by the interface, leaving others to the subclass implementation, e.g:
  • AnimalImpl provides an implementation for getName and setName, as the behavior is identical for all Animal instances; but
  • AnimalImpl does not provide an implementation for speak, as the behavior will differ between Cat and Dog instances.

Protected constructor

protected AnimalImpl() {
}

Getters and Setters

The getters and setters make use of the SingleTableEntityImpl.getDtls to retrieve the Dtls struct for the single row (in this example an AnimalDtls struct).