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:
Is there a way to not define the constructors in every subclass and avoid this error?