[Contents] [Previous] [Next]

Class hierarchies and containment hierarchies

Many products that support OOP provide class libraries that you can use and reuse by instantiation and subclassing as described in the previous sections. Inheritance and polymorphism are important benefits of these libraries, which allow you to create and use families of related objects that share the same interface even though their contents and behavior differ. A class hierarchy describes the patterns of inheritance within a family of related objects. These patterns are also called "is a" relationships; for example, the TScrollingWindow discussed earlier "is a" TWindow. Each class defines the objects that belong to that class in terms of inherited characteristics that are either used as is or replaced (overridden), plus new characteristics.

After you've instantiated an object, its inherited behavior is determined by its place in the class hierarchy, but it may also be part of a containment hierarchy: that is, it can contain and be contained by other objects. Containment relationships are also called "has a" relationships. For example, a window that contains a text object "has a" text object. The text object might in turn contain graphics or multimedia objects, and the window that contains it is in turn contained within a particular application.

Grady Booch (1994) describes this combination of class and containment hierarchies as "the canonical form of a complex system." It turns out that an amazing variety of artificial and natural structures, from cells to spacecraft, can be modeled by using similar complementary hierarchies. Virtually anything in the real world can be described both as "part of" something else and as one instance of a category of related things.

For example, an individual cardiac cell is part of a heart, which is part of a circulatory system, which is part of a body made up of many complex interdependent systems. At the same time, a cardiac cell is one of three basic kinds of muscle cells--cardiac, smooth, and striated--and it has some properties in common with those kinds of cells and some properties that are different. Similarly, a valve is part of a car engine, which is part of a car, and the valve
itself is made up of still smaller parts. A valve can also be described as a particular kind of valve that resembles other valves in some ways and differs from them in other ways.

This natural correspondence between the hierarchical yet flexible relationships among classes and the relationships we perceive in the real world give OOP its unique ability to model or simulate complex systems.


[Contents] [Previous] [Next]

Addison-Wesley Publishing Company, Copyright©1995 by Sean Cotter and Taligent,Inc. All rights reserved.