INTERFACE

Basic form

INTERFACE ifac.

Extras:

1. ... DEFERRED
2. ... LOAD
3. ... SUPPORTING REMOTE INVOCATION

Effect

Defines aninterface in ABAP Objects. The INTERFACE statement introduces a statement block, which you close with the ENDINTERFACE statement.

To define an interface in an ABAP program, use a statement block x:

INTERFACE intf.

...

ENDINTERFACE.

This statement block belongs to the global declarations in an ABAP program. Interfaces cannot be defined in procedures (subroutines, function modules, or methods). At present, you cannot nest interface definitions. You should define the interface at the start of each program, just as you would for any data or class declaration.

The interface components are declared in the statement block. These components are the same as class components and are declared as in an class definition:

Instance components:

DATA
for instance attributes
METHODS
for instance methods
EVENTS
for instance events
TYPES
for internal class types

Static components:

CLASS-DATA
for static attributes
CLASS-METHODS
for static methods
CLASS-EVENTS
for static events
CONSTANTS
for constants (that is, constant static attributes)

Interfaces:

INTERFACES
for nesting interfaces
ALIASES
for aliases for components of interfaces

Unlike classes, you cannot use the VALUE addition when you define interface attributes using the DATA statement. Note that the INTERFACES statement is used to nest and compound interfaces, not to implement them.

Interfaces do not have a visibility area. When you implement an interface in a class using the INTERFACES statement, the components of this interface extend the class-specific components of the public visibility area of the class.

&ABAP_EXAMPLE&

interface i_bike.
   methods drive.
endinterface.

class c_bicycle definition.
  public section.
    interfaces i_bike.
  private section.
    data: speed type i.
endclass.

class c_motorcycle definition.
  public section.
    data ignition value ' '.
    interfaces i_bike.
  private section.
    data: speed type i.
endclass.

data: mybike1 type ref to c_bicycle,
      mybike2 type ref to c_bicycle,
      mymotor type ref to c_motorcycle.

data:  if_bike type ref to i_bike,
       if_bike_tab like table of if_bike.

create object: mybike1, mybike2, mymotor.

if_bike = mybike1. append if_bike to if_bike_tab.
if_bike = mybike2. append if_bike to if_bike_tab.
if_bike = mymotor. append if_bike to if_bike_tab.

mymotor->ignition = 'X'.

loop at if_bike_tab into if_bike.
  if_bike->drive( ).
endloop.

class c_bicycle implementation.
  method i_bike~drive.
    speed = speed  + 10.
  endmethod.
endclass.

class c_motorcycle implementation.
  method i_bike~drive.
    if ignition = 'X'.
      speed = speed + 100.
    endif.
  endmethod.
endclass.

In this example, the interface I_BIKE is defined with the DRIVE method. The example shows how two different classes implement the interface and how objects in the class can be accessed using reference variables that are typed with reference to the interface.

Addition 1

... DEFERRED

Effect

In general, you define an interface in a context and then use it. By context, we mean the common locality of several classes and interfaces - for example, several local interfaces in a program. You may need to use an interface in a context before you have defined it - for example, when two interfaces refer to each other reciprocally. If so, you must make the interface known using the INTERFACE intf DEFERRED variant. There is no associatedDS:ABAP.ENDINTERFACE>ENDINTERFACE statement for this variant. The interface itself must be defined later in the same context.

Note

The INTERFACE intf DEFERRED variant shows only that the intf interface occurs in the same context so that the system does not search for it in higher context. You cannot however access components of the interface, such as events in the definition of an event handler method.

&ABAP_EXAMPLE&

INTERFACE I2 DEFERRED.

INTERFACE I1 .
     DATA A2 TYPE REF TO I2.
ENDINTERFACE.

INTERFACE I2.
     DATA A1 TYPE REF TO I1.
ENDINTERFACE.

In this example, a reference variable A2 is declared with reference to the interface I2 even though I2 is defined after I1. When the program is generated, the system knows that I2 is an interface in the same context because of the INTERFACE I2 DEFERRED statement. Otherwise, it would search for I2 in a superior context, such as the class library.

&ABAP_ADDITION_2&

... LOAD

&ABAP_EFFECT&

In general, the compiler automatically loads the description of a globalen interface from the class library when the interface is used for the first time in a program - particularly if the interface is used for the first time to type reference variables. However, if the description of a global interface is only need then a static component is accessed, or when an event handler method is defined, the interface description must be loaded explicitly using the INTERFACE intf LOAD (for technical reasons). In such cases, there is no associatedENDINTERFACE statement.

&ABAP_EXAMPLE&
INTERFACE i_docu_1 LOAD.

CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: m1 FOR EVENT ce1 OF i_docu_1.
ENDCLASS.

DATA: cref1 TYPE REF TO c1.

CREATE OBJECT cref1.

SET HANDLER: cref1->m1.

CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE / 'M1 handling event'.
ENDMETHOD.
ENDCLASS.

In this example, an event handler method m1 is declared in the local class C1 for the event ce1 of the global interface I_DOCU_1. The INTERFACE I_DOCU_1 LOAD statement loads the I_DOCU_1 interface from the class library.

&ABAP_ADDITION_3&

... SUPPORTING REMOTE INVOCATION

&ABAP_EFFECT&

The instance methods of the interfaces can be called remotely. The remote call operator is used for remote calls.

Related

CLASS

PUBLIC SECTION

PROTECTED SECTION

PRIVATE SECTION

ENDCLASS

CLASS-DATA

CLASS-METHODS

CLASS-EVENTS

METHODS

EVENTS

INTERFACES

ALIASES

METHOD

ENDMETHOD

INTERFACE

ENDINTERFACE

CREATE OBJECT

CALL METHOD

RAISE EVENT

SET HANDLER

Special variants of other keywords

Additional help

Interfaces