F Visibility section in class | CodeTheta

Visibility section in class

June 24, 2024

There are 3 types of visibility sections in the class.

Public Section
In the public section of the class access available inside the class and outside the class.

Protected Section
In protected section access available inside the class and its child class and accessible outside the class is not possible.

Private Section
In private section access available inside the class and its child class and outside the class is not possible.

Sample Code:
CLASS lcl_sample DEFINITION.
  PUBLIC SECTION.
    METHODS: public_method.

  PROTECTED SECTION.
    DATA: protected_attr TYPE string.
    METHODS: protected_method.

  PRIVATE SECTION.
    DATA: private_attr TYPE string.
    METHODS: private_method.
ENDCLASS.

CLASS lcl_sample IMPLEMENTATION.
  METHOD public_method.
    " Public method implementation
  ENDMETHOD.

  METHOD protected_method.
    " Protected method implementation
  ENDMETHOD.

  METHOD private_method.
    " Private method implementation
  ENDMETHOD.
ENDCLASS.

DATA: lo_sample TYPE REF TO lcl_sample.

CREATE OBJECT lo_sample.

" Accessing public method
lo_sample->public_method( ).

" Accessing protected attribute (Allowed within the class and any subclass)
lo_sample->protected_attr = 'Protected attribute value'.

" Accessing private attribute (Not allowed outside the class)
" lo_sample->private_attr = 'Private attribute value'. " This would raise a syntax error

IDE Used To Test This Code : SAP ABAP Editor.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us. We will reply you as soon as possible.

Post a Comment