Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Assigning object references : Using the CAST function : Casting an object reference parameter
 
Casting an object reference parameter
The following example classes are used by the following examples to demonstrate the use of the CAST function to pass INPUT object reference parameters. The first example class is a super class that defines nothing:
CLASS SuperClass:
END CLASS.
The next example class is a subclass that defines its own method, subClassMethod( ):
CLASS SubClass INHERITS SuperClass:
  METHOD PUBLIC VOID subclassMethod ( ):
  END METHOD.
END CLASS.
ABL does not, by default, allow an object reference of SuperClass to be used as an instance of SubClass. For example, if you try to execute subClassMethod( ) using the object reference to SuperClass, the method cannot be found. However, if you know that an object reference to SuperClass in reality points to an instance of SubClass, you can cast the reference as SubClass to use it.
The following example demonstrates the use of the CAST function to pass an INPUT object reference parameter. The Main container class defines an object reference (mySuper) for the super class and a method (subParmMethod( )) that expects an object parameter of type SubClass, as shown:
CLASS Main:
  DEFINE PRIVATE VARIABLE mySuper AS CLASS SuperClass NO-UNDO.

  CONSTRUCTOR PUBLIC Main ( ):
    mySuper = NEW SubClass ( ).
    subParmMethod (CAST(mySuper, SubClass)).
  END CONSTRUCTOR.

  METHOD PRIVATE VOID subParmMethod
    (INPUT myLocalSubClass AS CLASS SubClass):
    myLocalSubClass:subClassMethod( ).
  END METHOD.
END CLASS.
In the previous example, the constructor instantiates a SubClass object, but assigns it to the SuperClass object reference. Using the CAST function, the constructor then invokes subParmMethod( ), passing the object reference as the SubClass parameter the method expects and that it actually is. The subParmMethod( ) method then invokes the subClassMethod( ) method on the passed object reference.