Concepts of Programming Languages

(Sean Pound) #1

482 Chapter 11 Abstract Data Types and Encapsulation Constructs


11.4.1 Abstract Data Types in Ada


Ada provides an encapsulation construct that can be used to define a single
abstract data type, including the ability to hide its representation. Ada 83 was
one of the first languages to offer full support for abstract data types.

11.4.1.1 Encapsulation
The encapsulating constructs in Ada are called packages. A package can have
two parts, each of which is also is called a package. These are called the package
specification, which provides the interface of the encapsulation (and perhaps
more), and the body package, which provides the implementation of most, if
not all, of the entities named in the associated package specification. Not all
packages have a body part (packages that encapsulate only types and constants
do not have or need bodies).
A package specification and its associated body package share the same
name. The reserved word body in a package header identifies it as being a
body package. A package specification and its body package may be compiled
separately, provided the package specification is compiled first. Client code can
also be compiled before the body package is compiled or even written, for that
matter. This means that once the package specification is written, work can
begin on both the client code and the body package.

11.4.1.2 Information Hiding
The designer of an Ada package that defines a data type can choose to make
the type entirely visible to clients or provide only the interface information.
Of course, if the representation is not hidden, then the defined type is not an
abstract data type. There are two approaches to hiding the representation from
clients in the package specification. One is to include two sections in the pack-
age specification—one in which entities are visible to clients and one that hides
its contents. For an abstract data type, a declaration appears in the visible part
of the specification, providing only the name of the type and the fact that its
representation is hidden. The representation of the type appears in a part of the
specification called the private part, which is introduced by the reserved word
private. The private clause is always at the end of the package specification.
The private clause is visible to the compiler but not to client program units.
The second way to hide the representation is to define the abstract data
type as a pointer and provide the pointed-to structure’s definition in the body
package, whose entire contents are hidden from clients.
Types that are declared to be private are called private types. Private data
types have built-in operations for assignment and comparisons for equality and
inequality. Any other operation must be declared in the package specification
that defined the type.
The reason that a type’s representation appears in the package specification
at all has to do with compilation issues. Client code can see only the package
Free download pdf