title: Accessing Subclass Field from Array of Superclass?
body: <p>I just recently started learning java, so I'm having a little trouble understanding what I think is a polymorphism issue. I'm trying to program a chess game, so I have a an array of superclass called <code>GamePiece</code> for which the subclass <code>PawnPiece</code> extends. I have a method in both classes called <code>MovePiece()</code> which changes the position of the piece. When I use <code>MovePiece()</code>, changes the position values of the <code>PawnPiece</code>, but when I try to call the positions in my main code, it gives me the unchanged position of 'GamePiece'. Here's some of my code:</p>
  
  <pre><code>public class GamePiece {
      int position;
  
      //Constructor
      public GamePiece(int x){
         position=x;
      }
      public void MovePiece(int positionOfMove){};
  }
  
  public class PawnPiece extends GamePiece{
      int positionPawn;
  
      //Subclass Constructor
      public PawnPiece(int x){
          super(x);
      }
  
      public void MovePiece(int positionOfMovePawn){
         positionPawn=x;
  }
  
  public classChessMain{
      public static void main(String[] arg){
          GamePiece[] allPieces = new GamePiece[32];
          int startPositionPawn = 9;     //Arbitrary#
          allPieces[8]=new PawnPiece(int startPositionPawn); //index is arbitrary
          int playerMove = 5;     //Arbitrary#
          allPieces[8].MovePiece(playerMove);
      }
  }
  </code></pre>
  
  <p>The last line gives me the initial position (9 in this case), but I know if I could access the position of <code>PawnPiece</code>, it would give me 5. Any help all you coding wizards out there? I'd really appreciate it.</p>