Concepts of Programming Languages

(Sean Pound) #1

562 Chapter 12 Support for Object-Oriented Programming


procedure Do_It (A : T) is abstract;
end Base_Pkg;

12.9.4 Child Packages


Packages can be nested directly in other packages, in which case they are called
child packages. One potential problem with this design is that if a package has
a significant number of child packages and they are large, the nesting package
becomes too large to be an effective compilation unit. The solution is relatively
simple: Child packages are allowed to be physically separate units (files) that
are separately compilable, in which case they are called child library packages.
A child package is declared to be private by preceding the reserved word
package with the reserved word private. The logical position of a private
child package is at the beginning of the declarations in the specification pack-
age of the nesting package. The declarations of the private child package are
not visible to the nesting package body, unless the nesting package includes a
with clause with the child’s name.
One important characteristic of a child package is that even the private
parts of its parent are visible to it. Child packages provide an alternative to
class derivation, because of this visibility of the parent entities. So, the private
parts of the parent package are like protected members in a parent class where
a child package is used to extend a class.
Child library packages can be added at any time to a program. They do not
require recompilation of the parent package or clients of the parent package.
Child library packages can be used in place of the friend definitions in C++.
For example, if a subprogram must be written that can access the members of
two different classes, the parent package can define one of the classes and the
child package can define the other. Then, a subprogram in the child package
can access the members of both. Furthermore, in C++ if the need for a friend
is not known when a class is defined, it will need to be changed and recompiled
when such a need is discovered. In Ada 95+, new classes in new child packages
can be defined without disturbing the parent package, because every name
defined in the parent package is visible in the child package.

12.9.5 Evaluation


Ada offers complete support for object-oriented programming, although users
of other object-oriented languages may find that support to be both weak
and somewhat complex. Although packages can be used to build abstract data
types, they are actually more generalized encapsulation constructs. Unless child
library packages are used, there is no way to restrict inheritance, in which case
all subclasses are subtypes. This form of access restriction is limited in com-
parison to that offered by C++, Java, and C#.
C++ clearly offers a better form of multiple inheritance than Ada 95.
However, the use of child library units to control access to the entities of
Free download pdf