Java Reference
In-Depth Information
super(name, address, number);
setSalary(salary);
}
public void mailCheck()
{
System.out.println(“Within mailCheck of Salary class”);
System.out.println(“Mailing check to “ + getName()
+ “ with salary “ + salary);
}
//The remainder of the class definition...
}
Listing 8.8
(continued)
The mailCheck() method in the Employee class displays a message contain-
ing the employee's name and address. The mailCheck() in Salary displays a
message stating that execution is in Salary as well as the employee's name and
salary. Try to determine the output of the following statements:
Salary s = new Salary(“Thomas Jefferson”, “Monticello, VA”, 3, 2400.00);
s.mailCheck();
Because s refers to a Salary object, mailCheck() from the Salary class is
invoked and the output is:
Within mailCheck of Salary class
Mailing check to Thomas Jefferson with salary 2600.0
Now, let me put a spin on this example. Suppose that I instantiate a Salary
object and use an Employee reference to it, as I did in the VirtualDemo program
in Listing 8.9. I want you to study this program carefully and try to determine
its output. In particular, I want you to pay close attention to the statement:
e.mailCheck();
where e is an Employee reference referring to a Salary object.
public class VirtualDemo
{
public static void main(String [] args)
{
Salary s = new Salary(“Thomas Jefferson”, “Monticello, VA”,
3, 2600.00);
Listing 8.9
The VirtualDemo program demonstrates virtual methods in Java. (continued)
Search WWH ::




Custom Search