Hello my fellow ABAPers(others are welcome too!).
When first facing object orientation in ABAP, some of you have probably tried to do something like this (at least I did..):
CLASS lcl_fs_test DEFINITION.
PRIVATESECTION.
DATA: priv TYPE c LENGTH3VALUE'foo'.
ENDCLASS.
START-OF-SELECTION.
DATA: o_access TYPEREFTO lcl_fs_test.
FIELD-SYMBOLS<priv>TYPEany.
CREATEOBJECT o_access.
ASSIGN('O_ACCESS->PRIV')TO<priv>.
CLASS lcl_fs_test IMPLEMENTATION.
ENDCLASS.
and it resulted in short dump .
Well, some days ago I was going to work when I had an idea, what would happen if we tried to access the reference of the private attribute instead?
REPORT zfaw_test_reference.
CLASS lcl_reference_test DEFINITION.
PUBLICSECTION.
METHODS:
get_private_attribute RETURNING value(private_var)TYPE char3,
get_private_by_ref EXPORTINGdataTYPEREFTOdata.
PRIVATESECTION.
DATA: private_variable TYPE c LENGTH3VALUE'foo'.
ENDCLASS.
CLASS lcl_reference_test IMPLEMENTATION.
METHOD get_private_attribute.
private_var = me->private_variable.
ENDMETHOD.
METHOD get_private_by_ref.
GET REFERENCE OF me->private_variable INTOdata.
ENDMETHOD.
ENDCLASS.
DATA: reference_tester TYPEREFTO lcl_reference_test,
private_ref TYPEREFTOdata,
private_content TYPE char3.
FIELD-SYMBOLS: <private_content>LIKE private_content.
START-OF-SELECTION.
CREATEOBJECT reference_tester.
private_content = reference_tester->get_private_attribute().
WRITE: / 'Value before: ', private_content.
reference_tester->get_private_by_ref(IMPORTINGdata = private_ref ).
ASSIGN private_ref->* TO<private_content>.
<private_content> = 'bar'.
private_content = reference_tester->get_private_attribute().
WRITE: / 'After get by reference: ', private_content.
And this works!
Does this break any OO concept? I have no idea, I'm a newb!
What I know is, we have to create a special method to allow this kind of changes and so, our intent IS to allow this kind of behaviour!
But without the proper getters/setters we won't control how the attribute is changed, anyone can change the attribute from anywhere in the program, moreover, if we allow this kind of behaviour in our class why not declare the variable as public and save us all the trouble.
I don't know what I should think of this, I just find it interesting and I wanted to share =].