title: Calling child methods while using polymorphism in an arraylist
body: <p>I have a small project that I'm working with inheritance and polymorphism.  I have an ArrayList of type Employee that contains both Hourly and Salary employee objects.  I would like to be able to use a for loop to call a <code>calcPay</code> function in the Hourly class provided the object in the ArrayList of type Employee is an Hourly employee.  The line</p>
  
  <pre><code>System.out.println("Wage: " e.calcPay());
  </code></pre>
  
  <p>Gives the error <code>The method calcPay() is undefined for type employee</code>.  How do you downcast the object?  I've looked in a lot of forums and I couldn't find an option that would allow me to do it inline or without writing an abstract method that I'd have to include in all of the child classes of Employee.  Any insight would be much appreciated.</p>
  
  <pre><code>public class Driver {
  
       public static void main(String[] args) {
  
            ArrayList&lt;Employee&gt; list = new ArrayList&lt;Employee&gt;();
            Employee emp1 = new Hourly("Hourly Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789", 12.75);
            Employee emp2 = new Salary("Salary Emp", "123 E Center", "555-555-5555", 00001, "123-45-6789");
  
            list.add(emp1);
            list.add(emp2);
  
            for(Employee e : list){
                if(e instanceof Hourly)
                {
                    System.out.println("Wage: " e.calcPay());
                }
             }
      }
  
      public abstract class Employee {
  
          private String name, address, phone, ssn;
          private int empNo;
  
          Employee(String name, String address, String phone, int empNo, String ssn)
          {
              this.name = name;
              this.address = address;
              this.phone = phone;
              this.empNo = empNo;
              this.ssn = ssn;
          }
      }
  
  
      public class Hourly extends Employee {
  
          private double wage;
  
          Hourly(String name, String address, String phone, int empNo, String ssn, double wage) 
          {
              super(name, address, phone, empNo, ssn);        
                      this.wage = wage;
          }
  
          public double calcPay(double hours)
          {
                  return wage * hours;
          }
     }
  </code></pre>