Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Confused with abap inheritating

former_member186273
Participant
0 Kudos

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??

1 ACCEPTED SOLUTION

Former Member
0 Kudos
Hello Ethan,
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.
Yes, this is true. The reasoning is best explained with an example. In your example above, I create another child class ZCL_CHILD2 Inheriting from ZCL_PARENT.
ZCL_CHILD2 Definition.
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
Now, ZCL_CHILD  and ZCL_CHILD2 are not aware of each other, that is they do not know that they have a common parent ( The siblings of a parent are not aware of each other ).
So , it is technically not possible for  a method in  ZCL_CHILD1 to access protected members of ZCL_CHILD2 even though they might share a common parent and the access might be to the inherited members of the parent.The code snippet below would throw a syntax  error.
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
However, there is a back door entry , if i were to use a super class reference, and get the reference of an object of ZCL_CHILD2 inside a method in ZCL_CHILD, then It would be technically changing the attributes of an object for which it has no rights to do so.
The code snippet  below, is a clear example of the violation. The previous code snippet tried to perform the same activity , but it threw an error. However, using the super class as a reference type, it allows us to do so.
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
It is not a hard enforced error , as it is difficult for the compiler to know which type of  object it would refer to at compile time .   However as mentioned in the documentation, it does throw a warning when using Extended syntax check.
Syntax check warning
This warning is only displayed in SLIN
You 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
     I guess, this is fine. No issues with this though.
Hope this helps.
Thanks,
Venkat.

Message was edited by: Venkat Gowrishankar - Added Comment code - for an example of a dynamic type instantiation.

7 REPLIES 7

Former Member
0 Kudos
Hello Ethan,
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.
Yes, this is true. The reasoning is best explained with an example. In your example above, I create another child class ZCL_CHILD2 Inheriting from ZCL_PARENT.
ZCL_CHILD2 Definition.
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
Now, ZCL_CHILD  and ZCL_CHILD2 are not aware of each other, that is they do not know that they have a common parent ( The siblings of a parent are not aware of each other ).
So , it is technically not possible for  a method in  ZCL_CHILD1 to access protected members of ZCL_CHILD2 even though they might share a common parent and the access might be to the inherited members of the parent.The code snippet below would throw a syntax  error.
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
However, there is a back door entry , if i were to use a super class reference, and get the reference of an object of ZCL_CHILD2 inside a method in ZCL_CHILD, then It would be technically changing the attributes of an object for which it has no rights to do so.
The code snippet  below, is a clear example of the violation. The previous code snippet tried to perform the same activity , but it threw an error. However, using the super class as a reference type, it allows us to do so.
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
It is not a hard enforced error , as it is difficult for the compiler to know which type of  object it would refer to at compile time .   However as mentioned in the documentation, it does throw a warning when using Extended syntax check.
Syntax check warning
This warning is only displayed in SLIN
You 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
     I guess, this is fine. No issues with this though.
Hope this helps.
Thanks,
Venkat.

Message was edited by: Venkat Gowrishankar - Added Comment code - for an example of a dynamic type instantiation.

0 Kudos

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

0 Kudos

Hello ,

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

0 Kudos

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

0 Kudos

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

naimesh_patel
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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