next up previous contents
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:

\begin{displaymath}
class(A) = (s : String \leftarrow \mbox {\tt ''}Hello \mbox {\tt ''},\ a : Int
\leftarrow 0,\ b : B \leftarrow new\ B)
\end{displaymath}

Note that the information for class $A$ contains everything that it inherited from class $B$, as well as its own definitions. If $B$ had inherited other attributes, those attributes would also appear in the information for $A$. 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:

\begin{displaymath}
class(X) = (a_1:T_1\leftarrow e_1,\ \ldots,\ a_n:T_n\leftarrow e_n)
\end{displaymath}

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 $\mbox {\tt ''}\mbox {\tt ''}$, the default of Bool is false, and the default of any other type is void.5The default of type $ T$ is written $D_T$.

The implementation mapping gives information about the methods of a class. For the above example, implementation of A is defined as follows:


\begin{displaymath}
\begin{array}{rcl}
implementation(A,f) & = & (x, x+a) \\
implementation(A,g) & = & (y, y.concat(s))
\end{array}\end{displaymath}

In general, for a class $X$ and a method $m$,

\begin{displaymath}
\mbox{\em implementation}(X,m) = (x_1, x_2, \ldots, x_n, e_{body})
\end{displaymath}

specifies that method $m$ when invoked from class $X$, has formal parameters $x_1, \ldots, x_n$, and the body of the method is expression $e_{body}$.


next up previous contents
Next: Operational Rules Up: Operational Semantics Previous: Syntax for Cool Objects   Contents