title: How to 'cast' to a generic object and execute method with generic parameter
body: <p>I have a class which initializes some GUI components when they are requested:</p>
  
  <pre><code>public class ComponentInitializer&lt;T extends View&gt;
  
  public abstract void initialize(T object);
  </code></pre>
  
  <p>Now here is the snippet which initializes all components:</p>
  
  <pre><code>private &lt;T extends View&gt; void initComponents(View view) {
      // ArrayList&lt;Component&lt;? extends View&gt;&gt; this.componentInitializers
      for (ComponentInitializer&lt;? extends View&gt; initializer : this.componentInitializers) {
          View object = view.findViewById(initializer.getResourceId());
  
          if (object instanceof CheckBox) {
              ((ComponentInitializer&lt;CheckBox&gt;) initializer).initialize((CheckBox) object);
          }
          else if (object instanceof RadioButton) {
              ((ComponentInitializer&lt;RadioButton&gt;) initializer).initialize((RadioButton) object);
          }
          else if (object instanceof EditText) {
              ((ComponentInitializer&lt;EditText&gt;) initializer).initialize((EditText) object);
          }
      }
  }
  </code></pre>
  
  <p>That works; I tested it.</p>
  
  <p>But... There are numerous objects extending <code>android.view.View</code> and I'm too lazy to make a code block for each object extending <code>View</code>.</p>
  
  <pre><code>else if (object instanceof ObjectExtendingView) {
      ((ComponentInitializer&lt;ObjectExtendingView&gt;) initializer).initialize((ObjectExtendingView) object);
  }
  </code></pre>
  
  <p>Is it somehow possible to decrease the code snippet to just an unary statement, just like something like this:</p>
  
  <pre><code>for (ComponentInitializer&lt;? extends View&gt; initializer : this.componentInitializers) {
      initializer.initialize(object); // Maybe with some casts
  }
  </code></pre>
  
  <p>If yes, how? If not, what would be a workaround?</p>