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 } }
final class CatImpl extends AnimalImpl<Cat, CatDtls> implements Cat {
protected CatImpl() { }
The class has a protected constructor, as is the norm for the implementation classes.
@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.
@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 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.
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.