title: Returning an extended class where super class is expected
body: <p>I'm currently working on a game using the <em>Component Pattern</em>, and have always wondered how this is done.
  I have an entity which is practically just a bag of components. Each components extends the Component class, which just has some basic functionality. </p>
  
  <p>Extending the component class, new components are created, for handeling input, graphics etc. Now comes the problem; When I'm trying to get a specific component from the entity, it always returns the basic Component class, which prevents me from using the specific component functions. </p>
  
  <pre><code>    public class GameEntity
  {
      private ArrayList&lt;Component&gt; components;
  
      public GameEntity()
      {
          components = new ArrayList&lt;Component&gt;();
      }
  
      public void addComponent(Component component)
      {
          components.add(component);
      }
  
      public void update()
      {
  
      }
  
     public Component getComponent(Class type)
     {
         for (Component component : components)
         {
              if(component.getClass() == type)
              {
                  //return component as Class;
  
              }
         }
         return null;
     }
  
  
      public void draw(Canvas canvas)
      {
          for (Component component : components)
          {
              component.update();
              component.draw(canvas);
          }
      }
  
  }
  </code></pre>
  
  <hr>
  
  <p>Some example components:</p>
  
  <blockquote>
    <p>public class GraphicsComponent extends Component {</p>
    
    <p>public Bitmap bitmap;
        public Rect currentFrameRect;
        private ArrayList spriteAnimations;
        public SpriteAnimation currentAnimation;  public int                     x = 0;   public int                     y = 50;      public
    GraphicsComponent()   {
            spriteAnimations = new ArrayList();  }</p>
  
  <pre><code>/**
   * Adds image [converts to spriteanimation]
   * @param image
   */
  public void addImage(Bitmap image, String label)
  {
      Rect[] tmpRects = {new Rect(0, 0, image.getWidth(), image.getHeight())} ;
      addAnimation(new SpriteAnimation(
              image, tmpRects, label
      ));
  }
  
  public void addAnimation(SpriteAnimation spriteAnimation)
  {
      spriteAnimations.add(spriteAnimation);
  
     if(currentAnimation == null)
     {
         currentAnimation = spriteAnimation;
     }
  }
  
  
  @Override
  public void update()
  {
     currentFrameRect = currentAnimation.frames[currentAnimation.currentFrame];
  }
  </code></pre>
    
    <p>@Override   public void draw(Canvas canvas)     {</p>
  
  <pre><code>    if(currentAnimation != null)
      {
          currentAnimation.draw(x, y, canvas);
      }     }
  </code></pre>
  </blockquote>
  
  <pre><code>public int getWidth()
  {
      return currentAnimation.frames[currentAnimation.currentFrame].width();
  }
  
  public int getHeight()
  {
      return currentAnimation.frames[currentAnimation.currentFrame].height();
  }
  }
  
  
  public class InteractiveComponent extends Component
  {
      public GraphicsComponent graphics;
  
      public InteractiveComponent(GraphicsComponent graphics)
      {
          this.graphics = graphics;
      }
  
      public boolean isOver(int tapX, int tapY)
      {
          //left top right bottom
          if(tapX &gt; graphics.x &amp;&amp; tapX &lt; graphics.x + graphics.getWidth() &amp;&amp;
             tapY &gt; graphics.y &amp;&amp; tapY &lt; graphics.y + graphics.getHeight()
          )
          {
              return true;
          }
          return false;
      }
  
  }
  </code></pre>
  
  <p>There seem to be some problems with the code formatting, but it should be clear. 
  I can't access any specific functions like getHeight() in the graphicComponent or isOver() from the interactiveComponent because I just get a basic Component returned.</p>
  
  <p>I would like to return a <em>GraphicsComponent</em> or <em>InteractiveComponent</em> based on the Class that I pass into <em>getComponent()</em>.</p>