Skip to content

Import_Class_Protocol

See Also: Multiple-Inheritance, Class, Forward

Purpose

To import the definition(s) of a message or messages from a class that is not the superclass of the class being defined.

Syntax

Import_Class_Protocol {source-class} [{dest-class} [{message} | All]] ;
[Inherit] [No_Overwrite]

What It Does

This command identifies a class not in the ancestry of the receiving class, from which to import the definition(s) of either one identified message or all the messages defined in the source class.

Example

The following example shows how a subclass can derive some messages from its superclass and then mix in some messages from an imported class:

Class cSuperClass is a cObject
    Procedure Proc_1
        Showln "Inside Proc 1 of cSuperClass"
    End_Procedure

    Procedure Proc_2
        Showln "Inside Proc 2 of cSuperClass"
    End_Procedure

    Procedure Proc_3
        Showln "Inside Proc 3 of cSuperClass"
    End_Procedure
End_Class

Class cMixin is a Mixin
    Procedure Proc_1
        Showln "Inside Proc 1 of cMixin"
    End_Procedure

    Procedure Proc_4
        Showln "Inside Proc 4 of cMixin"
    End_Procedure
End_Class

Class cSubClass is a cSuperClass
    Import_Class_Protocol cMixin

    Procedure Proc_2  // Forwarding example.
        Showln "Before forwarding. Inside Proc 2 of cSubClass"
        Forward Send Proc_2
        Showln "After forwarding. Inside Proc 2 of cSubClass"
    End_Procedure
End_Class

Object oTest is a cSubClass
End_Object

Showln "This program demonstrates import class protocol"
Send Proc_1 to oTest   // executes in cMixin
Send Proc_2 to oTest   // executes in cSubClass, forwards to cSuperClass
Send Proc_3 to oTest   // executes in cSuperClass
Send Proc_4 to oTest   // executes in cMixin

Class cSubClass defined above will execute the code for Proc_1 of cMixin rather than the code in cSuperClass, because cSubClass imports the implementation from cMixin. The code defined in cSubClass for Proc_2 will forward to the definition in cSuperClass, because no definition for Proc_2 exists in cMixin. The implementation of Proc_3 in cSuperClass is the only implementation for Proc_3, so it will be inherited by cSubClass. The same is true for Proc_4 in cMixin – it will also be inherited by cSubClass.

You can use more than one Import_Class_Protocol in a class definition. When the same message is implemented in more than one imported class, the last imported definition is the one used. The following class inherits from one superclass, cSuperClass, and then imports from two other classes: cMixin1 and cMixin2. The difference here is that the code for Proc_4 in cMixin2 will execute instead of the implementation in cMixin1, because cMixin2 is imported after cMixin1.

Class cSuperClass is a cObject
    Procedure Proc_1
        Showln "Inside Proc 1 of cSuperClass"
    End_Procedure

    Procedure Proc_2
        Showln "Inside Proc 2 of cSuperClass"
    End_Procedure

    Procedure Proc_3
        Showln "Inside Proc 3 of cSuperClass"
    End_Procedure
End_Class

Class cMixin1 is a Mixin
    Procedure Proc_1
        Showln "Inside Proc 1 of cMixin1"
    End_Procedure

    Procedure Proc_4
        Showln "Inside Proc 4 of cMixin1"
    End_Procedure
End_Class

Class cMixin2 is a Mixin
    Procedure Proc_4
        Showln "Inside Proc 4 of cMixin2"
    End_Procedure
End_Class

Class cSubClass is a cSuperClass
    Import_Class_Protocol cMixin1
    Import_Class_Protocol cMixin2

    Procedure Proc_2  // Forwarding example.
        Showln "Before forwarding. Inside Proc 2 of cSubClass"
        Forward Send Proc_2
        Showln "After forwarding. Inside Proc 2 of cSubClass"
    End_Procedure
End_Class

Object oTest is a cSubClass
End_Object

Showln "This program demonstrates import class protocol"
Send Proc_1 to oTest   // Executes in cMixin1
Send Proc_2 to oTest   // Executes in cSubClass, fwds to cSuperClass
Send Proc_3 to oTest   // Executes in cSuperClass
Send Proc_4 to oTest   // Executes in cMixin2

