title: Java - Handling generics with inheritance
body: <p>I have a class that represents a vendor service and all their services have an authentication and an execute method.</p>
  
  <p>I started thiking of an abstract class that represents this as below.
  The thing is each of their services require a different request object, so I thought of using generics.
  The problem is that if use it, I can't handle the specifics of each request object. Each children must use some methods from the type.</p>
  
  <p>1) Should I try to make this way I'm trying, or remove this <code>executeRequest</code> method from the abstract class and each subclass implement it with the correct type?</p>
  
  <p>2) I always hear "prefer composition over inheritance". Should I move the <code>executeRequest</code> to an interface?</p>
  
  <p>Thanks in advance!</p>
  
  <pre><code>public abstract class VendorService  {
  
      private final VendorInitialization VendorInitialization;
  
      //a bean with some auth params
      public VendorService(VendorInitialization VendorInitialization) {
          this.VendorInitialization = VendorInitialization;
      }
  
      protected abstract &lt;T&gt; boolean validateRequest(T requestObject) throws VendorServiceBadRequest;
  
      protected abstract &lt;T, P&gt; P executeRequest(T requestObject);
  }
  
  
  public class VendorServiceAllocation extends VendorService {
  
      public VendorServiceAllocation(VendorInitialization VendorInitialization) {
          super(VendorInitialization);
      }
  
      @Override
      protected &lt;T&gt; boolean validateRequest(T requestObject) throws VendorServiceBadRequest {
  
          //List&lt;BeanAllocation&gt; requestObject = new Arraylist&lt;&gt;(); //I was using like this before
  
          //TODO: how to handle it as list of on this specific case?
          if (requestObject == null || requestObject.size() == 0) {
              throw new VendorServiceBadRequest(String.format("The list must have at least one element"));
          }
  
          //TODO: requestObject.get(0).getMySpecificFieldFromBeanAllocation will not work
  
          //some checks
          return true;
      }
  
      @Override
      protected &lt;T, P&gt; P executeRequest(T requestObject) {
          //executes and return a list of objects specific to this class
          return new List&lt;BeanAllocationResponse&gt;();
      }
  }
  </code></pre>
  
  <p>Edit, for clarification:
  In the child class <code>VendorServiceAllocation</code>, I need to use some methods that are specific of that type. </p>
  
  <p>E.g.: Inside <code>executeRequest</code>, I need to call <code>requestObject.customFunctionFromChild()</code></p>