title: Java generic design
body: <p>I've <code>ErrorFactory</code> class which based on error code creates an instance of different type of Errors. Also I have <code>Output</code> class which might contains one of those error types. </p>
  
  <p>Code below shows how I am trying to use it, but unfortunately getting error. Could you please help me figure out what is wrong there and suggest better design. Thanks for any help! </p>
  
  <pre><code>OutputLike output = new OutputLike();
  ErrorFactory errorFactory = new ErrorFactory();
  int errorCode = ((Long)error.get("error_code")).intValue();
  output.setError(errorFactory.&lt;Error&gt;getError(errorCode, error));
  
  
  Error:(97, 27) java: method setError in class com.electronsoftware.VKLiker.responseoutput.output.Output&lt;T&gt; cannot be applied to given types;
    required: com.electronsoftware.VKLiker.error.Error
    found: java.lang.Object
    reason: actual argument java.lang.Object cannot be converted to com.electronsoftware.VKLiker.error.Error by method invocation conversion
  </code></pre>
  
  <p>======</p>
  
  <pre><code>public class ErrorFactory &lt;T extends Error&gt; {
      public &lt;T&gt; T getError(int error_code, JSONObject errorBody){
          Error.ErrorType errorType = Error.ErrorType.values()[error_code];
  
          switch(errorType){
              case CAPTCHA_NEEDED:{
                  return (T) Parser.parseCaptchaRequiredError(errorBody);
              }
              case USER_AUTHORIZATION_FAILED:{
                  break;
              }
              case TOO_MANY_REQUESTS:{
                  break;
              }
              case NOT_ENOUGH_PERMISSIONS:{
                  break;
              }
              case UNKNOWN:{
                  break;
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>====</p>
  
  <pre><code>public class OutputLike &lt;E extends Error&gt; extends Output &lt;E&gt; {
      ....
  }
  
  public abstract class Output &lt;T extends Error&gt; {
  
      private boolean isError;
      private T error;
  
      public boolean isError(){
          return this.isError;
      }
      public T getError(){
          return this.error;
      }
      public void setError(T error){
          this.error = error;
      }
  }
  </code></pre>
  
  <p>====</p>
  
  <pre><code>public static ErrorCaptchaRequired parseCaptchaRequiredError(JSONObject error){
  
      ErrorCaptchaRequired captchaError = null;
      ....
      captchaError = new ErrorCaptchaRequired(msg, captchaId, captchaUrl);
      return captchaError;
  }
  </code></pre>
  
  <p>====</p>
  
  <pre><code>public class ErrorCaptchaRequired extends Error {
  ........
  }
  </code></pre>