0

I have created an abstract base class BaseModelDao with three constructors. When I create a class SubscriberScoreDao that extends BaseModelDao I have to redefine all three constructors in the subclass in order to avoid compile time errors. Is there a way to take advantage of the constructors I have defined in my BaseModelDao without having to reimplement the same logic in every subclass?

BaseModelDao

public abstract class BaseModelDao<T extends Model> {

    private static final String TAG = BaseModelDao.class.getSimpleName();

    private List<T> mModelList;

    protected BaseModelDao() {
        mModelList = new ArrayList<>();
    }

    protected BaseModelDao(Response<T>[] responseArray) {
        mModelList = fromResponseArray(responseArray);
    }

    protected BaseModelDao(Response<T> response) {
        mModelList = fromResponse(response);
    }

    public List<T> getModelList() {
        return mModelList;
    }

    public abstract Class<T> getModelClass();

    private List<T> fromResponse(Response<T> response) {
        List<T> responseList = response.getResultData();
        return responseList;
    }

    public List<T> fromResponseArray(Response<T>[] responseArray) {
        return fromResponse(getResponseObjectFromArray(responseArray));
    }

    // more helper methods...

}

SubscriberScoreDao

public class SubscriberScoreDao extends BaseModelDao<SubscriberScore> {

    public static final String TAG = SubscriberScoreDao.class.getSimpleName();

    public SubscriberScoreDao(){
        super();
    }

    public SubscriberScoreDao(Response<SubscriberScore>[] responseArray) {
        super(responseArray);
    }

    public SubscriberScoreDao(Response<SubscriberScore> responseArray) {
        super(responseArray);
    }

    @Override
    public Class<SubscriberScore> getModelClass() {
        return SubscriberScore.class;
    }
}

The constructors shown above are the ones I am trying to eliminate. When I want to use the SubscriberScoreDao in code it looks like this.

    LendingRestClient.getInstance().getSubscriberScoring(new Callback<Response<SubscriberScore>[]>() {
        @Override
        public void success(Response<SubscriberScore>[] responseArray, retrofit.client.Response response) {
            mSubscriberScoreDao = new SubscriberScoreDao(responseArray);
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });

If the three constructors that call super() are not defined in the SubscriberScoreDao then the code throws a compile time error at this line:

mSubscriberScoreDao = new SubscriberScoreDao(responseArray);

Error: Error Message

Is there a way to not define the constructors in every subclass and avoid this error?

Andrea Thacker
  • 3,440
  • 1
  • 25
  • 37

1 Answers1

1

You could declare the constructor (in the base class) with a vararg:

class Super<T> {
    private List<T> responses;

    public Super(Response<T>...responses) {
        this.responses = Arrays.asList(responses);
    }
}

Your subclass would only have to declare 1 constructor, which takes care of the functionality of all 3 of the constructors you have.

class Sub extends Super<SubscriberScore> {
    public Sub(Response<SubscriberScore>...responses) {
        super(responses);
    }
}

You can now instantiate Sub as:

new Sub();
new Sub(new Response<SubscriberScore>());
new Sub(new Response<SubscriberScore>[] {

});
Vince
  • 14,470
  • 7
  • 39
  • 84