Base class behavior cannot be imported. Use inheritance for your primary class ancestry and then use the Import_Class_Protocol command to add new behaviors. It only makes sense to use Import_Class_Protocol when the methods the imported class defines are not specific to a single class. When there is no opportunity for reuse of the code, just implement the new methods in your subclass.

You may specify an Import_Class_Protocol command outside of a class definition, but then you must provide the target class name that receives the imported procedures and functions. For example, instead of specifying the Import_Class_Protocol commands in the previous example, you can omit them and add the following two lines after the definition of cSubClass:

Import_Class_Protocol cMixin1 cSubClass
Import_Class_Protocol cMixin2 cSubClass

The next example shows how a single message can be imported. The next example declares a mixin for a class, cMixin, that implements two messages; but only one of the messages (Proc_2) is imported:

Class cSuperClass is a cObject
    Procedure Proc_1
        Showln "Inside Proc 1 of cSuperClass"
    End_Procedure

    Procedure Proc_2
        Showln "Inside Proc 2 of cSuperClass"
    End_Procedure
End_Class

Class cMixin is a Mixin
    Procedure Proc_1
        Showln "Inside Proc 1 of cMixin"
    End_Procedure

    Procedure Proc_2
        Showln "Inside Proc 2 of cMixin"
    End_Procedure
End_Class

Class cSubClass is a cSuperClass
    Import_Class_Protocol cMixin cSubClass Proc_2
End_Class

Object oTest is a cSubClass
End_Object

Showln "This program demonstrates import class protocol"
Send Proc_1 to oTest   // cSuperClass code executes.
Send Proc_2 to oTest   // cMixin code executes.

Note: You must also specify the target class name when you use the option to limit the Import_Class_Protocol to one message. The target class name also must be specified when you use the Inherit or No_Overwrite options. When the Inherit or No_Overwrite options import all the messages rather than just one message, use the keyword All, immediately after the name of the target class, Dest, of the Import_Class_Protocol command.

The Inherit keyword will cause the Import_Class_Protocol to import not only the implementations of messages in the mixin class but also all its ancestors. The following example shows how the Inherit option can be used to import a class and its ancestors:

Class cMixinSuperClass is a Mixin
    Procedure Proc_1
        Showln "Inside Proc 1 of cMixinSuperClass"
    End_Procedure

    Procedure Proc_2
        Showln "Inside Proc 2 of cMixinSuperClass"
    End_Procedure
End_Class

Class cSuperClass is a cObject
    Procedure Proc_3
        Showln "Inside Proc 3 of cSuperClass"
    End_Procedure
End_Class

Class cMixin is a cMixinSuperClass
    Procedure Proc_1
        Showln "Inside Proc 1 of cMixin"
    End_Procedure

    Procedure Proc_4
        Showln "Inside Proc 4 of cMixin"
    End_Procedure
End_Class

Class cSubClass is a cSuperClass
    Import_Class_Protocol cMixin cSubClass All Inherit
End_Class

Object oTest is a cSubClass
End_Object

Showln "This program demonstrates import class protocol"
Send Proc_1 to oTest
Send Proc_2 to oTest
Send Proc_3 to oTest
Send Proc_4 to oTest

The No_Overwrite keyword will cause the system to keep any definition of a message that already exists in the target class, dest, rather than overwrite it with another definition of the same name from the imported class. This option is useful when you want to merge two classes but not lose the implementations already defined in the target class. The default option is to overwrite the subclass with the imported implementation. To see this, you can change the preceding example by substituting the definition of cSubClass as shown below. The new subclass will execute Proc_4 as defined in the class cSubClass, rather than the implementation of cMixin.

Class cSubClass is a cSuperClass
    Procedure Proc_4
        Showln "Inside Proc 4 of cSubClass"
    End_Procedure

    Import_Class_Protocol cMixin cSubClass All Inherit No_Overwrite
End_Class

Notes

  • Imported message definitions containing the forward command should not be used. If an imported message definition includes a forward procedure call, the ancestors of the dest class will receive the forward, not the ancestors of source. The ancestors of dest may contain either no such message or a message of the specified name but with a definition differing from that required.