Object Oriented ABAP Class visibility sections

In this article we will understand in detail about the ABAP Class visibility sections.

  • In an ABAP class, you can have three visibility sections PUBLIC SECTION, PROTECTED SECTION, PRIVATE SECTION.
  • There is no default visibility section. Hence, you must declare at least one of the three visibility section when defining ABAP Class.
  • All components of the class will belong to one of the three visibility sections.
  • You have to define the visibility section in the sequence, PUBLIC SECTION, PROTECTED SECTION and PRIVATE SECTION.

The above points are summarized in below pseudo code.

CLASS <classname> DEFINITION.
PUBLIC SECTION.
..class components under public section
PROTECTED SECTION.
..class components under protected section
PRIVATE SECTION.
..class components under private section
ENDCLASS.

Let’s understand few more points related to visibility sections.

  • Only components under PUBLIC SECTION can be accessed from outside class.
  • Components under PROTECTED SECTION cannot be accessed from outside class. Protected section components can be accessed from within the class as well as in methods of the subclass,
  • PRIVATE SECTION components cannot be accessed from outside the class as well as it is not available to inheritors of the class.

Let’s understand the visibility concept in ABAP Class with the help of an example.

In the above code, class LCL_SUB inherits class LCL. In both classes, we have defined the three visibility sections.

The superclass has 3 methods, m1, m2 and m3 under each visibility section PUBLIC, PROTECTED and PRIVATE respectively. The subclass has methods m4, m5, m6 under PUBLIC, PROTECTED and PRIVATE section respectively.

In superclass, we see that we can access all methods of the class inside a method, indicating that all components under PUBLIC, PROTECTED or PRIVATE are available within the class.

In the subclass, we notice that we can access components under PUBLIC and PROTECTED section of parent class inside the child class method. Thus, all components under PUBLIC and PROTECTED section of parent class are available to the components of child class.

With object ‘lo’ of superclass, we can access method M1 only. Thus, all components defined under PUBLIC SECTION are accessible from outside the class, but we cannot access components defined under PRIVATE SECTION and PROTECTED SECTION from outside the class.

With object ‘lo_sub’ of subclass, we can access methods M1 and M4. Thus, with the object of subclass, we can access components defined under PUBLIC SECTION of both parent as well as child class. We cannot access the components of PROTECTED and PRIVATE section of both parent as well as child class from outside the class.

Summary

It is thus understood that the PROTECTED SECTION is a special section inside which you place components that should be made available to the inheritor class. PROTECTED and PRIVATE section components cannot be accessed by object of the class.