Examples of Classes in SAP ABAP
Here are two sample programs that illustrate the usage of classes in SAP ABAP.
Program 1: A Simple Class and Object Creation
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
Program 2: Using Static Attribute and Method
REPORT demo_static_class.
CLASS demo_static_class DEFINITION.
PUBLIC SECTION.
CLASS-DATA: counter TYPE i.
CLASS-METHODS: increment_counter, display_counter.
ENDCLASS.
CLASS demo_static_class IMPLEMENTATION.
METHOD increment_counter.
counter = counter + 1.
ENDMETHOD.
METHOD display_counter.
WRITE: / 'Counter:', counter.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo_static_class=>increment_counter( ).
demo_static_class=>display_counter( ).
Output :
Counter: 1
Classes in SAP ABAP
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.
Table of Content
- Introduction to Classes in SAP ABAP
- Syntax for defining a class in SAP ABAP
- Syntax for implementing a class in SAP ABAP
- Attributes in SAP ABAP Classes
- Methods in SAP ABAP Classes
- Accessing Attributes and Methods in SAP ABAP:
- Static Attributes
- Constructors in SAP ABAP
- ME operator in methods
- Examples of Classes in SAP ABAP