Java Reference
In-Depth Information
The PayDemo program in Listing 8.7 instantiates one Hourly object, two
Salary objects, and one Employee object, and passes them to the payEmployee()
method. Study the program and try to determine its output.
public class PayDemo
{
public static void main(String [] args)
{
Boss boss = new Boss();
Hourly h = new Hourly(“Abe Lincoln”, “Springfield, IL”,
16, 8.00);
Salary s = new Salary(“George Washington”, “Valley Forge, DE”,
1, 5000.00);
Employee x = new Salary(“Rich Raposa”, “Rapid City, SD”,
47, 250000.00);
Employee y = new Employee(“George W.”, “Houston, TX”, 43);
System.out.println(“** Paying Abe Lincoln **”);
boss.payEmployee(h);
System.out.println(“\n** Paying George Washington **”);
boss.payEmployee(s);
System.out.println(“\n** Paying Rich Raposa **”);
boss.payEmployee(x);
System.out.println(“\n** Paying George W. **”);
boss.payEmployee(y);
}
}
Listing 8.7
The PayDemo program passes Employee objects to the payEmployee() method
of a Boss.
In the PayDemo program, an Hourly reference h and a Salary reference s are
passed in to the payEmployee() method. This is valid because the parameter is
of type Employee and Salary and Hourly objects are Employee objects:
boss.payEmployee(h);
boss.payEmployee(s);
The reference x is of type Employee but refers to a Salary object. When this
object is paid:
boss.payEmployee(x);
the instanceof statement in payEmployee() is true when compared to Salary,
even though the reference is of type Employee, because x actually refers to a
Salary object.
Search WWH ::




Custom Search