02-20-2013 10:27 AM
Hi All
Today when I'm reading the abap documentation regarding to the protected section. It has following statements.
- In subclasses, it is not possible to access the protected components of the superclasses using reference variables of the type of the superclass, because otherwise an attribute of an object of the dynamic type of the superclass or another subclass could be changed. In the last case, a warning is produced by the extended syntax check.
It said it's not possible to access protected components of superclass by using reference variables of type of superclass. so I wrote the following abap snippets to test it.
class ZCL_PARENT definition
create public .
public section.
data public_var type i.
methods public_method.
protected section.
data protected_var type i.
methods protected_method.
private section.
data private_var type i.
ENDCLASS.
CLASS ZCL_PARENT IMPLEMENTATION.
method public_method.
Write: / 'Public method'.
endmethod.
method protected_method.
write: / 'Protected method'.
endmethod.
ENDCLASS.
Child class to extend the parent.
class ZCL_CHILD definition
public
inheriting from ZCL_PARENT
create public .
public section.
data cpublic_var type i.
data p type ref to zcl_parent.
protected section.
data cprotected_var type i.
methods c_protected_method.
private section.
data c_private_var type i.
ENDCLASS.
CLASS ZCL_CHILD IMPLEMENTATION.
method c_protected_method.
write:/ 'child protected method'.
p->protected_var = 2.
me->cprotected_var = 3.
endmethod.
ENDCLASS.
As you can see the screenshot that I can actually access the protected components by using p->.
What's going on here? And why I couldn't access my own protected components (cprotected_var) even under class zcl_child??
02-20-2013 2:47 PM
In subclasses, it is not possible to access the protected components of the superclasses using reference variables of the type of the superclass, because otherwise an attribute of an object of the dynamic type of the superclass or another subclass could be changed. In the last case, a warning is produced by the extended syntax check.
CLASS zcl_child2 DEFINITION
INHERITING FROM zcl_parent
CREATE PUBLIC .
PUBLIC SECTION.
DATA cpublic_var TYPE i.
DATA p TYPE REF TO zcl_parent.
METHODS c_protected_method.
PROTECTED SECTION.
DATA cprotected_var TYPE i.
PRIVATE SECTION.
DATA c_private_var TYPE i.
ENDCLASS. "zcl_child2 DEFINITIO
CLASS zcl_child2 IMPLEMENTATION.
METHOD c_protected_method.
DATA : lr_child TYPE REF TO zcl_child.
CREATE OBJECT lr_child.
lr_child->protected_var = '1'.
WRITE:/ 'child protected method'.
p->protected_var = 2.
me->cprotected_var = 3.
ENDMETHOD. "c_protected_method
ENDCLASS. "zcl_child2 IMPLEMENTATION
CLASS zcl_child2 IMPLEMENTATION.
METHOD c_protected_method.
DATA : lr_child TYPE REF TO zcl_child,
lr_super TYPE REF TO zcl_parent.
CREATE OBJECT lr_child. "or CREATE OBJECT lr_super TYPE ('ZCL_CHILD').
lr_super = lr_child.
lr_super->protected_var = '1'.
WRITE:/ 'child protected method'.
p->protected_var = 2.
me->cprotected_var = 3.
ENDMETHOD. "c_protected_method
ENDCLASS. "zcl_child2 IMPLEMENTATION
Syntax check warningThis warning is only displayed in SLINYou can no longer access protected components using a superclass reference.
And why I couldn't access my own protected components (cprotected_var) even under class zcl_child
Message was edited by: Venkat Gowrishankar - Added Comment code - for an example of a dynamic type instantiation.
02-20-2013 2:47 PM
In subclasses, it is not possible to access the protected components of the superclasses using reference variables of the type of the superclass, because otherwise an attribute of an object of the dynamic type of the superclass or another subclass could be changed. In the last case, a warning is produced by the extended syntax check.
CLASS zcl_child2 DEFINITION
INHERITING FROM zcl_parent
CREATE PUBLIC .
PUBLIC SECTION.
DATA cpublic_var TYPE i.
DATA p TYPE REF TO zcl_parent.
METHODS c_protected_method.
PROTECTED SECTION.
DATA cprotected_var TYPE i.
PRIVATE SECTION.
DATA c_private_var TYPE i.
ENDCLASS. "zcl_child2 DEFINITIO
CLASS zcl_child2 IMPLEMENTATION.
METHOD c_protected_method.
DATA : lr_child TYPE REF TO zcl_child.
CREATE OBJECT lr_child.
lr_child->protected_var = '1'.
WRITE:/ 'child protected method'.
p->protected_var = 2.
me->cprotected_var = 3.
ENDMETHOD. "c_protected_method
ENDCLASS. "zcl_child2 IMPLEMENTATION
CLASS zcl_child2 IMPLEMENTATION.
METHOD c_protected_method.
DATA : lr_child TYPE REF TO zcl_child,
lr_super TYPE REF TO zcl_parent.
CREATE OBJECT lr_child. "or CREATE OBJECT lr_super TYPE ('ZCL_CHILD').
lr_super = lr_child.
lr_super->protected_var = '1'.
WRITE:/ 'child protected method'.
p->protected_var = 2.
me->cprotected_var = 3.
ENDMETHOD. "c_protected_method
ENDCLASS. "zcl_child2 IMPLEMENTATION
Syntax check warningThis warning is only displayed in SLINYou can no longer access protected components using a superclass reference.
And why I couldn't access my own protected components (cprotected_var) even under class zcl_child
Message was edited by: Venkat Gowrishankar - Added Comment code - for an example of a dynamic type instantiation.
02-21-2013 3:21 AM
Hi Naimesh
Thanks for your reply. I have further question regarding to it.
First, let me upload the screenshot that missed in my previous post.
The code was written in Eclipse with ADT plugins.
My question is actually against the documentation, the doc says "In subclass it is not possible to access the protected components of the superclasses using reference variables of the type of the superclass " As you can see from the screenshot, where I can access protected_var and protected_method from the subclass zcl_child with a refernece p of type superclass zcl_parent.
And I don't even need the backdoor approach.
You know I'm used to be a java developer so I'm very confused with this statement. In Java world, the object instance can access public and protected attributes directly. However, it's not the case in ABAP.
But from the example I provided, it seems to me that the document is wrong.
Please correct me if I misunderstand the documentation.
Thanks
Ethan
02-21-2013 12:22 PM
The documentation does confuse, but at the end it does tell us that it is a warning and will not throw an error.
The back door entry is just an example to show the reasoning behind the documentation . In my opinion, I feel the documentation is too strongly written, however its intentions is to ensure that the developer avoid using the back door approach shown above.
I hope this clears your doubt.
Thanks
Venkat
02-21-2013 3:22 PM
Documentation on Protected Section seems to be confusing but it also notes down the fact that it could lead to error situation by dynamic assignment.
I'm not sure how Extended Syntax check is integrated in Eclipse using ADT plugin, I haven't tried using ADT plugin on my Eclipse yet. Extended Syntax check in ABAP Editor produces a warning, which can't be hidden using any Pragmas or Pseudo comment. That says like - I don't want you to use this. But if you want to use it use anyways, I wont allow you to hide that from others.
Regards,
Naimesh Patel
03-01-2013 5:10 AM
I created a post ABAP Protected Section – Is it really Protected? on my personal blog. Lets see what others has to say.
Thanks,
Naimesh Patel
02-20-2013 3:47 PM
Since the PROTECTED attributes are visible to the Inherited objects, system would allow them to have access of PROTECTED components in the Subclass.
Even though, it is allowed, you should take extra caution when using this approach to gain access of the PROTECTED attributes in the Subclass having a different Object reference than itself. As long as you are using the PROTECTED attributes referring to its own object reference ME->, it would be ok and robust.
If you use any other object reference and gain access of PROTECTED attributes, you may run into issues, when the object assigned this object reference is more specific or having reference generated using different branch of inheritance. This could definitely happen, if your application gets extended in future. Your Application may be as robust as it could be when accessing these attributes using other object reference.
Regards,
Naimesh Patel
02-21-2013 9:55 AM
Documentation also menitoned...it is just a warning in extended check.
If you do extended check you can see...a syntax check warning like this.
menu
program->check->extended program check
Messages for Syntax Check Warnings(Warnings)
Program: ZTEST_AMAR Row: 54
Syntax check warning
This warning is only displayed in SLIN
You can no longer access protected components using a superclass reference. any more.
Internal Message Code: MESSAGE G-8
Cannot be hidden using a pragma or pseudo-comment