SAP CLASS VISIBILITY
Get Example source ABAP code based on a different SAP table
ARTICLE
Visibility Sections in Classes
The
These sections define the external visibility of the
All components declared in the public visibility section defined with
All components declared in the protected visibility section defined with
All components declared in the private visibility section defined with
The following table summarizes the visibilities of a class:
Note
A subclass can generally never access the protected components of a subclass from a different branch in the inheritance hierarchy, even if they are inherited from a common superclass. This rule can only be lifted by a friendship.
Encapsulation
The three visibility sections form the basis for the important feature of encapsulation in ABAP Objects. When declaring a class, you should ensure you declare the minimum possible number of components in the public section and create these public components carefully. The public components of global classes may not be changed once you have released the class.
ABAP_PGL
Note
The class is the smallest encapsulation unit in ABAP Objects. A method can therefore use all components of all instances of the same class, except for the components of its own class. An exception to this rule are subclasses that cannot access the private components of superclasses, if they are not their
Example
In method
PUBLIC SECTION.
METHODS m1.
PROTECTED SECTION.
DATA a11 TYPE i.
PRIVATE SECTION.
DATA a12 TYPE i.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
DATA lref1 TYPE REF TO c1.
lref1->a11 = 0. 'OK
lref1->a12 = 0. 'OK
ENDMETHOD.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS m2.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m2.
DATA: lref1 TYPE REF TO c1,
lref2 TYPE REF TO c2.
lref1->a11 = 0. 'Syntax warning, access to a11 not permitted
lref2->a11 = 0. 'OK
'lref1->a12 = 0. 'Syntax error, access to a11 not permitted
'lref2->a12 = 0. 'Syntax error, a12 not visible
ENDMETHOD.
ENDCLASS.