Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Instantiating and managing class-based objects : Defining an object reference return type
 

Defining an object reference return type

User-defined functions and class methods can define return types as object references (as class or interface types). When an object is passed locally, class instances are always returned by reference. Thus, only a reference to an object is returned, not the object itself. When an object is passed between an AppServer and an ABL client, the object is serialized by the sender, and the receiver uses this information to create a copy of the object. (See OpenEdge Application Server: Developing AppServer Applications for additional restrictions on objects passed between an AppServer an an ABL client.)
This is the syntax to define an object reference return type for a method:

Syntax

METHOD { PUBLIC | PROTECTED | PRIVATE }[ OVERRIDE ][ FINAL ][CLASS]   
object-type-namemethod-name ( [parameter[ , parameter]...] ) :
  [method-body]
This is the syntax to define an object reference return type for a user-defined function:
FUNCTION function-name RETURNS [CLASS]object-type-name
  ( [parameter[ , parameter]...] ) :
  [function-body]
Descriptions of the object reference return type syntax elements (bolded) follow:
CLASS
The CLASS keyword is required if object-type-name conflicts with an abbreviation for a built in ABL data type, such as INTE (INTEGER). Otherwise, it can optionally be used to clarify the readability of the statement.
object-type-name
The class or interface type name of the object reference to be returned. This can be the fully qualified object type name or the unqualified class or interface name, depending on the presence of an appropriate USING statement in the class or procedure file. For more information on object type names, see Defining and referencing object type names. For more information on the USING statement, see Referencing an object type name without its package.
For more information on the syntax of these statements, see the METHOD statement and FUNCTION statement reference entries in OpenEdge Development: ABL Reference.
The following code fragment, from the sample class acme.myObjs.CustObj, illustrates a method returning a class return type, in this case, to initialize a message object (acme.myObjs.Common.MsgObj) for use by the current instance of CustObj:
USING acme.myObjs.*.
USING acme.myObjs.Common.*.
USING acme.myObjs.Interfaces.*.

CLASS acme.myObjs.CustObj INHERITS CommonObj IMPLEMENTS IBusObj:
  DEFINE PRIVATE VARIABLE rMsg AS CLASS MsgObj NO-UNDO.
  ...

  CONSTRUCTOR PUBLIC CustObj( ):
    ...

    iNumCusts = 0.
    /* Fill temp table and get row count */
    FOR EACH Customer NO-LOCK WHERE Customer.CreditLimit > 50000:
      CREATE ttCustomer.
      ASSIGN
        iNumCusts          = iNumCusts + 1
        ttCustomer.RecNum  = iNumCusts
        ttCustomer.CustNum = Customer.CustNum
        ttCustomer.Name    = Customer.Name
        ttCustomer.State   = Customer.State.
    END.
    rMsg = MessageHandler ("acme.myObjs.CustObj").
  END CONSTRUCTOR.

  METHOD PROTECTED OVERRIDE CLASS MsgObj MessageHandler
    (INPUT iObjType AS CHARACTER):
    RETURN NEW MsgObj (iObjType).
  END METHOD.
  ...
END CLASS.
The MessageHandler( ) method definition implements an abstract method defined in the immediate super class. This method instantiates the MsgObj class and returns the object to the caller.
ABL treats a return value from a class method the same as an assignment. That is, the compiler verifies that the object reference being returned by the method is consistent with the variable to which the object reference is being assigned. For more information on the compatibility rules for assigning object references, see Assigningobject references.
Thus, the rules for assigning an object reference return value from within a method or user-defined function are the same as for assigning an OUTPUT object reference parameter from within a method. (For more information on passing object references as OUTPUT parameters, see Passingobject reference parameters.) The caller of the method must define a data element to receive the returned object reference that is the same class as the return object reference, a super class of the returned object reference, or an interface of the returned object reference. As with parameters, if the caller has as its target an object reference variable defined to reference a super class or interface, the caller can only invoke those methods and access data that are defined by the specified super class or interface. If the caller attempts to call a method or access data that is not defined in the super class or attempts to call a method not defined by the interface, the compiler generates an error.