What are the benefits of creating subclasses of an abstract class for every different type?

Viewed 26
  1. My first question is more of a design question - In my spring boot application I'm defining an Address model

As you can see there can be multiple subtypes of this Address class such as office, residential_permanent, residential_temporary which I'm defining by an enum AddressType. Now my first question is should I create concrete instances out of this address class and simply differentiate based on the value of attribute AddressType or should I make this Address class as abstract and for each AddressType create a subclass extending Address. Something like this -

@Entity
public abstract class Address extends BaseModel {
    private AddressType addressType;
    private String city;
    private String state;
    private String country;
    public Address(AddressType addressType) {
        this.addressType = addressType;
    }
}

public enum AddressType {
    RESIDENTIAL_PERMANENT,
    RESIDENTIAL_TEMPORARY,
    OFFICE,
}

@Entity
public class ResidentialPermanent extends Address {
    public ResidentialPermanent() {
        super(AddressType.RESIDENTIAL_PERMANENT);
    }
}

According to my understanding I see the following pros and cons of using the abstract class approach :

PROS:

  • In future requirements may change and I might need to handle each different AddressType differently so having separate classes makes sense. CONS:
  • Fairly complicated architecture for something as simple as Address. And would require additional joins when querying database.
  1. Now my second question is ORM related Should I annotate both the abstract class and it's child classes as entity or only one of them and if so which one?
0 Answers
Related