title: Changing parent object to child object
body: <p>I need a bit of help here. so i have this. I was basically wondering when you create an array of object of a parent class, then change that object to a child class, can I access the methods of that child class and if not why. thanks for any help.</p>
  
  <pre><code>public class Racer {
  
  private String name;
  private int position;
  
  // Constructor
  public Racer()
  {}
  public Racer(String name)
  {
      this.name = name;
      position = 0;
  }
  
  public String getName()
  {
      return name;
  }
  
  
  public int getPosition()
  {
      return position;
  }
  
  public void setPosition(int n)
  {
      position = n;
  }
  public void setName(String n){
      this.name=n;
  }
  }
  </code></pre>
  
  <p>the child class</p>
  
  <pre><code>public class Spartiates extends Racer{
  
  private int energy;
  
  public Spartiates(){
      super();
  }
  
  public Spartiates(String name){
      setName(name);
      setPosition(20);
      energy=100;
  }
  
  public void setEnergy(int energy){
      this.energy=energy;
  }
  
  public int getEnergy(){
      return energy;
  }
  }
  </code></pre>
  
  <p>main class</p>
  
  <pre><code> public class demo{
       public static void main(String[] args){
          Racer [] player = new player[3];
          for(int i=0; i&lt;player.length; i++){
              player[i] = new Spartiates();
          }
          System.out.println(player[1].getEnergy());
    }
  </code></pre>
  
  <p>so here the problem the getEnergy method doesn't work so I was wondering why. If anybody can help it would be very much appreciated. thanks </p>