Release 10.1A: OpenEdge Getting Started:
Object-oriented Programming

> ## Assigning object references

> You can freely assign one object reference to another object reference if they are defined with compatible class types. The object references in an assignment have compatible class types if one of the following is true:

> - Both references are defined as the same class or interface type (`rCustObj` `=` `rCustObj` or `rIBusObj` `=` `rIBusObj`).

> - The left-hand reference is defined as a super class of the right-hand reference (`rCommonObj` `=` `rCustObj`).

> - The left-hand reference is defined as an interface implemented by the right-hand reference (`rIBusObj` `=` `rCustObj`).

> This assignment always sets the left-hand object reference to a copy of the right-hand object reference that points to the same object, not a reference to a new copy of the object.

> Also, if the left-hand object reference already has a reference to an object, and there is no other available reference to that object, you must make sure to delete that object before reassigning the reference. Otherwise, you have created a memory leak—the previous reference is overwritten with the new value and the previous object continues to exist with no object references available to delete it. This is very similar to the precautions you must take to avoid memory leaks while maintaining persistent procedures and other types of dynamic 4GL objects.

> When you assign a subclass object reference to a super class object reference, the new object reference still points to the original object instance, but provides access to the object according to the super class type. Thus, if you use the super class object reference, you can only access the methods defined in that super class.

> You can also assign an interface object reference to any class object reference, where the class implements the specified interface type at any level in its class hierarchy. If you use the interface object reference, you can only access the methods defined in the interface.

> So an object reference can be copied to an object reference of the same type, a super type, or an interface that the object implements. The converse of this rule is not true. An object reference to a super class cannot be copied to a subclass object reference unless you use the `CAST` function. The compiler rejects this assignment and only allows it if an appropriate `CAST` function is provided on the right-hand side (see the "Assignment and the CAST function" section).

> The following variation on the sample `Main` class demonstrates compatible assignment between different types of object references using the sample classes. These assignments are indicated in this `Main` class code by the following numbered lines:

> 1. Assignment between identical class types (`rCustObj` to `rCustObj`).

> 2. Assignment to a super class type from one of its subclass types (`rCustObj` to `rCommonObj`).

> 3. Assignment to an interface type from a class that implements that interface (`rCustObj` to `rIBusObj`).

> This is the `Main` class with the numbered lines:

>     CLASS Main:
>         DEFINE PRIVATE VARIABLE rCustObj
>             AS CLASS acme.myObjs.CustObj NO-UNDO.
>         DEFINE PRIVATE VARIABLE rCustObj2
>             AS CLASS acme.myObjs.CustObj NO-UNDO.
>         DEFINE PRIVATE VARIABLE rCommonObj
>             AS CLASS acme.myObjs.Common.CommonObj NO-UNDO.
>         DEFINE PRIVATE VARIABLE rIBusObj
>             AS CLASS acme.myObjs.Interfaces.IBusObj NO-UNDO.
>         ...
>         CONSTRUCTOR PUBLIC Main( ):
>             ...
>             rCustObj = NEW acme.myObjs.CustObj( ).
>         END CONSTRUCTOR.
>         METHOD PUBLIC VOID ObjectInfo( ):
>             rHelperClass:ListNames(INPUT-OUTPUT rCustObj).
>             rCustObj:CheckCredit( ).
>     /*#1*/  rCustObj2 = rCustObj.
>             rHelperClass:ListDate(INPUT rCustObj).
>             MESSAGE rCustObj:timestamp.
>     /*#2*/  rCommonObj = rCustObj.
>             rHelperClass:ReportOutput(OUTPUT rIBusObj).
>             rIBusObj:logObj(INPUT "Customer.out").
>     /*#3*/  rIBusObj = rCustObj.
>         END METHOD.
>         DESTRUCTOR PUBLIC Main( ):
>             DELETE OBJECT rCustObj.
>             rCustObj = ?.
>             ...
>         END DESTRUCTOR.
>     END CLASS.

> Note that all of these object references, in fact, point to the same instance of one object. Thus, the destructor for `Main` invokes only one `DELETE` `OBJECT` statement for the `acme.myObjs.CustObj` instance. When the destructor executes, this statement deletes the instance of `acme.myObjs.CustObj` that all three object references point to, and all four variables become invalid object references.

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095