A DeptDirectory is either:
A directory Entry is a triple (name, office, phone) of strings.
In Scheme, we could implement this definition using the following collection of structures:
Note that the preceding Scheme code leaves most of the corresponding data defintion unstated! It never mentions the new type DeptDirectory. It does not express the fact that instances of Empty and Cons are elements of the type DeptDirectory, nor does it state that the first and rest fields of a Cons must be of type Entry and Dept Directory, respectively. Similarly, it fails to state that the fields name, office and phone are all strings.(def-struct (Empty)) (def-struct (Cons first rest)) (def-struct (Entry name office phone))
In Java we can completely represent the data definition using the following collection of classes:
abstract class DeptDirectory {}
class Empty extends DeptDirectory {}
class Cons extends DeptDirectory {
Entry first;
DeptDirectory rest;
// the following constructor is used to initialize Cons objects
Cons(Entry f, DeptDirectory r) {
first = f;
rest = r;
}
}
class Entry {
String name, office, phone;
// the following constructor is used to initialize Entry objects
Entry(String n, String o, String p) {
name = n;
office = o;
phone = p;
}
}
The Java code is wordier, but it captures all of the information
in the data definition.
The Cons class contains two fields
first and rest akin to the two fields in the
corresponding Scheme struct. Similarly, the Entry
class contains three fields name, office, and
phone just like the Scheme struct
Entry given above. The Java code involves several concepts
that we have not discussed before.
First, the class DeptDirectory is abstract because it is used simply to group the classes Empty and Cons into a larger class that encompasses both kinds of data. An abstract class cannot be instantiated. All objects in the type DeptDirectory must be instances of its concrete (not abstract) subclasses. There are no instances of class DeptDirectory.
Second, the classes Empty and Cons extend the class DeptDirectory defining Empty and Cons as subclasses (subsets) of the class DeptDirectory. In Java, every class C determines a type (a set of objects) consisting of all instances of the class C and all subclasses of C. Hence, all instances of the classes Empty and Cons belong to the type DeptDirectory.
Third, the class name preceding each variable name specifies the permissable type of the corresponding variable; only values of the specified type can be assigned to the variable. Similarly, the class name preceding each method name specifies the result type of the corresponding method; only values of the specified type can be returned as results of the method.
Fourth, the constructors for the classes Cons and Entry specify how new instances of those classes are initialized. The expression
new Entry("Corky","DH3104","x6042")
creates a new Entry object and initializes its fields
by executing the body of the Entry constructor given
the binding of the constructor parameters
n, o, p,
to the arguments "Corky","DH3104","x6042", respectively.
Fifth, if a class does not include a constructor, Java creates a default constructor of no arguments that initializes each field of the object to the ``zero'' value of its declared type. The ``zero'' value for any class type is the null reference. The Emtpy class above relies on the fact that Java provides a default ``zero-ary'' constructor.