Java Reference
In-Depth Information
{
System.out.println(“Getting hours for “ + this.name
+ “ earning “ + salary);
return hoursWorked;
}
}
The PartTimeSalary class has one field and one method; however, a
PartTimeSalary object will have six fields: name, address, SSN, number,
salary, and hoursWorked.
Similarly, the PartTimeSalary class has one method, getHoursWorked(),
but a PartTimeSalary object has three methods: getHoursWorked(),
computePay(), and mailCheck().
Notice that the getHoursWorked() method in the PartTimeSalary class
accesses the name field inherited from Employee, the salary field inherited
from Salary, and the hoursWorked field in its own class.
The this reference is used for accessing this.name in the getHoursWorked()
method of the PartTimeSalary class to emphasize that the name field is a
member of this object, even though name is not a field in this class. The
same is true for accessing salary. Keep in mind that the compiler prefixes
this. to salary and hoursWorked for us, as it would have done with name
had we not explicitly used the this reference ourselves.
The is a relationship should also be maintained. A child is a parent, a child is
a grandparent, and so on. In our example, a part-time salaried employee is a
salaried employee, and a part-time salaried employee is an employee.
The java.lang.Object Class
The Java language API includes a special class named Object that is the root
class of the entire Java hierarchy. The Object class, found in the java.lang pack-
age, is the parent of every Java class, either directly (meaning the class is an
immediate child of Object) or indirectly (meaning Object is an ancestor further
up the inheritance tree).
For example, consider the following Employee class that does not declare a
parent class.
public class Employee
{
//Class definition
}
Search WWH ::




Custom Search