CHAPTER 11 REVIEW
QUESTIONS
1. What are the two kinds of abstractions in programming
languages?
- process abstraction and data abstraction
2. Define ADT
- ADT is a data type that is hidden from the program
units, and the declaration of the type and the protocols, are contained in a
single syntactic unit
3. What are the advantages of the two parts of the
definition of abstract data type?
- Information
hiding, improved readability, and easier to manage
4. What are the language design requirements for a language
that supports abstract data types?
- Among the new ideas of the last 50 years in programming
methodologies and programming language design, data abstraction is one of the
most profound. We begin by discussing the general concept of abstraction in programming
and programming languages. Data abstraction is then defined and illustrated
with an example. This topic is followed by descriptions of the support for data
abstraction in Ada, C++, Objective-C, Java, C#, and Ruby.
5. What are the language design issues for abstract data
types?
- So, the first design issue for abstract data types is
the form of the container for the interface to the type. The second design
issue is whether abstract data types can be parameterized. For example, if the
language supports parameterized abstract data types, one could design an
abstract data type for some structure that could store elements of any type.
Parameterized abstract data types are discussed in Section 11.5. The third
design issue is what access controls are provided and how such controls are
specified. Finally, the language designer must decide whether the specification
of the type is physically separate from its implementation (or whether that is
a developer choice).
6. Explain how information hiding is provided in an Ada
package.
- 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.
7. To what is the private part of an Ada package
specification visible?
- 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.
8. What is the difference between private and limited
private types in Ada?
- 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. An alternative to private types is a more
restricted form: limited private types. Nonpointer limited private types are described in the private section of
a package specification, as are nonpointer private types. The only syntactic
difference is that limited private types are declared to be limited private in
the visible part of the package specification. that defined the type.
9. What is in an Ada package specification? What about a
body package?
- 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.
10. What is the use of the Ada with clause?
- The with clause makes the names defined in external
packages visible; in this case Ada.Text_IO, which provides functions for input
and output of text.
11. What is the use of the Ada use clause?
- The use clause eliminates the need for explicit
qualification of the references to entities from the named package.
44. What is a Java package, and what is its purpose?
- A Java package is a mechanism for organizing Java
classes into namespaces similar to the modules of Modula. Java packages can be
stored in compressed files called JAR files, allowing classes to download
faster as a group rather than one at a time. Programmers also typically use
packages to organize classes belonging to the same category or providing
similar functionality.
PROBLEM SET
3. Write an analysis of the similarities of and
differences between the access specifier of Java and C++
- Instead of controlling blocks of declarations
like C++ does, access specifiers (public, private and protected) are placed on
each definition for each member of a class. Without an explicit access
specifier, the element defaults to “friendly,” which means it is accessible to
other elements in the same package (equivalent to them all being friends) but inaccessible
outside the package. The class, and each method within the class, has an access
specifier to determine whether it’s visible outside the file. Sometimes the
private keyword is used less in Java because “friendly” access is often more
useful than excluding access from other classes in the same package (however,
with multithreading the proper use of private is essential). The Java protected
keyword means “accessible to inheritors and to others in this package.” There
is no equivalent to the C++ protected keyword which means “accessible to
inheritors only” (private protected used to do this, but it was removed).
4. What are the advantages of the nonpointer concept in
Java?
- Most studies agree that pointers are one of the primary
features that enable programmers to inject bugs into their code. Given that
structures are gone, and arrays and strings are objects, the need for pointers
to these constructs goes away. Thus, Java has no pointer data types. Any task
that would require arrays, structures, and pointers in C can be more easily and
reliably performed by declaring objects and arrays of objects. Instead of
complex pointer manipulation on array pointers, you access arrays by their
arithmetic indices. The Java run-time system checks all array indexing to
ensure indices are within the bounds of the array. You no longer have dangling
pointers and trashing of memory because of incorrect pointers, because there
are no pointers in Java.
5. Explain the evolution of the techniques for providing
encapsulations in programming languages over recent years.
- In programming languages, encapsulation is used to
refer to one of two related but distinct notions, and sometimes to the
combination[1][2] thereof: A language mechanism for restricting access to some
of the object's components. A language construct that facilitates the bundling
of data with the methods (or other functions) operating on that data. Some
programming language researchers and academics use the first meaning alone or
in combination with the second as a distinguishing feature of object oriented
programming, while other programming languages which provide lexical closures
view encapsulation as a feature of the language orthogonal to object
orientation.
11. Why is the destructor of C# rarely used?
- A destructor runs after a class becomes unreachable. It
has the special "~" character in its name. The exact time it is
executed is not specified. But it always runs when the class is not reachable
in memory by any references. The conclusion is that destructors are not useful
in many C# programs. In the large, complex programs that need special
destruction logic, the using-dispose pattern is usually better. In simple
programs, neither construct is useful.
12. How are classes in Ruby made dynamic?
- In Ruby, classes are simply objects like any other,
which are then assigned to a constant.
Hence, to create a new class dynamically we instantiate the class Class
with Class.new, and then assign it to a constant via const_set (we invoke it on
Kernel so that it is a top-level constant like any other class). We then add the code that makes up the class
in a do-end block.
In that do-end block, for each field we invoke
define_method twice, first for the getter method and then the setter method
with get_instance_variable and set_instance_variable, respectively. For each field, we create the instance
variables (e.g., for make, we use @make).
Note how we make use of passing the argument in for the setter.