Concepts of Programming Languages

(Sean Pound) #1
12.7 Support for Object-Oriented Programming in Java 553

used for storage reclamation. Like many other language features, although
garbage collection avoids some serious problems, such as dangling pointers, it
can cause other problems. One such difficulty arises because the garbage col-
lector deallocates, or reclaims the storage occupied by an object, but it does no
more. For example, if an object has access to some resource other than heap
memory, such as a file or a lock on a shared resource, the garbage collector does
not reclaim these. For these situations, Java allows the inclusion of a special
method, finalize, which is related to a C++ destructor function.
A finalize method is implicitly called when the garbage collector is about
to reclaim the storage occupied by the object. The problem with finalize is
that the time it will run cannot be forced or even predicted. The alternative to
using finalize to reclaim resources held by an object about to be garbage
collected is to include a method that does the reclamation. The only problem
with this is that all clients of the objects must be aware of this method and
remember to call it.

12.7.2 Inheritance


In Java, a method can be defined to be final, which means that it cannot be
overridden in any descendant class. When the final reserved word is specified
on a class definition, it means the class cannot be subclassed. It also means that
the bindings of method calls to the methods of the subclass are statically bound.
Java includes the annotation @Override, which informs the compiler to
check to determine whether the following method overrides a method in an
ancestor class. If it does not, the compiler issues an error message.
Like C++, Java requires that parent class constructor be called before the
subclass constructor is called. If parameters are to be passed to the parent
class constructor, that constructor must be explicitly called, as in the following
example:

super(100, true);

If there is no explicit call to the parent-class constructor, the compiler inserts
a call to the zero-parameter constructor in the parent class.
Java does not support the private and protected derivations of C++. One
can surmise that the Java designers believed that subclasses should be subtypes,
which they are not when private and protected derivations are supported. Thus,
they did not include them. Early versions of Java included a collection, Vector,
which included a long list of methods for manipulating data in a collection con-
struct. These versions of Java also included a subclass of Vector, Stack, which
added methods for push and pop operations. Unfortunately, because Java does
not have private derivation, all of the methods of Vector were also visible in
the Stack class, which made Stack objects liable to a variety of operations that
could invalidate those objects.
Java directly supports only single inheritance. However, it includes a kind
of abstract class, called an interface, which provides partial support for multiple
Free download pdf