Suppose I have the following:
public abstract class AbstractResponse { // this class is totally empty! } public class ResponseA extends AbstractResponse { // JSON POJO } public class ResponseB extends AbstractResponse { // JSON POJO } public abstract class AbstractClient { public abstract Class getType(); public AbstractResponse readResponse() { ObjectMapper mapper = new ObjectMapper(); AbstractResponse response; try { return (AbstractResponse) mapper.readValue(data, getType()); } catch (IOException e) {...} } } public class ResponseAClient extends AbstractClient { public Class getType() { return ResponseA.class; } } public class ResponseBClient extends AbstractClient { public Class getType() { return ResponseB.class; } }
ResponseA and ResponseB have nothing in common except JSON POJO. As you can see, I use the empty AbstractResponse abstract class to avoid code duplication for readResponse() in the ResponseAClient and ResponseBClient client classes.
My question is :
The wrong practice of using an empty abstract class for such cases, and if so, what is the best way to encode this? I was thinking about using generics, but I have seen many warnings that prevent the use of Java Generics.
Edit: Thanks for the answers. After helpful comments from @Kayaman, I would like to rephrase my question as follows:
Is there a more efficient implementation for the situation described above than using an empty interface / abstract class. It just seems like bad practice has an empty interface / abstract class.
source share