The declaration section of a class can be split into up to three different visibility sections.
These sections define the external visibility of the class components and therefore the interfaces of the class for all users allowed by the package concept. Each component of a class must be explicitly assigned to one of the visibility sections.
- Public visibility section
All components declared in the public visibility section defined with PUBLIC SECTION are accessible to all users as well as in the methods of all heirs and in the class itself. The public components are the interface of the class to each user.
- Protected visibility section
All components declared in the protected visibility section defined with PROTECTED SECTION are accessible in the methods of all heirs and in the class itself. The protected components form a specific interface between the class and its subclasses.
- Private visibility section
All components declared in the private visibility section defined with PRIVATE SECTION are only accessible in the methods of the class itself, and are not even visible to the heirs. The private components therefore do not form an interface to the users of the class.
The following table groups the visibilities for a class:
Visible for | PUBLIC SECTION | PROTECTED SECTION | PRIVATE SECTION |
Same class and its friends | X | X | X |
Any subclasses | X | X | - |
Any Repository objects | X | - | - |
Note
A subclass cannot access the protected components from a subclass of a different branch in the hierarchy that is inherited from a common superclass.
Encapsulation
The three visibility sections form the basis for the important object property of encapsulation in ABAP Objects. When declaring a class, you should declare the minimum possible number of components in the public section and these public components must be created carefully. For global classes, they can no longer be changed after the class is released.
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. The exceptions to this rule are subclasses, which cannot access the private components of superclasses if they are not their friends.
Example
In the method m1 of the class c1, reference variables of the static type c1 can be used to access the protected attribute a11 and the private attribute a12 of any objects of c1. In the method m2 of the subclass c2, a reference variable of the static type c2 can similarly be used to access the protected attribute a11. Access to the attribute using a reference variable of the static type c1 is not permitted, as otherwise it would also be possible to access an object of the superclass or another subclass. It is not possible to access the private attribute of the superclass with either reference variable.
- CLASS c1 DEFINITION.
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.
No comments:
Post a Comment