I want to create a common service and common request as shown below:
public interface CommonService {
CommandDTO createOrUpdate(CommonRequest request);
}
Then implement this service as shown below:
public class CompanyARequest extends CommonRequest {
// properties
}
public class CompanyAServiceImpl implements CommonService {
@Override
public CommandDTO createOrUpdate(CompanyARequest request) {
// ...
}
}
However, although CompanyARequest
is inherited from CommonRequest
, createOrUpdate
method throws “Method does not override method from its superclass” error.
On the other hand, if I use generic for the request, the error is gone, but if I use generic for request and dto, there will be too much letter is used and I just want to use generic letters for entities that will be used in CommonService
. SO, how can I fix this problem? Is there any mistake of my implementation?
Advertisement
Answer
You are looking for a generic interface from what I understand:
public interface CommonService<C extends CommonRequest> {
CommandDTO createOrUpdate(C request);
}
public class CompanyAServiceImpl implements CommonService<CompanyARequest> {
@Override
public CommandDTO createOrUpdate(CompanyARequest request) {
// ...
}
}
If you were to define it like you first did, the issue is that any CommonRequest
should be accepted by the method according to its definition in the interface.