Example Program
Here is an example of interface and a class implementing that interface.
* Creating interface
INTERFACE my_interface1
* declaring data variable
DATA: num1 TYPE i,
num2 TYPE i,
res TYPE i.
* Declaring abstract methods
METHOD : add.
METHOD: subtract.
ENDINTERFACE
* Creating class
CLASS my_class1 DEFINATION.
* Declaring public section because interface can implement only in the public section
public SECTION.
* Using interface inside class and assigning value of data attributes of interface
INTERFACEs my_interface1 DATA VALUES num1 = 5 , num2= 10.
ENDCLASS
* Class implementing Interface
CLASS my_class1 IMPLEMENTATION.
* Using method inside implementing class
METHOD my_interface1~add.
* adding two number
my_interface1~res =my_interface1~ num1 + my_interface1~num2.
* printing result
write: my_interface1~res.
ENDMETHOD
METHOD my_interface1~subtract.
* subtracting two number
my_interface1~res =my_interface1~ num2 - my_interface1~num1.
* printing result
write: my_interface1~res.
ENDMETHOD
ENDCLASS.
Output
15
10
SAP ABAP | Interfaces
ABAP(Advanced Business Application Programming) is an object-oriented programming language that supports many oops concepts like other programming languages. It supports all the four pillars of oops i.e. Inheritance, Polymorphism, Abstraction, and Encapsulation. The interface is one of the oops concepts that ABAP supports and it plays a very important role in implementing all these oops concepts.
Table of Content
- What is Interface in SAP ABAP
- Syntax for Creating an Interface in SAP ABAP
- Implementation of Interface Inside Class in SAP ABAP
- Example Program
- Program Execution – Creating Objects ‘object1’ and ‘object2’
- Benefits and Application of Interface in SAP ABAP