Public, Protect and Private access in OOPs


Class components will have to be declared under any of visibility sections like Public Section, Protected Section and Private Section. These visibility sections will have to be declared with this order. That means protected section or Private section cannot come before Public section. As per the requirement if the program only needs to contain Public section then there is no need to declare further section. This is similar to the Protected and Private section also.

The data and methods declared in Public Section in a class can be and accessed by that class and any user, subclass of the program.
When the data and methods are declared in Protected Section in a class then those will be accessed by that class and sub-classes only. Sub-classes can be any child class of the main class.
When the data and methods are declared in Private Section in a class then those will be accessed by only that class, not by any other class.

Below is a program by which these 3 condition can be reflected:

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    DATA v_pub_parent TYPE char50 VALUE 'Parent class public data'.
    METHODS m_parent.

  PROTECTED SECTION.
    DATA v_pro_parent TYPE char50 VALUE 'Parent class protected data'.

  PRIVATE SECTION.
    DATA v_pri_parent TYPE char50 VALUE 'Parent class private data'.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS m_child.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_parent.
    WRITE: / 'Accessing components from Parent class',
           / v_pub_parent,
           / v_pro_parent,
           / v_pri_parent.
    SKIP.
  ENDMETHOD.                    "m_parent
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_child.
    WRITE: / 'Accessing components from Child class',
           / v_pub_parent,
           / v_pro_parent.
    SKIP.
  ENDMETHOD.                    "m_child
ENDCLASS.                    "child IMPLEMENTATION

DATA: obj_parent TYPE REF TO parent,
      obj_child  TYPE REF TO child.

START-OF-SELECTION.  CREATE OBJECT: obj_parent, obj_child.

  CALL METHOD: obj_parent->m_parent,
               obj_child->m_child.

  WRITE: / 'Every user can access public data',
         / obj_parent->v_pub_parent.

Now at debugging level we shall see the accessing components of same class. Public, protected & private components are accessible from the same class.


From the child / sub class public & protected components are accessible.


From the user level only the public components are accessible.


Below is the output of this.

Share this

Related Posts