| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (8 out of 18)

Single Inheritance

A single inheritance class is a class that has a unique inheritance path leading from itself to the root class, because neither the class nor any of its ancestors has more than one parent.

For a single inheritance class, there is only one reasonable way to order the class inheritance path: it lists the class itself, its parent, it's parent's parent, and so on, culminating in the root class.

For example, consider first the following single inheritance class hierarchy:


The figure shows several classes in a hierarchy based on EQUIPMENT. Arrows point from the superior classes to the subclasses. The structure of the hierarchy is:

Following the rule for single inheritance given above:

The classes ITEM and OBJECT will be omitted from class inheritance paths from this point on, because they would be exactly the same in every one.

Inheritance of Default Values

When you create an instance of a class, some system-defined attributes, and any user-defined attribute, can receive an initial value. G2 calls such a value the default value of the attribute. This value is derived from the definition of the instantiated class.

When you reset a KB, attribute values do not return to their default values: they keep the values that they had prior to the reset. This behavior distinguishes default values of attributes from initial values of variables and parameters, which are restored each time a KB is reset.


Caution: Be careful not to confuse a default value, as described in this section, with a default attribute, as described in the next section.

For every attribute, a class can inherit a default value from a superior class, or it can specify a default value of its own, overriding any inherited value. When a class does not explicitly define a default value, a new instance of the class has the value specified by the first class in the class's inheritance path that defines a default value for the attribute.

For example, consider the following hierarchy:


The figure shows the same classes that we looked at earlier. Two of the classes, EQUIPMENT and PERIPHERAL, explicitly define icons, A picture of each icon appears by the class that defines it. The icon by EQUIPMENT is called the "workstation icon." The icon by PERIPHERAL is called the "node icon."

An object's icon is specified by the default value of the Icon-description attribute of its class description. This is a system-defined attribute that has a default value provided by G2. A user-defined class can override this default by specifying its own value for Icon-description, as EQUIPMENT and PERIPHERAL both do.

With the default Icon-description values shown, an instance of EQUIPMENT has a workstation icon, because the class defines that default value for the attribute. An instance of COMPUTER has the same icon, because COMPUTER inherits the default value from EQUIPMENT. PC similarly inherits that default value from COMPUTER, so a PC instance also has the workstation icon.

However, PERIPHERAL explicitly specifies a different default Icon-description value: the node icon. This definition shadows EQUIPMENT's default icon description, and is inherited by NETWORK. Thus instances of both PERIPHERAL and NETWORK have node icons, not workstation icons.

Inheriting Default Values for Stubs

Icons and stubs are closely related, but they are specified by two different attributes of a definition. This independence could result in mismatched icons and stubs, so the G2 class inheritance rules contain a special provision that prevents it.

To prevent such a mismatch, a class can inherit a stubs definition only from the class from which it inherits its icon definition, or from a descendent of that class: classes superior to the class that supplies the icon definition are disallowed. If no acceptable superior class provides a stubs definition, the class's stubs default value is none.

Inheritance of Methods

A given class can have any number of associated methods that define the behavior of instances of the class. These methods can take arguments that tailor the behavior to suit the particular instance. Methods defined for different classes can have the same name.

When you call a method on an instance of a class, G2 searches the Class-inheritance-path of that class and uses the first explicitly defined method it encounters that has the same number of arguments as the calling expression. If no match is found with any class in the class inheritance path, G2 signals an error.

For more information about methods and their use of the class inheritance path, see Chapter 22, Methods.

Duplicate Attributes

To facilitate modular design and encapsulation, G2 allows a subclass to define an attribute whose name duplicates that of an inherited user-defined attribute. Instances of the subclass then have both the inherited attribute and the locally defined attribute. G2 does not permit a user-defined attribute to duplicate the name of a system-defined attribute.

Attributes with the same name are customarily called duplicate attributes, though actually only their names are duplicates. Duplicate attributes have no more relation to one another than any two attributes have. They may or may not hold data of the same type, or share any other characteristics.

If you change a duplicate attribute's definition to eliminate the duplication, nothing changes but the name; the attributes themselves are unaffected. The same applies if you change a name in a way that creates a duplication. In either case, G2 updates all instances and tables as needed to reflect the change.

Duplicate attributes can lead to confusion, and complicate the task of referring to attributes in expressions. Use them sparingly if at all, and only where they provide a significant advantage.

Naming Duplicate Attributes

To prevent ambiguity, G2 qualifies the names of duplicate attributes as needed with the name of the class that defines each attribute. The syntax for a qualified attribute name is:

For every class, G2 determines what duplicate attribute name(s) to qualify by following the class's inheritance path. G2 uses without qualification the first attribute with a given name that it encounters. This attribute is called the default attribute. G2 qualifies each subsequently defined duplicate attribute with the name of the class that defines it.


Caution: Be careful not to confuse a default attribute, as described in this section, with a default value, as described in the previous section.

For example, consider the following hierarchy:


Suppose that both EQUIPMENT and COMPUTER explicitly define an attribute named Application. The table of a COMPUTER instance would show two attributes: Application, representing the locally defined attribute, and Equipment::Application, representing the inherited attribute.

G2 lists attributes from the bottom up in the order in which their definitions appear on the class's inheritance path, so the table looks like this:


An instance of PC has the same Application attributes as an instance of COMPUTER, because the PC class inherits them from the COMPUTER class:


In the preceding two tables, the default Application attribute is the same for both COMPUTER and PC: it is PC's attribute. The reason is that, for both COMPUTER and PC, COMPUTER is the first class on the class's inheritance path to define an Application attribute.

However, if PC also explicitly defined an Application attribute, an instance of PC would have three such attributes: Application, Computer::Application, and Equipment::Application:


The attribute now named Computer::Application is the same attribute that was named Application before PC defined its own Application attribute. It is no longer PC's default Application attribute, because PC's own Application definition is now the first such definition by a class on PC's inheritance path.

Referencing Duplicate Attributes

When you reference one of a set of duplicate attributes, you must qualify the reference as needed to specify the correct attribute. The name for any attribute of an item appears with the necessary qualification in the item's table.

If you do not qualify the name in a reference to a duplicate attribute, G2 uses the default attribute of the class of the item. For example, if pc-1 is an instance of PC in the EQUIPMENT hierarchy pictured above, then:

refers to the Application attribute that PC inherits from COMPUTER, and:

refers to the Application attribute that PC inherits from EQUIPMENT.

The meaning of an unqualified name can change if the hierarchy changes. For example, if PC were to define its own Application attribute, then:

would thereafter refer to Application attribute that PC itself defines. To reference computer's Application attribute, you would need a qualified name:

To guard against such changes in meaning, you can qualify a reference to a default attribute for a class. Such qualification, though initially redundant, ensures that your code will mean the same thing even if subsequent changes give the class a different default attribute. For example:

means the same thing whether or not PC defines an Application attribute of its own.

When you give a qualified name in a reference, G2 always does the same thing: it uses the default attribute of the class named in the reference. Thus a qualified reference need not name the class that actually defines the attribute.

For example, in the hierarchy shown previously on the MULT-DEFS-WS workspace, the names Network::Application, Peripheral::Application, and Equipment::Application all refer to EQUIPMENT's Application attribute, because that is the default Application attribute for all three classes.

Duplicate Attributes and Default Values

When duplicate attributes exist, the default values for each attribute are completely independent of one another. They have no more relationship than they would if each attribute had a different name.

Be careful not to define a duplicate attribute when all you want to do is shadow an inherited attribute's default value. You can define a new default value for any attribute in any class definition, as described under Specifying Default Values for Inherited Attributes.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (8 out of 18)

Copyright © 1997 Gensym Corporation, Inc.