Mentor SAP
2017-05-25 Submitted by:- Admin

In this program, class C1 has three attributes declared in different sections as follows:-


 Commondata in public section
 Protectdata in private section
 Privatedata in private section


In the main program, an object, OBJ1 is created from class C1 and an incorrect attempt is made to display the protected and private attribute of class C1 using its object OBJ1. Compilation of this program produces an error.

REPORT zaccessibility.

CLASS parentclass DEFINITION.
  PUBLIC SECTION.
    DATA : commondata(30) TYPE c VALUE 'Accessible to all'.

  PROTECTED SECTION.
    DATA : protectdata(40) TYPE c VALUE 'Protected data'.

  PRIVATE SECTION.
    DATA : privatedata(40) TYPE c VALUE 'Private data'.
ENDCLASS.

CLASS parentclass IMPLEMENTATION.
ENDCLASS.


START-OF-SELECTION.
  DATA : parent TYPE REF TO parentclass.

  CREATE OBJECT : parent.

  WRITE : /5 parent->protectdata,
             parent->privatedata.

Output :

On compilation, an error will be generated which will prove that protected and private components of a class cannot be accessed by external users.