INTERFACES are like classes and act as data types for Objects.
Interfaces are independent structures that can extend the functionality of the class.
Interfaces can gets implemented in the same class where the functionality extension required.
Interfaces can declare globally or locally in ABAP programs.
All the components like attributes, methods, types etc can be declared in an Interface.
Interfaces contains methods without any implementation.
Interfaces extend the class functionality by adding their own components to public section.
Interface contains only declaration parts and won’t contain any implementation or executable statements.
In the class definition part, the declared interfaces gets listed.
All the operations in interfaces are defined in the class methods and it should present in the class implementation part.
The main use of interfaces is re-usability and maintain framework.
Interface references allow users to address and use different classes in the same way.
Interfaces with inheritance allows a single method within an interface to behave differently in different classes.
All the components of the class can also declared in the interface.
Defining Interfaces –
The local interface definition shown below –
INTERFACE <interface-name>.
...
<attributes-declaration>
<methods-declaration>
<events-declaration>
...
ENDINTERFACE
The interface definition contains the declaration for all components like attributes, methods, events of the interface.
Interfaces do not have any implementation part, since their methods are already implemented in the class.
Implementing Interfaces –
Interfaces do not have instances to implement like classes. Use the below statement to implement an interface –
INTERFACES interfce-name.
The above statement should be coded in the class declaration part.
During the interface implementation in a class, all the interface components are added in the public section.
The interface component can be addressed as a member of the class.
For example, assume component name is comp and interface name is intfce then the class component addressed as intfce~comp.
The class must implement all the interfaces methods implemented in it.
The interface method implementation part of the class is –
METHOD intfce~comp.
...
ENDMETHOD.
Interfaces can implement by different classes and these classes are extended by the same set of components. The interface methods can implemented differently in each class.
Example –
Simple example to implement interfaces in application program.
Code –
*&---------------------------------------------------------------------*
*& Report Z_INTERFACES
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_INTERFACES.
* Interface Definition
INTERFACE interfacenew.
* Declaring interface variables.
DATA: A TYPE I,B TYPE I.
* Declaring interface method.
METHODS methodnew.
ENDINTERFACE.
* classnew1 Definition
CLASS classnew1 DEFINITION.
PUBLIC SECTION.
* Interface definition in classnew1
INTERFACES interfacenew.
ENDCLASS.
* classnew1 implementation
CLASS classnew1 IMPLEMENTATION.
* methodnew implementation using interface in calssnew1
METHOD interfacenew~methodnew.
DATA result TYPE I.
Result = interfacenew~A + interfacenew~B.
WRITE:/ 'Addition Result is: ', result.
ENDMETHOD.
ENDCLASS.
* classnew2 Definition
CLASS classnew2 DEFINITION.
PUBLIC SECTION.
* Interface definition in classnew2
INTERFACES interfacenew.
ENDCLASS.
* classnew2 implementation
CLASS classnew2 IMPLEMENTATION.
* methodnew implementation using interface in calssnew2
METHOD interfacenew~methodnew.
DATA result TYPE I.
result = interfacenew~A * interfacenew~B.
WRITE:/ 'Multiplication Result is: ', result.
ENDMETHOD.
ENDCLASS.
* Creatng class object
START-OF-SELECTION.
* Declaring and creating object for classnew1
DATA: childobj1 TYPE REF TO classnew1.
CREATE OBJECT childobj1.
* Assigning value to interface variables of classnew1 object
childobj1->interfacenew~A = 10.
childobj1->interfacenew~B = 20.
WRITE /'Accessing methodnew through childclass1 object...'.
* Calling methodnew using classnew1 object
CALL METHOD: childobj1->interfacenew~methodnew.
ULINE.
* Declaring and creating object for classnew2
DATA: childobj2 TYPE REF TO classnew2.
CREATE OBJECT childobj2.
* Assigning value to interface variables of classnew2 object
childobj2->interfacenew~A = 10.
childobj2->interfacenew~B = 20.
WRITE /'Accessing methodnew through childclass2 object...'.
* Calling methodnew using classnew2 object
CALL METHOD: childobj2->interfacenew~methodnew.
ULINE.
Output –
Explaining Example –
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
interfacenew is the interface defined with fields A, B and method methodnew. classnew1 and classnew2 are the two classes defined with interface interfacenew. So these two classes can access interfacenew components.
classnew1 and classnew2 implemented the interface method methodnew differently in each class. So the outputs displays accordingly.