Skip to content

Multiple Inheritance

Each class that you declare automatically inherits all of the components of its superclass. DataFlex also supports the ability to import the methods of other classes that are not part of the superclass.

A class declaration imports the methods of a class (other than its superclass) by executing an Import Protocol statement in its class definition. The syntax of the import protocol statement is shown below:

Import_Class_Protocol {imported-class}

Where:

  • {imported-class} is the name of the class whose attributes are being imported. For more information on Import Protocol statements, refer to the Import_Class_Protocol command in the DataFlex Language Reference.

Example of Multiple Inheritance

An example of a class definition with multiple inheritance is shown below:

Class cMessage_Mixin is a Mixin
    Procedure Define_cMessage_Mixin
        Property String psMessage ""
    End_Procedure

    Procedure DoShowMessage
        String sMessage
        Get psMessage To sMessage
        Send Info_Box sMessage
    End_Procedure
End_Class

Class cMessageButton is a Button
    Import_Class_Protocol cMessage_Mixin

    Procedure Construct_Object
        Forward Send Construct_Object
        Send Define_cMessage_Mixin   // important!!!
    End_Procedure

    Procedure OnClick
        Send DoShowMessage
    End_Procedure
End_Class

The above example declares two new classes. The first one, cMessage_Mixin, is designed specifically to be mixed into the class definition of some other class using the multiple-inheritance model. Classes designed for this purpose are called Mixin classes (they are based on the DataFlex Mixin class). You would never instantiate objects of any Mixin class; they are designed only as building blocks for other classes.

The second class in the example, cMessageButton, is based on the button class but it also imports all methods from the cMessage_Mixin class. Notice that the constructor method of this class calls the inherited Define_cMessage_Mixin method. This is important because it demonstrates how properties can be defined in a Mixin and then imported. Properties defined in the constructor of a Mixin class would not get imported because the Mixin's constructor is never called.

The way that DataFlex works is to only import the methods and properties that are directly defined in the class that is being imported. Thus, the imported class's own inherited methods are not imported, nor are its property definitions or private objects. Strictly speaking, this is not multiple inheritance. Instead, it can be thought of as importing the skills of another class.

Importing class skills (or methods) is less complicated and more useful than full multiple inheritance because it eliminates the possibility of an unwanted clash of method definitions. Such a clash would otherwise occur when the superclass of the imported class contains a method or property that is also defined in the superclass of the new class that you are creating.