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 to invoke a method
 
Casting an object reference to invoke a method
The following example shows an alternative Main class used to access the sample classes SuperClass and SubClass defined in the previous example, this time to demonstrate the use of the CAST function to cast an object reference for a method call:
CLASS Main:
  DEFINE PRIVATE VARIABLE mySuper AS CLASS SuperClass NO-UNDO.

  CONSTRUCTOR PUBLIC Main ( ):
    mySuper = NEW SubClass ( ).
    CAST(mySuper, SubClass):subClassMethod ( ).
  END CONSTRUCTOR.
END CLASS.
In the previous example, the constructor sets the SuperClass object reference to a new SubClass instance, then invokes subClassMethod( ) on the SubClass instance by casting the SuperClass object reference to SubClass.