|Lesson 4||How are Packages used in Java?|
|Objective||Describe Java packages and how they are useful.|
Describe Java Packages
- Describe what packages are and why they are useful.
- List at least two ways to use packages.
This module examines two of Java's most innovative features:
- packages and
Packages are containers for classes and they are used to keep the class name space compartmentalized.
For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some
other class named List stored elsewhere. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.
Through the use of the interface keyword, Java allows you to fully abstract an interface from its implementation. Using an interface, you can specify a set of methods that can be implemented by one or more classes.
The interface, itself, does not actually define any implementation. Although they are similar to abstract classes, interfaces have an additional capability: A class can implement more than one interface and by contrast,
a class can only inherit a single superclass (abstract or otherwise).
In the preceding sections, the name of each example class was taken from the same name space.
This means that a unique name had to be used for each class to avoid name collisions. After a while, without some way to manage the name space, you could runout of convenient, descriptive names for individual classes.
You also need some way to be assured that the name you choose for a class will be reasonably unique and not collide with class names chosen by other programmers. Imagine a small group of programmers fighting over who gets to use the name
"Foobar" as a class name. Or, imagine the entire Internet community arguing over who first named a class "Coffee".
Fortunately, Java provides a mechanism for partitioning the class name space into more manageable chunks. This mechanism is the package. The package is both a naming and a visibility control mechanism.
You can define classes inside a package that are not accessible by code outside that package. You can also define class members that are exposed only to other members of the same package.
This allows your classes to have intimate knowledge of each other, but not expose that knowledge to the rest of the world.
Standard definition for Packages
Packages group together related classes, interfaces, and other subpackages.
Packages can be thought of as subsystems, they are not necessarily applications in themselves, but they contribute major components to an application.
Packages are useful for several reasons, including:
- Packages group together related classes and interfaces.
- Classes and interfaces in a package can use names that are unique within that package, even though these names might conflict with similar names outside the package.
For example, you might use popular names like Stack and List within a package, even though they might conflict with the same name in another package.
- Classes, methods, and variables defined within a package can be restricted to that package.
This last point plays a key role in access control, which we'll take a look at in a moment.