Concepts of Programming Languages

(Sean Pound) #1

516 Chapter 11 Abstract Data Types and Encapsulation Constructs


statement in place of the type name. For example, if we wanted to import all
of the types in stkpkg, we could use the following:

import stkpkg.*;

Note that Java’s import is only an abbreviation mechanism. No otherwise
hidden external resources are made available with import. In fact, in Java
nothing is implicitly hidden if it can be found by the compiler or class loader
(using the package name and the CLASSPATH environment variable).
Java’s import documents the dependencies of the package in which it
appears on the packages named in the import. These dependencies are less
obvious when import is not used.

11.7.3 Ada Packages


Ada packages, which often are used to encapsulate libraries, are defined in hier-
archies, which correspond to the directory hierarchies in which they are stored.
For example, if subPack is a package defined as a child of the package pack, the
subPack code file would appear in a subdirectory of the directory that stored
the pack package. The standard class libraries of Java are also defined in a
hierarchy of packages and are stored in a corresponding hierarchy of directories.
As discussed in Section 11.4.1, packages also define namespaces. Vis-
ibility to a package from a program unit is gained with the with clause. For
example, the following clause makes the resources and namespace of the
package Ada.Text_IO available.

with Ada.Text_IO;

Access to the names defined in the namespace of Ada.Text_IO must be quali-
fied. For example, the Put procedure from Ada.Text_IO must be accessed as

Ada.Text_IO.Put

To access the names in Ada.Text_IO without qualification, the use clause
can be used, as in

use Ada.Text_IO;

With this clause, the Put procedure from Ada.Text_IO can be accessed sim-
ply as Put. Ada’s use is similar to Java’s import.

11.7.4 Ruby Modules
Ruby classes serve as namespace encapsulations, as do the classes of other lan-
guages that support object-oriented programming. Ruby has an additional
naming encapsulation, called a module. Modules typically define collections of
Free download pdf