Java Reference
In-Depth Information
public void payEmployee(Salary s)
{
s.computePay();
}
public void payEmployee(Hourly h)
{
h.computePay();
}
}
This Boss class has three overloaded versions of payEmployee(), one for
each type of employee, and therefore needs to refer to three classes in the pay-
roll package: Employee, Salary, and Hourly. The statement:
import payroll.*;
tells the compiler to look in the payroll package when attempting to locate the
classes used by Boss; therefore, this Boss class does not need to use the payroll
prefix when referring to classes from the payroll package.
You never need to use import statements, because you can always refer to
a class by its fully qualified name (the name of the class prefixed with its
package name). The import keyword is strictly a convenience. In fact, when
compiling your classes, the compiler removes all import statements and
replaces all class names with their fully qualified name. That said, you will
use import statements all the time. Using the fully qualified name for every
class is tedious and can actually make your code more difficult to read.
The third option when using a class from another package is to import the
class itself. The following Boss class individually imports each class that it is
using from another package.
package management;
import payroll.Employee;
import payroll.Hourly;
import payroll.Salary;
public class Boss
{
public void payEmployee(Employee e)
{
e.mailCheck();
}
public void payEmployee(Salary s)
{
s.computePay();
}
Search WWH ::




Custom Search