title: What method signature is appropiate returning a generic object?
body: <p>What should be the signature of a method that takes a generic object and returns another generic object, one that either is the same or a sub class of the original class? That is, if the method takes some generic class <code>A</code>, the returned object is guaranteed to be either <code>A</code> or <code>B</code> such that <code>B extends A</code> (directly or indirectly)?</p>
  
  <p>The code below exemplifies what I'm trying to do, in the function <code>getList()</code>:</p>
  
  <pre><code>package com.company;
  import java.util.ArrayList;
  
  public class Main {
      private Main(){
           List&lt;String&gt; stringList = new GenericMessageListCreator.getList(StringGenericMessage.class);
      }
  
      private class GenericMessageListCreator() {
          public List&lt;GenericMessage&lt;T1&gt;&gt; getList(Class&lt;T1 extends GenericMessage&gt; clazz) {
              return new ArrayList&lt;T1&gt;();
          }
      }
  
      private class GenericMessage&lt;T&gt; {
          public GenericMessage(){};
          private T internalValue;
  
          public void setValue(T value) {
              this.internalValue = value;
          }
  
          public void echoValue() {
              System.out.println("I contain " + internalValue);
          }
      }
  
      private class StringMessage extends GenericMessage&lt;String&gt;{}
      private class IntegerMessage extends GenericMessage&lt;Integer&gt;{}
  }
  </code></pre>
  
  <p>Example aside, in actuality I'm writing a registry of classes that are used for <code>Command</code>s in a command pattern. When I get an object by its class I want to fetch the appropriate <code>Command</code> and pass the object to it.</p>