Next: Operational Rules
Up: Operational Semantics
Previous: Syntax for Cool Objects
Contents
Class definitions
In the rules presented in the next section, we need a way to refer to
the definitions of attributes and methods for classes.
Suppose we have the following Cool class
definition:
class B {
s : String <- "Hello";
g (y:String) : Int {
y.concat(s)
};
f (x:Int) : Int {
x+1
};
};
class A inherits B {
a : Int;
b : B <- new B;
f(x:Int) : Int {
x+a
};
};
Two mappings, called class and implementation, are associated
with class definitions. The class mapping is
used to get the attributes, as well as their types and initializations, of a
particular class:
Note that the information for class contains everything that it
inherited from class , as well as its own definitions. If had
inherited other attributes, those attributes would also appear in the information
for . The attributes are listed in the order they are inherited
and then in source order:
all the attributes from the greatest ancestor are listed first in the
order in which they textually appear, then the attributes of the next greatest
ancestor, and so on, on down to the attributes defined in the particular class.
We rely on this order in describing how new objects are initialized.
The general form of a class mapping is:
Note that every attribute has an initializing expression, even if the
Cool program does not specify one for each attribute. The default
initialization for a variable or attribute is the default of
its type. The default of Int is 0, the default of String
is
, the default of Bool is false, and the default
of any other type is void.5The default of type is
written .
The implementation mapping gives information about the methods of
a class. For the above example, implementation of A is defined as
follows:
In general, for a class and a method ,
specifies that method when invoked from class , has formal
parameters
, and the body of the method is expression
.
Next: Operational Rules
Up: Operational Semantics
Previous: Syntax for Cool Objects
Contents