Next: Initial Values
Up: Creating Classes
Previous: Initialization Variables
Each instance-variable-clause declares a number of instance
variables for instances of the class, or expressions to be evaluated
when an instance of the class is created. The first part of a clause
is the clause specifier, one of sequence, public,
private, etc. A clause specifier determines the properties of
instance variables declared in its clause:
- sequence -- This clause form does not declare any
instance variables. It only specifies expressions to be evaluated (in
order) when an instance of the class is initialized.
- public -- These instance variables are
fully visible. A derived class can override public declarations,
and public declarations override existing declarations in the
superclass.
- private -- These instance variables
can be accessed only within the class definition. They cannot be
overridden in a derived class and do not override declarations in the
superclass.
- inherit -- Declares instance
variables that must be defined in the superclass or one of its
ancestors (as public). An inherit declaration can be
overridden in derived classes.
- rename -- Similar to inherit,
but a new name is always used to access the value locally and the
reference cannot be overridden. The new name can only be used within
the class definition. A rename declaration accesses the
superclass-defined variable value, even if the variable is overridden.
Each clause specifier can be used for any number of clauses in any
order within a single class*/names expression.
Figure 6.1 summarizes the variable-declaring specifiers
according to two basic properties:
- Visibility -- Each instance variable is either
exposed or hidden. An exposed variable can be
accessed from derived classes and from outside the object, and can be
overridden in a derived class. A hidden variable can only be used
within the class definition, and it cannot be overridden.
- Generation -- Each instance variable declaration is
either a new variable definition (potentially overriding a
definition in a superclass) or a reference to an existing
variable in a superclass.
The collection of instance variable declarations induces two sets of
variables:
- The internal instance variables bound within
the class* expression: this is the collection of all
instance-variables, internal-instance-variables,
and inherited-variables.
Along with the variables for initialization variables,
this-variable, and super-init-variable, all of these
variables must be distinct.
- The external instance variables visible outside
the class* expression (for derived classes or reference via
ivar): this is the collection of all instance-variables and
external-instance-variables. All of these variables must be distinct.
The same identifier can be used as an internal variable and an
external variable, and it is possible to use the same identifier as
internal and external variables for different bindings (as long as
all internal variables are distinct and all external variables are
distinct).
For public and private instance variables, the
instance-var-initial-value-expr expression provides a value
for the variable in an object; when an initial value expression is not
given, (void) is used. The process for evaluating initial value
expressions is decribed in section 6.3.3 and section 6.4.
For inherit and rename instance variables, the
inherited-variable or external-inherited-variable specifies
a (public) instance variable from the superclass. This inheritance is
verified when the class*/name expression is evaluated: if an
inherited instance variable is not found in the superclass (or one of
its ancestors), the exn:object:inherit exception is raised. The process that
gives values to inherited variables is described in
section 6.4.
Figure 6.1: Instance variable specifier summary
Next: Initial Values
Up: Creating Classes
Previous: Initialization Variables
PLT