ABAP Keyword Documentation → ABAP - Reference → Declarative statemnts → 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 allow you to enhance the class-specific public points of contact 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, you define interfaces either globally in the repository or locally in an ABAP program.
Interface Components
You can define exactly the same components 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
. You can
modify some interface components to meet the requirements of the class. For example, you can identify methods as abstract or final, or you can assign
initial values to attributes.
When you implement an interface 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
.
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 interface method meth
:
METHOD intf~meth.
...
ENDMETHOD.
Instead of being specified by intf~meth
, the method can also be specified by an alias defined using ALIASES
.
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 allow you to use different classes 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
You use object references in object reference variables to access
objects. Instead of creating reference variables with reference to a
class, you can also create them 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
, you must first declare a reference
variable cref
with reference to the class. If the class class
implements an interface intf
, you can assign the class reference variable cref
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
, you can address the interface components as follows:
Using the class reference variable (not recommended)
- Accessing an attribute
attr
:cref->intf~attr
- Calling a method
meth
:[CALL METHOD] 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, you can only use the interface name to access constants.
- Accessing a constant
const
:intf=>const
For all other static components of an interface, you can only use object references or the class class
that implements the interface.
- Accessing a static attribute
attr
:class=>intf~attr
- Calling a static method
meth
:[CALL METHOD] class=>intf~meth