title: Access subclass fields from a base class in Java
body: <p>I have a base class called <em>Geometry</em> from which there exists a subclass <em>Sphere</em>:</p>
  
  <pre><code>public class Geometry 
  {
   String shape_name;
   String material;
  
   public Geometry()
   {
       System.out.println("New geometric object created.");
   }
  }
  </code></pre>
  
  <p>and a subclass:</p>
  
  <pre><code>public class Sphere extends Geometry
  {
   Vector3d center;
   double radius;
  
   public Sphere(Vector3d coords, double radius, String sphere_name, String material)
   {
    this.center = coords;
    this.radius = radius;
    super.shape_name = sphere_name;
    super.material = material;
   }
  }
  </code></pre>
  
  <p>I have an ArrayList that contains all <em>Geometry</em> objects and I want to iterate over it to check whether the data from a text file is read in correctly. Here is my iterator method so far:</p>
  
  <pre><code>public static void check()
   {
    Iterator&lt;Geometry&gt; e = objects.iterator();
    while (e.hasNext())
    {
     Geometry g = (Geometry) e.next();
     if (g instanceof Sphere)
     {
      System.out.println(g.shape_name);
      System.out.println(g.material);
     }
    }
   }
  </code></pre>
  
  <p>How do I access and print out the <em>Sphere's</em> radius and center fields?
  Thanks in advance :)</p>