Java Reference
In-Depth Information
//Hourly definition...
}
The Employee, Salary, and Hourly classes are all now in the same package,
which is named payroll. These classes do not need to be in the same package;
this was a design decision. Part of designing a Java application is deciding
what packages to create and in which package each class will belong.
The Java naming convention recommends package names be all lowercase,
unless an acronym is part of the name. Typically, your package name will
be multiple terms, and each term is separated by the dot operator. Examples
include java.lang, java.awt.event, org.omg.CORBA, and org.omg.stub
.java.rmi.
The Namespace Created by Packages
Packages create a namespace for all classes. If a class is in a package , the name
of the package becomes a part of the name of the class . The package name is prefixed
to the name of your class, separated by the dot operator.
For example, the Employee class is declared in the payroll package. The
Employee class is now referred to:
payroll.Employee
The Employee class can no longer be referred to as simply Employee. The
package name payroll must be used by any other class outside of the payroll
package that wants to use the Employee class. Similarly, the Salary and Hourly
classes become:
payroll.Salary
payroll.Hourly
Classes in the same package do not need to use the package name when
referring to each other. This is one of the benefits of putting classes that
go together in the same package. For example, the Salary class extends
Employee and can use the name Employee instead of payroll.Employee
because Salary is also in the payroll package.
Packages create a namespace that is useful in avoiding naming conflicts.
Two classes named Employee that both appear in the default package cannot
be distinguished from each other; however, if one of the Employee classes is in
Search WWH ::




Custom Search