Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
To make classes easier to find and to use, to avoid naming conflicts, and to control access, programmers bundle groups of related classes and interfaces into packages.
Definition: A package is a collection of related classes and interfaces providing access protection and namespace management.The classes and interfaces that are part of the Java platform are members of various packages that bundle classes by function: fundamental classes are in
java.lang
, classes for reading and writing (input and output) are injava.io
, and so on. You can put your classes and interfaces in packages, too.Let's look at a set of classes and examine why you might want to put them in a package. Suppose that you write a group of classes that represent a collection of graphic objects, such as circles, rectangles, lines, and points. You also write an interface,
Draggable
, that classes implement if they can be dragged with the mouse by the user://in the Graphic.java file public abstract class Graphic { . . . } //in the Circle.java file public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file public class Rectangle extends Graphic implements Draggable { . . . } //in the Draggable.java file public interface Draggable { . . . }You should bundle these classes and the interface in a package for several reasons:
- You and other programmers can easily determine that these classes and interfaces are related.
- You and other programmers know where to find classes and interfaces that provide graphics-related functions.
- The names of your classes wont conflict with class names in other packages, because the package creates a new namespace.
- You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.