Concepts of Programming Languages

(Sean Pound) #1

518 Chapter 11 Abstract Data Types and Encapsulation Constructs


presents the client interface, and a body, which supplies the implementation
of the abstract data type. Data type representations can appear in the package
specification but be hidden from clients by putting them in the private clause of
the package. The abstract type itself is defined to be private in the public part of
the package specification. Private types have built-in operations for assignment
and comparison for equality and inequality.
C++ data abstraction is provided by classes. Classes are types, and
instances can be either stack or heap dynamic. A member function (method)
can have its complete definition appear in the class or have only the proto-
col given in the class and the definition placed in another file, which can be
separately compiled. C++ classes can have two clauses, each prefixed with
an access modifier: private or public. Both constructors and destructors can
be given in class definitions. Heap-allocated objects must be explicitly deal-
located with delete.
As with C++, Objective-C data abstractions are classes. Classes are types
and all are heap dynamic. Methods declarations must appear in interface sec-
tions of classes and method definitions must appear in implementation sections.
Constructors are called initializers; they must be explicitly called. Instance
variables can be private or public. Access to methods cannot be restricted.
Method calls use syntax that is similar to that used by Smalltalk. Objective-C
supports properties and access methods for properties can be furnished by the
compiler.
Java data abstractions are similar to those of C++, except all Java objects
are allocated from the heap and are accessed through reference variables.
Also, all objects are garbage collected. Rather than having access modifiers
attached to clauses, in Java the modifiers appear on individual declarations
(or definitions).
C# supports abstract data types with both classes and structs. Its structs are
value types and do not support inheritance. C# classes are similar to those of Java.
Ruby supports abstract data types with its classes. Ruby’s classes differ
from those of most other languages in that they are dynamic—members can
be added, deleted, or changed during execution.
Ada, C++, Java 5.0, and C# 2005 allow their abstract data types to be
parameterized—Ada through its generic packages, C++ through its templated
classes, and Java 5.0 and C# through their collection classes and interfaces and
user-defined generic classes.
To support the construction of large programs, some contemporary lan-
guages include multiple-type encapsulation constructs, which can contain a
collection of logically related types. An encapsulation may also provide access
control to its entities. Encapsulations provide the programmer with a method
of organizing programs that also facilitates recompilation.
C++, C#, Java, Ada, and Ruby provide naming encapsulations. For Ada
and Java, they are named packages; for C++ and C#, they are namespaces; for
Ruby, they are modules. Partially because of the availability of packages, Java
does not have friend functions or friend classes. In Ada, packages can be used
as naming encapsulations.
Free download pdf