ABAP Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Classes and Interfaces → ABAP Objects - Overview
Interfaces
The components of a class are divided into visibility sections, and this forms the external point of contact between the class and its user. For example, the public components of a class define its public scope, since all of its attributes and method parameters can be addressed by all users. The protected components form an interface between the class and those classes that inherit from it (subclasses).
Interfaces are independent structures that enable the class-specific public points of contact to be enhanced by implementing them in classes. Different classes that implement the same interface can all be addressed in the same way. Alongside inheritance, interfaces provide one of the pillars of polymorphism, since they allow a single method within an interface to behave differently in different classes. Interface reference variables allow users to address different classes in the same manner. Interfaces can also be nested.
Other versions: 7.31 | 7.40 | 7.54
Defining Interfaces
Like classes, interfaces can be defined either globally in the repository or locally in an ABAP program.
Interface Components
Exactly the same components can be defined in an interface as in a class.
Implementing Interfaces
Unlike classes, interfaces do not have instances. Instead, interfaces are implemented by classes. The
INTERFACES
statement in the
declaration part of a class is used to implement interfaces in a class. This statement may only appear
in the public section of the class, that is, after PUBLIC SECTION
. Some interface components can be modified to meet the requirements of the class. For example, methods can be identified as abstract or final, or
initial values can be assigned to attributes.
When an interface is implemented in a class, the components of the interface are added to the other
components in the public section. A component comp
of an interface
intf can be addressed as though it were a member of the class under the name intf~comp
.
For use outside and inside the classes, ALIASES
can be used to define alias names for these components.
The class must implement the methods of all interfaces implemented in it. The implementation part of
the class must contain a method implementation for each non-abstract and non-optional interface method meth
:
METHOD intf~meth.
...
ENDMETHOD.
Interface methods defined as optional using the addition DEFAULT do not need to be implemented explicitly and demonstrate default behavior when called.
Interfaces can be implemented by different classes. Each of these classes is extended by the same set of components. However, the methods of the interface can be implemented differently in each class.
Interfaces enable different classes to be used in a uniform way using interface reference variables (polymorphism). Interfaces that are implemented in different classes extend the public scope of each class by the same set of components. If a class does not have any class-specific public components, the interfaces define the entire public face of the class.
Interface Reference Variables
Object references are used in object reference variables to address
objects. Instead of creating reference variables with reference to a
class, they can also be created with reference to an interface.
This kind of reference variable can contain references to objects of classes that implement the corresponding
interface. A reference variable obj
with reference to an interface
intf is declared with the statement DATA
obj TYPE REF TO intf. This reference variable allows programs access to precisely those components
defined in the interface, that is, the components of an object that have been added to the class-specific components by implementing the interface.
Accessing Objects Using Interface References
To generate an object of the class class
, a reference variable cref
must first be created with reference to the class. If the class class
implements
an interface intf
, the class reference variable cref
can be assigned to the interface reference variable iref
as follows:
iref = cref
.
The reference in iref
now points to the same object as the reference in cref
.
It is also possible to directly generate an object to which the interface reference variable points
initially. In this case, the TYPE
addition of the statement CREATE OBJECT
must be used to specify a class that implements the interface.
CREATE OBJECT iref TYPE class
.
If the interface intf
contains an instance attribute attr
and an instance method meth
, the interface components can be addressed as follows:
Using the class reference variable (not recommended)
- Accessing an attribute
attr
:cref->intf~attr
- Calling a method
meth
:cref->intf~meth
Using the interface reference variable (recommended):
- Accessing an attribute
attr
:iref->attr
- Calling a method
meth
:[CALL METHOD] iref->meth
Accessing the Static Components of Interfaces
As far as the static components of interfaces are concerned, the interface name can only be used to access constants.
- Accessing a constant
const
:intf=>const
For all other static components of an interface, only object references or the class class
that implements the interface can be used.
- Accessing a static attribute
attr
:class=>intf~attr
- Calling a static method
meth
:[CALL METHOD] class=>intf~meth