![](/cvo_files/imgs_sap_abap/logos/logo_cvo_academy_it.png)
✒️ABAP Las Interfaces
ABAP Las Interfaces
![ABAP Las Interfaces ABAP Las Interfaces](https://www.cvosoft.com/cvo_files/imgs_sap_abap/og-images/8.jpg)
Interfaces
Interfaces are standalone structures that can be implemented within a class to extend its scope. A class's specific scope is defined by its components and visibility sections. Protected components of a class define its scope concerning its subclasses. Interfaces expand a class's scope by adding their own components to the public section. This facilitates users accessing different classes through a common contact point.
Interfaces, coupled with inheritance, provide a fundamental pillar of polymorphism. They enable a single method with an interface to behave differently in different classes. Interfaces allow for the uniform use of different classes by leveraging references to interfaces, i.e., polymorphism. For example, interfaces implemented in different classes expand the public scope of the class with the same set of components. If the class doesn't have any specific public components of its own, then the interface completely describes the class's public scope.
Defining an Interface
Interfaces can be defined either globally in the R/3 Repository or locally in an ABAP program. The definition of a local interface consists of the code between the following statements:
INTERFACE <INTERFACE_NAME>.
...
ENDINTERFACE.
This definition contains the declaration of all components (attributes, methods, and events) of the interface. The same components can be defined in an interface as in a class.
Implementing Interfaces
When implementing an interface in a class, the interface's components are added to the rest of the components in the public section. A component of an interface can be addressed as if it were a member of the class under the following name:
<INTERFACE-COMPONENT>.
The class must implement the methods of all interfaces implemented in it. The implementation part of the class must contain the implementation of each method of the interface:
METHOD <INTERFACE-METHOD>.
...
ENDMETHOD.
References to Interfaces
Referenced variables allow access to objects. Instead of creating referenced variables with reference to a class, they can be created with reference to an interface.
To define a reference to an interface, the following addition is used:
TYPE REF TO <INTERFACE>.
A referenced variable with the referenced type to an interface is called a referenced variable to an interface or simply an interface reference.
An interface reference allows the user to use the following declaration:
<INTERFACE_REFERENCE>-><COMPONENT>.
Directing and Assigning Objects Using Interface References
First, we must declare a reference variable to the class to create an object of the class.
If the class implements an interface, the following assignment between the referenced variable to the class and an interface reference can be used to make the interface reference point to the same object as the class reference:
<INTERFACE_REFERENCE> = <CLASS_REFERENCE>.
Using the referenced variable to a class
To access an attribute, we'll use:
<CLASS_REFERENCE>-><INTERFACE-ATTRIBUTE>.
To access the method, we'll use:
CALL METHOD <CLASS_REFERENCE>-><INTERFACE-METHOD>.
Using the referenced variable to an interface
To access an attribute, we'll use:
<INTERFACE_REFERENCE>-><ATTRIBUTE>.
To access the method, we'll use:
CALL METHOD <INTERFACE_REFERENCE>-><METHOD>.
Whenever static components of interfaces are involved, only the interface name can be used to access constants:
To access a constant, we'll use:
<INTERFACE>=><CONSTANT>.
For all other static components of an interface, only object references or the class implementing the interface can be used:
To access a static attribute, we'll use:
<CLASS>=><INTERFACE-ATTRIBUTE>.
To call a static method, we'll use:
CALL METHOD <CLASS>=><INTF-METH>.
E.g
*&---------------------------------------------------------------------*
*& Report ZPRUEBA_INTERFACES
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zprueba_interfaces.
*----------------------------------------------------------------------*
* INTERFACE estado
*----------------------------------------------------------------------*
*
* Definición de la interfaz "estado" con un método para imprimir.
*----------------------------------------------------------------------*
INTERFACE estado.
METHODS imprimir.
ENDINTERFACE. "estado
*----------------------------------------------------------------------*
* CLASS contador DEFINITION
*----------------------------------------------------------------------*
*
* Definición de la clase "contador" que implementa la interfaz "estado".
*----------------------------------------------------------------------*
CLASS contador DEFINITION.
PUBLIC SECTION.
INTERFACES estado. " Implementa la interfaz estado.
METHODS incrementar.
PRIVATE SECTION.
DATA cuenta TYPE i.
ENDCLASS. "contador DEFINITION
*----------------------------------------------------------------------*
* CLASS contador IMPLEMENTATION
*----------------------------------------------------------------------*
*
* Implementación de la clase "contador" que incrementa la cuenta.
*----------------------------------------------------------------------*
CLASS contador IMPLEMENTATION.
METHOD estado~imprimir.
WRITE: / 'La cuenta en el contador es: ', cuenta.
ENDMETHOD. "estado~imprimir
METHOD incrementar.
ADD 1 TO cuenta.
ENDMETHOD. "incrementar
ENDCLASS. "contador IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS bicicleta DEFINITION
*----------------------------------------------------------------------*
*
* Definición de la clase "bicicleta" que implementa la interfaz "estado".
*----------------------------------------------------------------------*
CLASS bicicleta DEFINITION.
PUBLIC SECTION.
INTERFACES estado. " Implementa la interfaz estado.
METHODS manejar.
PRIVATE SECTION.
DATA velocidad TYPE i.
ENDCLASS. "bicicleta DEFINITION
*----------------------------------------------------------------------*
* CLASS bicicleta IMPLEMENTATION
*----------------------------------------------------------------------*
*
* Implementación de la clase "bicicleta" que maneja la velocidad.
*----------------------------------------------------------------------*
CLASS bicicleta IMPLEMENTATION.
METHOD estado~imprimir.
WRITE: / 'La velocidad de la bicicleta es: ', velocidad.
ENDMETHOD. "estado~imprimir
METHOD manejar.
ADD 10 TO velocidad.
ENDMETHOD. "manejar
ENDCLASS. "bicicleta IMPLEMENTATION
DATA: cuenta TYPE REF TO contador,
bici TYPE REF TO bicicleta,
estado TYPE REF TO estado,
ti_estado TYPE TABLE OF REF TO estado.
START-OF-SELECTION.
CREATE OBJECT: cuenta, bici.
DO 5 TIMES.
CALL METHOD: cuenta->incrementar,
bici->manejar.
ENDDO.
APPEND: cuenta TO ti_estado,
bici TO ti_estado.
LOOP AT ti_estado INTO estado.
CALL METHOD estado->imprimir.
ENDLOOP.
 
 
 
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
🎓Cursando Actualmente: Consultor en SAP Fiori
🎓Egresado de los módulos:
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