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

Inheritance in Class Hierarchies

Classes are bound into hierarchies by inheritance. If you understand how G2 handles inheritance, you will be able to design class hierarchies to suit any need.

Single inheritance is very straightforward: a subclass inherits everything characteristic of its direct superior class (parent). If you do not redefine any system-defined attributes in the subclass definition, or add any class-specific attributes to the definition, an instance of the subclass differs only in name from an instance of its parent class.

Single inheritance is convenient where information is tree-structured, or sufficiently close to it that an acceptable amount of duplication in class definitions can make up the difference. Single inheritance can usually provide everything that a user-defined class hierarchy needs.

Where information is not tree-structured, you can use multiple inheritance to represent the information efficiently. Multiple inheritance differs from single inheritance in that it allows a subclass to have more than one direct superior class.

Various complexities can arise in multiple inheritance, because the parent classes can include duplicate and conflicting attributes. If you understand how G2 copes with these, as described later in this chapter, you should have no difficulty using multiple inheritance.

Every definition has two attributes for controlling and displaying inheritance: Direct-superior-classes and Class-inheritance-path. You must understand these attributes in order to understand G2 inheritance.

Direct-superior-classes Attribute

This attribute of a class lists the class's parent(s). In single inheritance, only one class appears: the direct superior class. In multiple inheritance, more than one class appears. The first class named in the attribute is called the primary direct superior, and all subsequent classes are secondary direct superiors.

When more than one direct superior class exists, the order in which they appear in the Direct-superior-classes attribute, in conjunction with the structure of the existing class hierarchy, determines the class inheritance path, which controls what happens in case of duplicate or conflicting inheritance.

Class-inheritance-path Attribute

This attribute of a class lists all classes that contribute to the class's inheritance, in precedence order. The list begins with the class itself: the immediate class. The list ends with the root of the class hierarchy, which is always ITEM. The contents of the list between the immediate class and the root class vary with the details of the immediate class's inheritance.

G2 automatically derives a class's inheritance path based on its direct superior class(es) and the existing class hierarchy, and updates the path as needed when you modify the hierarchy.

Using the Class Inheritance Path

Class inheritance paths are the key to working with class hierarchies. Almost everything discussed in this chapter either explains how G2 generates class hierarchy paths, or shows how G2 uses such paths to determine class properties.

G2 uses a class's inheritance path to resolve duplicate and conflicting attributes among the superior classes that the class inherits. Such resolution is exactly the same in single and multiple inheritance: G2 scans the class inheritance path and gives precedence to the first relevant attribute definition that it encounters.

Since the immediate class is always the first class on its own inheritance path, this technique allows a class to override, or shadow, any inherited attribute definition with a definition of its own.

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

Copyright © 1997 Gensym Corporation, Inc.