2021 Mar 28 9:06 PM
How to access attributes and methods of object referenced by object_reference of static_type OBJECT created with a dynamic_type of cl_class where cl_class its own attributes and methods.
CLASS cl_name DEFINITION.
PUBLIC SECTION.
DATA title TYPE string.
METHODS: "NO CONSTRUCTOR
change_title IMPORTING i_title TYPE string ,
display_title.
ENDCLASS.
CLASS cl_name IMPLEMENTATION.
METHOD change_title.
title = i_title.
ENDMETHOD.
METHOD display_title.
DATA: lv_string TYPE string.
CONCATENATE 'Title of the class is' lv_string INTO lv_string SEPARATED BY space.
MESSAGE lv_string TYPE 'I'.
RETURN.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
data l_rcl_object type ref to object.
CREATE OBJECT l_rcl_object TYPE cl_name.
"HOW TO ACCESS title of the object referenced by l_rcl_object->title or call method l_rcl_object->change_title or"l_rcl_object->display_title?
2021 Mar 28 9:34 PM
it would be a better approach if you can create an interface with the attributes and methods that you will be reusing in the sub classes. Then you can create an object of type that interface and instantiate with that class reference. Then you can use that interface to access the common attributes and objects.
Else if you can dynamically acces like shown in the below help
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenclass_components_addressing.htm
2021 Mar 28 9:34 PM
it would be a better approach if you can create an interface with the attributes and methods that you will be reusing in the sub classes. Then you can create an object of type that interface and instantiate with that class reference. Then you can use that interface to access the common attributes and objects.
Else if you can dynamically acces like shown in the below help
https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abenclass_components_addressing.htm
2021 Mar 30 1:40 AM
Thanks a lot. Dynamic access solved my query and found a better approach.
2021 Mar 29 7:06 AM
The compiler/syntax checker sees that TITLE is not a member of OBJECT "class" so that gives an error. You have to use what is called "down casting":
DATA(l_rcl_name) = CAST cl_name( l_rcl_object ).
OR
DATA l_rcl_name TYPE REF TO cl_name.
l_rcl_name ?= object.then you can use L_RCL_NAME->TITLE, etc.
Read the ABAP documentation for more information.
2021 Mar 30 1:48 AM
2021 Mar 29 9:35 PM
As alternative to explicit cast, proposed by Sandra, the syntax for dynamic call could be used:
CALL METHOD l_rcl_object->('CHANGE_TITLE') EXPORTING i_title = 'Some title'.
CALL METHOD l_rcl_object->('DISPLAY_TITLE').
additionally title variable is missing in the method display_title
CONCATENATE 'Title of the class is' title INTO lv_string SEPARATED BY space.
2021 Mar 30 1:38 AM
2021 Mar 30 7:24 AM
Please edit your question, select your code and press the "CODE" button to make it correctly colorized/indented, so that it's easier for us to analyze it. Thank you.
2021 Mar 30 8:22 AM