title: Class of a generic Java type cannot be assigned to a variable whose Class type is upper bounded by a generic super type
body: <p>I use Java 8. In my design a have a few simple classes which model value parameters such as <code>FloatParameter</code> or <code>EnumParameter&lt;E&gt;</code>. A have a common generic super class of these classes (<code>GenericParameter&lt;T&gt;</code>) which implements parameter name and its default value. The sub classes implement other attributes specific to them such as range in case of <code>FloatParameter</code>.</p>
  
  <p>Moreover, I want to work with the types of the parameters regardless of their specific type. But I still want to bound the types in the way that they are sub types of <code>GenericParameter&lt;T&gt;</code>. In order to do that, I created a method such as <code>process(Class&lt;? extends GenericParameter&lt;?&gt;&gt; paramType)</code>.</p>
  
  <p>Now, the problem is that the <code>EnumParameter.class</code> <strong>cannot</strong> be assigned to a variable of type <code>Class&lt;? extends GenericParameter&lt;?&gt;&gt;</code> while <code>FloatParameter.class</code> <strong>can</strong> be.</p>
  
  <p>Further I list the code for the classes to make it more clear and reproducible:</p>
  
  <pre><code>public class GenericParameter&lt;T&gt; {
      protected String name;
      protected T defaultValue;
  }
  
  public class FloatGenericParameter extends GenericParameter&lt;Float&gt; {
      ...
  }
  
  public class TypedGenericParameter&lt;T&gt; extends GenericParameter&lt;T&gt; {
      ...
  }
  
  Class&lt;? extends GenericParameter&lt;?&gt;&gt; fgpc = FloatGenericParameter.class; // ok
  Class&lt;? extends GenericParameter&lt;?&gt;&gt; tgpc = TypedGenericParameter.class; // error: incompatible types: Class&lt;TypedGenericParameter&gt; cannot be converted to Class&lt;? extends GenericParameter&lt;?&gt;&gt;
  Class&lt;? extends GenericParameter&gt; tgpc2 = TypedGenericParameter.class; // warning: [rawtypes] found raw type: GenericParameter
  </code></pre>
  
  <p>Finally, when using a non-generic base class, there is no problem:</p>
  
  <pre><code>public class Parameter {
      ....
  }
  
  public class FloatParameter extends Parameter {
      ...
  }
  
  public class TypedParameter&lt;T&gt; extends Parameter {
      ...
  }
  
  Class&lt;? extends Parameter&gt; fpc = FloatParameter.class; // ok
  Class&lt;? extends Parameter&gt; tpc = TypedParameter.class; // ok
  </code></pre>
  
  <p>Please, do you have any suggestions?</p>
  
  <p>I can go with <code>process(Class&lt;?&gt; paramType)</code> as a workaround or do casts, but I wanted to benefit from the static type checking by the compiler.</p>
  
  <p>EDIT:</p>
  
  <p>I would like to use the cast when registering factories that produce GUI components for each parameter type. The code looks like:</p>
  
  <pre><code>addParameterComponentFactory(EnumParameter.class, new ParameterComponentFactory() { ... })
  </code></pre>
  
  <p>In such case, the compiler would check the added parameter type at compile time. Also the code would be more self-explaining.</p>
  
  <p>EDIT 2:</p>
  
  <p>Currently, I am using the suggested approach to introduce a type parameter for the <code>addParameterComponentFactory</code> method. The signature looks like this:</p>
  
  <pre><code>public static &lt;P extends GenericParameter&lt;?&gt;&gt; addParameterComponentFactory(Class&lt;P&gt; clazz, ParameterComponentFactory pcf)
  </code></pre>
  
  <p>With this definition I am able to specify <code>TypedParameter.class</code> (<code>EnumParameter.class</code> - also one type param) as well as I obtain the static type checking.</p>