// Kyle Thayer, CSE 142 // Shows an example of Polymorphism public class EmployeeMain { public static void main(String[] args) { LegalSecretary liz = new LegalSecretary(); Employee ed = new Employee(); Secretary sally = new Secretary(); Lawyer lisa = new Lawyer(); //Since Liz is a legal secretary, this calls the specific // legal secretary getSalary() method System.out.println("Liz's salary: " + liz.getSalary()); // Since Ed is a normal employee, it calls the normal // employee getSalary() method System.out.println("Ed's salary: " + ed.getSalary()); //Since Lisa is a Lawyer, we can ask her to sue lisa.sue(); // Sally is not a Lawyer, so we can't ask her to sue //sally.sue(); // We Can also create different types of employees and store these in variables of // type Employee. Employee sasha = new Secretary(); Employee lucas = new Lawyer(); Employee linda = new LegalSecretary(); // Linda is a legal secretary so it calls the specific // legal secretary getSalary() method System.out.println("linda's salary: " + linda.getSalary()); // Sasha is a secretary which inherits the normal employee // getSalary() method System.out.println("sasha's salary: " + sasha.getSalary()); //Even though Lucas is a Lawyer, he is saved in an Employee // variable, and Java doesn't know if any given Employee can sue //lucas.sue(); // Knowing that lucas is a lawyer, I can tell Java to consider // him a lawyer by casting and then ask him to sue ((Lawyer) lucas).sue(); //If I try to cast a non-lawer as a lawyer, Java will compile // but it will break when I try to run it: //((Lawyer) sasha).sue(); // Because Secretary, Lawyer and LegalSecretary are all Employees, we can store these // objects in an array of Employees Employee[] employees = {liz, ed, sally, lisa, sasha, lucas, linda}; for (int i = 0; i < employees.length; i++) { printInfo(employees[i]); } } // Prints the info for an Employee. // Note: Any object that is an Employee (extends Employee) can be passed to // printInfo since they all have the methods called in printInfo public static void printInfo(Employee e) { System.out.println(e.getSalary()); System.out.println(e.getVacationDays()); System.out.println(e.getHours()); System.out.println(e.getVacationForm()); System.out.println(); } }