CatImpl

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

import curam.inheritance.Cat;
import curam.inheritance.struct.CatDtls;
import curam.test.codetable.ANIMAL_TYPE;

public class CatImpl extends AnimalImpl<Cat, CatDtls>
  implements Cat {

  protected CatImpl() {
  }

  @Override
  protected String getDiscriminatorValue() {
    return ANIMAL_TYPE.CAT;
  }

  @Override
  protected void mapBaseKeyToConcreteDtls() {
    getConcreteRowDtls().animalID = getBaseRowDtls().animalID;
  }

  public int getNumberOfLivesRemaining() {
    return getConcreteRowDtls().numberOfLivesRemaining;
  }

  public void setNumberOfLivesRemaining(final int value) {
    getConcreteRowDtls().numberOfLivesRemaining = value;
  }

  public void speak() {
    System.out.println("Miaow!  My name is " + getName()
        + " and I have " + getNumberOfLivesRemaining()
        + " lives remaining");
  }

  public void setNewInstanceDefaults() {// none required
  }

  public void crossFieldValidation() {// none required
  }

  public void crossEntityValidation() {// none required
  }

  public void mandatoryFieldValidation() {// none required
  }

}

Class declaration

final class CatImpl extends AnimalImpl<Cat, CatDtls>
  implements Cat {
The class:
  • extends the AnimalImpl class created above, specifying the Cat interface and CatDtls struct as parameters; and
  • implements the Cat interface (which in turn extends the Animal interface).

Constructor

protected CatImpl() {
  }

The class has a protected constructor, as is the norm for the implementation classes.

Specifying the discriminator value

@Override
  protected String getDiscriminatorValue() {
    return ANIMAL_TYPE.CAT;
  }

The class must override the BasePlusConcreteTableImpl.getDiscriminatorValue method to specify the discriminator String value which distinguishes Cat instances from other types of Animal.

In this example a code-table constant is used to provide the String value.

Mapping the base key

@Override
  protected void mapBaseKeyToConcreteDtls() {
    getConcreteRowDtls().animalID = getBaseRowDtls().animalID;
  }

The class overrides the BasePlusConcreteTableImpl.mapBaseKeyToConcreteDtls method, which is called when a new entity instance is stored on the database. Typically, the base row uses the AUTO_ID facility to assign a primary key value on insert, and since (in this example) Animal and Cat share key values, the key value assigned to the Animal.animalID column must also be stored on the Cat.animalID column.

The method makes use of these methods from BasePlusConcreteTableImpl :
  • getBaseRowDtls, to access the AnimalDtls row data; and
  • getConcreteRowDtls, to access the CatDtls row data.

Getters and Setters

The getters and setters make use of the BasePlusConcreteTableImpl.getConcreteRowDtls method to access the CatDtls row data.

Implementations for the getters and setters for the Animal fields are inherited from AnimalImpl.

speak

public void speak() {
    System.out.println("Miaow!  My name is " + getName() +
      " and I have "+ getNumberOfLivesRemaining() +
      " lives remaining");
  }

This class must provide an implementation of the Animal.speak method - recall that this method is not implemented in AnimalImpl, as the logic differs between CatImpl and DogImpl.