gtpd1m2aDatabase Reference

Object-Oriented Concepts

TPFCS has been implemented using an object-oriented design. While application programmers need no knowledge of object-oriented concepts to use the services TPFCS provides, a knowledge of general object-oriented concepts described in this document is required to understand how collections are stored in a TPF database.

An object is an entity consisting of data and functions that are available to manage the data. The terms attributes and methods are often used to refer to the data fields and functions, respectively. Objects are classified into different categories. Each category of objects is known as an object class, or simply as a class. An object class is merely a description that defines the exact format of the attributes (data) as well as the exact methods (functions) to be applied to the data. The attributes and methods together comprise each object of that class. Each specific, existing example of an object is said to be an instance of the object class. In discussing object-oriented design, the terms object and instance are often used interchangeably. Both refer to an actual example of a generic type that is described by a class. Each object or instance contains all the attributes and methods defined for its class. We commonly say that each instance contains those attributes and methods from its class.

Some object classes can be categorized and divided into more specific classes, just as a generic category can be divided into more specific types. The more generic class is commonly known as the superclass of the more specific classes. Likewise, each of the more specific classes is commonly known as a subclass of the more generic class. A given subclass, in turn, can be divided into additional, more specific subclasses. Each subclass definition is built on the definition of its superclasses, and can also specify a set of additional attributes and methods unique to the subclass. An object, which is an instance of a subclass, not only contains or inherits the attributes and methods defined for the subclass, but also inherits the attributes and methods defined for each of the more generic classes (superclasses) as well.

You can best understand how different objects inherit attributes from different classes by drawing an inheritance tree, which is a diagram that shows how different classes and their objects are related. Consider the following example of an inheritance tree:

Figure 11. Class Inheritance Tree Example


In our example, ClassA is divided into two subclasses, ClassB1 and ClassB2. ClassB1 contains only one subclass (ClassC1), whereas ClassB2 is divided into two additional subclasses called ClassD1 and ClassD2 respectively. In our example, only the leaves of an inheritance tree represent classes for which specific objects or instances can exist. ClassA, ClassB1, and ClassB2 can be referred to as abstract classes because there are no objects belonging to those classes that do not also belong to an additional subclass.

All objects of ClassC1 not only inherit all of the attributes and methods defined for ClassC1, but also inherit the attributes and methods defined for ClassB1 and ClassA. Similarly, all ClassD1 objects inherit the attributes and methods defined for ClassD1, ClassB2, and ClassA; and all ClassD2 objects inherit the attributes and methods defined for ClassD2, ClassB2, and ClassA. Notice that, while all of the objects inherit the attributes and methods defined for ClassA, none of the objects inherit from all of the classes. For example, a ClassD1 object does not inherit attributes and methods from ClassC1, ClassB1, or ClassD2.

It is also important to note that, in our example as in many other object-oriented designs, a specific object can be an instance of more than one class. For example, based on the way we have shown the class inheritance, any ClassD1 object is not only an instance of ClassD1, but is also an instance of ClassB2 as well as an instance of ClassA. In simpler terms, we can say that a ClassD1 object is also a ClassB2 object and is also a ClassA object.

You can best understand how class inheritance works by considering a concrete, but hypothetical example. Suppose that you were designing an object-oriented database to store information about vehicles. An object-oriented design of such a database might be shown as follows:

Figure 12. Object Class Inheritance


In our example, every object belonging to the Vehicle class also belongs to the OBJECT class. 1 The Vehicle class is divided into two more specific subclasses: the NonMotorVehicle and the MotorVehicle classes. Notice that the NonMotorVehicle class only contains one subclass: the Bicycle class. On the other hand, the MotorVehicle class contains two subclasses: the Truck class and the Automobile class. The Automobile class, in turn, contains several subclasses of its own; the only two shown are the SportsCar and the Limousine classes.

Assume that all of the classes shown are abstract except for the Bicycle class, the Truck class, and the subclasses of Automobile. This means that there is no OBJECT in our database that is not something more specific; for example, an OBJECT can be a Vehicle. Likewise, there are no Vehicle objects in our database that are not additionally classified as a specific type of Vehicle, and so on. Furthermore, there is no Automobile object that is not a specific type of Automobile: for example, a specific instance of the Automobile class must be either a SportsCar, a Limousine, or an instance of one of the other Automobile subclasses not shown. It is also important to note that, in our example, every object inherits from the generic OBJECT class. The OBJECT class is not only abstract, but it can be referred to as the base class of our database because all instances inherit from it.

Suppose we have stored objects in our database that represent specific vehicles, including Frank's sports car and Jackie's limousine. Frank's sports car is an instance of the SportsCar class. It is a SportsCar. We can also say that Frank's sports car is an Automobile, that it is a MotorVehicle, that it is a Vehicle, and that it is an OBJECT. After all, any sports car can be classified as an automobile, as well as a motor-powered vehicle, a generic vehicle, or even as a physical object. Similarly, Jackie's limousine is a Limousine, an Automobile, and so on.

The type of inheritance that has been described is also known as single inheritance. Single inheritance is a design characteristic of an object-oriented database in which each class inherits directly from only one immediate superclass. For example, even though SportsCar inherits from Automobile, which inherits from MotorVehicle and so on, SportsCar only inherits directly from one immediate superclass: Automobile. This is true for all the other classes in our vehicle database because it is designed using single inheritance.

You can best understand single inheritance by considering its alternative, which is multiple inheritance. Multiple inheritance is a design characteristic of an object-oriented database in which a given class can inherit directly from more than one immediate superclass. Our original vehicle database was not designed using multiple inheritance because no class inherited directly from more than one immediate superclass. Another vehicle database could implement multiple inheritance in the following way:

Figure 13. Multiple Inheritance Example


This is an example of multiple inheritance because Motorcycle inherits directly from both TwoWheeler and MotorVehicle. Note that multiple inheritance and single inheritance are mutually exclusive.


Footnotes:

1
Assume that the database for this example may contain other subclasses of OBJECT in addition to Vehicle, as well as other subclasses for Automobile in addition to SportsCar and Limousine. These subclasses are not shown in Figure 12 to simplify our discussion.