// Helene Martin, CSE 142 // Client code can treat objects from an inheritance // hierarchy in the same way. For example, Secretary // objects can be treated as Employees because they // are guaranteed to have all the behaviors Employee // defines. public class EmployeeMain { public static void main(String[] args) { // Arrays of references of the supertype can refer to // objects of any subtype Employee[] emps = {new Employee(5), new Secretary(30), new Lawyer(2), new LegalSecretary(3)}; for (int i = 0; i < emps.length; i++) { // When these methods are called, Java looks to // the actual object type to know what to do. // e.g. lawyer objects return a bigger value from getSalary() System.out.println(emps[i].getVacationDays()); System.out.println(emps[i].getSalary()); System.out.println(); } // (the code above is what the Critter simulation does -- it has a // list of all currently visible Critter objects and loops through // calling each of their getMove, fight, eat, toString, getColor // methods in that order) Employee elle = new Employee(23); Secretary salvatore = new Secretary(34); coolMethod(salvatore); coolMethod(elle); } // Prints the salary of the given employee. // Can be called with objects of any Employee subclass! public static void coolMethod(Employee e) { System.out.println(e.getSalary()); } }