So I've being doing ABAP and ABAP Objects for a while now. Then I found out that accessing a protected attribute 'externally' is possible as long as you've initiated the call from inside one of its subclass.
Is anyone else surprised by this? In other languages, I don't think this is allowed. Do you think this is a good feature? Doesn't this make code less secure?
In the code below, you can see that an instance of lcl_child tried to access the protected attribute 'name' of another instance whose type is lcl_parent - a superclass of lcl_child.
CLASS lcl_parent DEFINITION.
p_name TYPE string.
DATA name TYPE string.
CLASS lcl_parent IMPLEMENTATION.
name = p_name.
CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.
IMPORTING p_o_parent TYPE REF TO lcl_parent.
CLASS lcl_child IMPLEMENTATION.
super->Constructor( 'BABY' ).
" Should I be able to access this 'PUBLICLY' because
" I'm inside the subclass of lcl_parent? Note that this
" can be a different instance from me.
o_parent TYPE REF TO lcl_parent,
o_child TYPE REF TO lcl_child.
CREATE OBJECT o_parent EXPORTING p_name = 'MOMMY'.
CREATE OBJECT o_child.
* WRITE o_parent->name. " error. not accessible!
o_child->Test( o_parent ). " outputs 'MOMMY', why possible?!?!?!?!