![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP La definición de Clases, Atributos, Métodos y Eventos
ABAP La definición de Clases, Atributos, Métodos y Eventos
![ABAP La definición de Clases, Atributos, Métodos y Eventos ABAP La definición de Clases, Atributos, Métodos y Eventos](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/435.jpg)
Defining Classes, Attributes, Methods, and Events
Local and Global Classes
In ABAP, classes can be declared either globally or locally. Global classes are defined in the class builder, via transaction SE24 in the ABAP Workbench. These classes are stored in Class Pools within the class library in the R/3 repository. All ABAP programs in an R/3 system can access global classes.
Local classes, on the other hand, are defined within an ABAP program and can only be invoked from the program in which they are defined. When using a class in an ABAP program, the system first searches for a local class with the specified name. If none is found, it looks for a global class. Besides visibility, there is no difference between using a global or local class. However, the way a local class and a global class are created differs significantly.
When defining a class to be used in a single program, it's usually sufficient to define the visible components to suit our program. Conversely, global classes must be prepared to be used anywhere.
Definition of Local Classes
Local classes comprise statements enclosed between CLASS... ENDCLASS. A complete class definition consists of a declarative part, known as class definition, where components are defined, and an implementation part, known as class implementation, where these components are implemented.
The declarative part or class definition is delimited by:
CLASS DEFINITION.
...
ENDCLASS.
The declarative part contains the declaration of all class components (attributes, methods, and events). When defining local classes, the declarative part belongs to the global data of the program and thus should be positioned at the beginning of the program.
If methods are declared in the declarative part of a class, their implementation part should also be written. This is included between the following statements:
CLASS IMPLEMENTATION.
...
ENDCLASS.
The implementation part contains the implementation of all class methods. This part acts as a block, meaning any code section not part of the block won't be accessible.
Class Structure and Components
The structure of a class is primarily defined based on:
- A class contains components.
- Each component is assigned to a visibility section (public, protected, or private).
- Classes implement methods.
Class components represent its contents and are all declared in the declarative part of the class. Components define the attributes of objects in a class.
When defining a class, each component is assigned to one of three different visibility sections, defining the external interface of the class. All components of a class are visible within the class and share the same space, so their names must be unique within the class.
There are two types of components in a class: those that exist separately for each object of a class, and those that exist only once for the entire class, regardless of the number of instances. These components are known as instance-dependent or instance-independent (or static) components, respectively.
In ABAP objects, classes can define the following components:
- Attributes
- Methods
- Events.
Attributes
Attributes are the internal data fields of a class and can have any ABAP data type. An object's state is determined by the content of its attributes. One type of attribute is referenced variables. These variables allow creating and accessing objects, so if defined in a class, they allow accessing other objects from within the class.
There are two types of attributes:
- Instance-dependent: where attribute content is specific to each object, declared using the DATA statement.
- Instance-independent or static: where attribute content defines the state of the class and is valid for all class instances. Static attributes exist only once for the class, declared using the CLASS-DATA statement. They are accessible from the entire execution environment of the class. All objects of a class can access their static attributes. If a static attribute is changed in an object, the change is visible in all other objects of the class.
DATA connection_id TYPE /dmo/connection_id.
CLASS-DATA conn_counter TYPE i.
Methods
Methods are internal procedures of a class that define the behavior of an object. Methods can access all class attributes, allowing them to change the content of an object's attributes.
Methods also have an interface with parameters that allow them to receive values when invoked and return values after the call. Private attributes of a class can only be changed by methods of the same class.
The definition and interface of a method are similar to function module definitions. A method is defined in the declarative part of the class and implemented in the implementation part using the statements:
METHOD
...
ENDMETHOD.
Local data types and objects can be declared in methods in the same way as in any other ABAP procedure. Methods are called using the CALL METHOD statement.
There are three types of methods:
- Instance-dependent: declared using the METHODS statement, can access all class attributes and trigger all class events.
- Instance-independent or static: declared using the CLASS-METHODS statement, can only access static attributes and trigger static events.
- Special: apart from normal methods callable with the CALL METHOD statement, there are two special methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, automatically invoked when an object is created (CONSTRUCTOR) or when class components are first accessed (CLASS_CONSTRUCTOR).
METHOD class_constructor.
conn_counter_class = conn_counter_class + 1.
ENDMETHOD.
METHOD constructor.
IF i_carrier_id IS INITIAL OR i_connection_id IS INITIAL.
RAISE EXCEPTION TYPE cx_abap_invalid_value.
ENDIF.
me->carrier_id = i_carrier_id.
me->connection_id = i_connection_id.
conn_counter = conn_counter + 1.
ENDMETHOD.
Events
Events are occurrences or incidents used by objects or classes to trigger the execution of a method in another object or class. These methods are called event handler methods.
In a normal method call, the method can be called by any number of users. When an event is triggered, any number of these methods can be called.
The linkage between the event trigger and the event handler is not established in advance but in the runtime environment. In normal method calls, the calling program determines which methods to call; these methods must exist.
The event handler determines which events it needs to react to. There doesn't need to be a registered event handler method for every event. Class events can be triggered in the methods of the same class using the RAISE EVENT statement.
A method, whether from the same or a different class, is declared as an event handler method using the addition FOR EVENT OF.
Events have a parameter interface similar to methods but only have output parameters. Parameters are passed by the trigger (RAISE EVENT statement) to the event handler method, which receives them as input parameters.
The binding linkage between the trigger and the handler is dynamically established in the program using the SET HANDLER statement. The trigger and the handler can be objects or classes, depending on whether we have instance-dependent events or static events and event handler methods. When an event is triggered, the corresponding event handler method is executed in all classes registered for that handler.
There are two types of events:
- Instance-dependent: declared with the EVENTS statement, can only be triggered in an instance-dependent method.
- Instance-independent or static: declared with the CLASS-EVENTS statement, all methods can trigger static events. Static events are the only type of events that can be triggered by a static method.
Types and Constants
ABAP data types can be defined within a class using the TYPES statement. Data types are not specific to each instance and exist only once for all class objects.
Constants are a special type of static attributes. Their value is set when they are declared and cannot be changed. They are declared using the CONSTANTS statement. Constants exist only once for all class objects.
When defining data types within a class definition, the READ ONLY addition can be used. After writing the variable type just declared, this means that the public attribute declared with the DATA statement can be read from outside the class but can only be modified by methods of the same class. It's worth noting that the READ ONLY addition can only be used within the public part of a class declaration.
Class Visibility
The declarative or definition part of a class is divided into three areas of different visibility. These three areas define the external visibility of the class components, i.e., the interface between the class and the user.
Each class component must be assigned to one of these three sections:
- PUBLIC SECTION: all components declared in the public section are accessible to all users of the class and to all methods of the class and any class that inherits from it. Public components form the interface between the class and the user.
- PROTECTED SECTION: all components declared in the protected section are accessible to all methods of the class and to classes that inherit from it. Protected components form the interface between a class and all its subclasses.
- PRIVATE SECTION: components declared in the private section are only visible in the methods of the same class. Private components are not part of the external interface of the class.
CLASS zprueba_jega DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 149 Publicaciones Académicas
🎓Egresado de los módulos:
- Carrera Consultor en SAP Fiori
- Carrera Consultor ABAP Nivel Avanzado
- Carrera Consultor ABAP Nivel Inicial
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez