Web hosting mysql - 146 Chapter 6 Similarly, the following Hourly class
146 Chapter 6 Similarly, the following Hourly class extends the Employee class using the extends keyword. public class Hourly extends Employee { public float hourlyRate; //Pay rate public float hoursWorked; //Weekly hours worked public float computePay() { System.out.println( Computing hourly pay for + name); float pay = 0.0F; if(hoursWorked <= 40) { pay = hourlyRate * hoursWorked; } else //Need to compute overtime { pay = (hourlyRate * 40) + (hourlyRate * (hoursWorked - 40) * 1.5F); } return pay; } } A child class has access to the fields and methods in the parent class, depending on the access specifier, which is discussed in Chapter 7, Advanced Java Language Concepts. The computePay() method of the Salary class displays the name of the employee being paid, but there is no name field in the salary class. The name field is in Employee, the parent of Salary. In this example, because name is public, Salary has acces to it and can use it at any point in the Salary class. Notice that the Hourly class prints out the employee s name within its computePay() method, using the name field inherited from the Employee class. Instantiating Child Objects Now that we have defined the Employee, Salary, and Hourly classes, let s look at a program that instantiates and uses these classes. The following Inherit- Demo program creates an Employee, Salary, and Hourly object. Study the program carefully and try to determine its output. public class InheritDemo { public static void main(String [] args) { System.out.println( Instantiating an Employee );
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services