Common Lisp the Language, 2nd Edition


next up previous contents index
Next: Inheritance of Class Up: Inheritance Previous: Inheritance of Methods

28.1.3.2. Inheritance of Slots and Slot Options

change_begin
The set of names of all slots accessible in an instance of a class C is the union of the sets of names of slots defined by C and its superclasses. The structure of an instance is the set of names of local slots in that instance.

In the simplest case, only one class among C and its superclasses defines a slot with a given slot name. If a slot is defined by a superclass of C, the slot is said to be inherited. The characteristics of the slot are determined by the slot specifier of the defining class. Consider the defining class for a slot S. If the value of the :allocation slot option is :instance, then S is a local slot and each instance of C has its own slot named S that stores its own value. If the value of the :allocation slot option is :class, then S is a shared slot, the class that defined S stores the value, and all instances of C can access that single slot. If the :allocation slot option is omitted, :instance is used.

In general, more than one class among C and its superclasses can define a slot with a given name. In such cases, only one slot with the given name is accessible in an instance of C, and the characteristics of that slot are a combination of the several slot specifiers, computed as follows:

A consequence of the allocation rule is that a shared slot can be shadowed. For example, if a class defines a slot named S whose value for the :allocation slot option is :class, that slot is accessible in instances of and all of its subclasses. However, if is a subclass of and also defines a slot named S, 's slot is not shared by instances of and its subclasses. When a class defines a shared slot, any subclass of will share this single slot unless the defclass form for specifies a slot of the same name or there is a superclass of that precedes in the class precedence list of that defines a slot of the same name.

A consequence of the type rule is that the value of a slot satisfies the type constraint of each slot specifier that contributes to that slot. Because the result of attempting to store in a slot a value that does not satisfy the type constraint for the slot is undefined, the value in a slot might fail to satisfy its type constraint.

The :reader, :writer, and :accessor slot options create methods rather than define the characteristics of a slot. Reader and writer methods are inherited in the sense described in section 28.1.3.1.

Methods that access slots use only the name of the slot and the type of the slot's value. Suppose a superclass provides a method that expects to access a shared slot of a given name, and a subclass defines a local slot with the same name. If the method provided by the superclass is used on an instance of the subclass, the method accesses the local slot.
change_end



next up previous contents index
Next: Inheritance of Class Up: Inheritance Previous: Inheritance of Methods


AI.Repository@cs.cmu.edu