Open In App

Visibility Section in Classes | SAP ABAP

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other business software solutions. C++ is used to implement the ABAP kernel. A procedural and object-oriented programming model are both supported by the hybrid programming language ABAP.

Introduction to Classes in SAP ABAP

A class is a blueprint or template in SAP ABAP, a class defines the properties and behavior of objects. it encapsulates data – including the methods that act on this data. This encapsulation allows for modular code structures. reusable entities can be created with ease. Implementing various Object-Oriented Programming (OOP) concepts in SAP ABAP such as encapsulation, inheritance, and polymorphism on classes functioning as their foundation. Developers can create complex, structured applications through their use of these tools. this process not only facilitates superior code management but also enhances reusability.

Visibility Sections in SAP ABAP

In SAP ABAP (Advanced Business Application Programming), like in many other programming languages, you can control the visibility and accessibility of class components (attributes, methods, and events) using access modifiers, including public, private, and protected. These access modifiers determine where and how the class components can be accessed. Here’s an explanation of each:

Public Section in SAP ABAP

Components declared as public are accessible from anywhere, both within the class and from external programs or classes. This means they can be used by other classes or objects, making them part of the public interface of the class.

CLASS <class_name> DEFINITION.
PUBLIC SECTION.
" Define public attributes and methods.
ENDCLASS.

Protected Section in SAP ABAP:

Components declared as protected are similar to private components, but with an additional level of access. They are accessible from the same class and its subclasses (inheritors), but not from external programs or classes. Protected components are used when you want to provide access to subclasses but still keep them hidden from external code.

CLASS <class_name> DEFINITION.
PROTECTED SECTION.
" Define protected attributes and methods.
ENDCLASS.

Private Section in SAP ABAP:

Components declared as private are only accessible within the same class. They are not visible or accessible from external programs or classes. Private components are used to encapsulate the internal details of a class and are not part of the class’s public interface.

CLASS <class_name> DEFINITION.
PRIVATE SECTION.
" Define private attributes and methods.
ENDCLASS.

Example of Classes in SAP ABAP:

REPORT demo_class_usage.

CLASS demo_class DEFINITION.
PUBLIC SECTION.
METHODS:
display_name.
PRIVATE SECTION.
DATA:
name TYPE string.
ENDCLASS.

CLASS demo_class IMPLEMENTATION.
METHOD display_name.
WRITE: / 'Name:', name.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: obj TYPE REF TO demo_class.
CREATE OBJECT obj.
obj->name = 'John Doe'.
obj->display_name( ).

Output:

Name: John Doe

You use these access modifiers to control the visibility and access to the various parts of your class. By doing so, you can maintain the encapsulation of your class’s internal logic and provide a well-defined public interface for other parts of your ABAP program or other classes. This is a fundamental principle of object-oriented programming that helps improve code organization, reusability, and maintainability.

Related posts:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads