Java Reference
In-Depth Information
Notice that you no longer need to qualify Book with its package name.
Java's Class Library Is Contained in Packages
As explained earlier in this topic, Java defines a large number of standard classes that are
available to all programs. This class library is often referred to as the Java API (Application
Programming Interface). The Java API is stored in packages. At the top of the package hier-
archy is java . Descending from java are several subpackages. Here are a few examples:
Subpackage Description
java.lang Contains a large number of general-purpose classes
java.io Contains I/O classes
java.net Contains classes that support networking
java.applet Contains classes for creating applets
java.awt Contains classes that support the Abstract Window Toolkit
Since the beginning of this topic, you have been using java.lang . It contains, among sev-
eral others, the System class, which you have been using when performing output using
println( ) . The java.lang package is unique because it is imported automatically into every
Java program. This is why you did not have to import java.lang in the preceding sample
programs. However, you must explicitly import the other packages. We will be examining
several packages in subsequent chapters.
Interfaces
In object-oriented programming, it is sometimes helpful to define what a class must do but
not how it will do it. You have already seen an example of this: the abstract method. An
abstract method defines the signature for a method but provides no implementation. A sub-
class must provide its own implementation of each abstract method defined by its super-
class. Thus, an abstract method specifies the interface to the method but not the implement-
ation . While abstract classes and methods are useful, it is possible to take this concept a
step further. In Java, you can fully separate a class' interface from its implementation by
using the keyword interface .
An interface is syntactically similar to an abstract class, in that you can specify one or
more methods that have no body. Those methods must be implemented by a class in order
for their actions to be defined. Thus, an interface specifies what must be done, but not how
to do it. Once an interface is defined, any number of classes can implement it. Also, one
class can implement any number of interfaces.
Search WWH ::




Custom Search