Java Reference
In-Depth Information
Employee e = new Employee();
e.name = “Robert Smith”;
e.address = “111 Java Street”;
e.SSN = 999001111;
e.number = 1;
System.out.println(“Instantiating a Salary”);
Salary s = new Salary();
s.name = “Jane Smith”;
s.address = “222 Oak Drive”;
s.SSN = 111009999;
s.number = 2;
s.salary = 100000.00F;
System.out.println(“Instantiating an Hourly”);
Hourly h = new Hourly();
h.name = “George Washington”;
h.address = “333 Espresso Lane”;
h.SSN = 111990000;
h.number = 3;
h.hourlyRate = 10.00F;
h.hoursWorked = 50;
System.out.println(“Paying employees”);
//e.computePay(); //Does not compile!
System.out.println(s.number + “ “ + s.computePay());
System.out.println(h.number + “ “ + h.computePay());
System.out.println(“Mailing checks”);
e.mailCheck();
s.mailCheck();
h.mailCheck();
}
}
The InheritDemo program starts by instantiating each of the three employee
types and initializing their fields. Up until the “Paying employees” statement,
the only output is:
Instantiating an Employee
Instantiating a Salary
Instantiating an Hourly
The computePay() method is then invoked on the Salary and Hourly
objects. Notice within main() the salary object can access the number field
inherited from its parent using:
s.number
The same is true for the Hourly object, which accesses the number field
using the statement:
h.number;
Search WWH ::




Custom Search