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.
What is Interface in SAP ABAP
The interface in SAP ABAP is different from the class, it can not have any implementation like the class. It defines a set of method declarations that a class must implement without providing any implementation detail of that method. Interface helps in achieving multiple inheritance. Multiple inheritance can be defined as a class can inherit multiple interfaces. Due to Inheritance interface provides a base for polymorphism because the method declared in the interface behaves differently in different classes. Like class Interface can be defined locally or globally in the ABAP programming language.
Syntax for Creating an Interface in SAP ABAP
Syntax
INTERFACE interface-name
DATA declaration.....
ABSTRACT METHOD declaration.....
EVENT declaration....
.
.
ENDINTERFACE
Syntax of interface explained: Here INTERFACE is a keyword for declaring interface and interface-name is the name of interface. DATA declaration is declaring the data. METHOD declaration is declaring the method .It is important to note that all the methods declared in interface are Abstract. You can also declare many field like EVENT, CONSTANT etc. At the end of interface we declare ENDINTERFACE.
Implementation of Interface Inside Class in SAP ABAP
INTERFACE interface_name
Explanation: First we create a class inside that we use above syntax to implement interface. here interface_name means name of interface.
Implementing method of an interface inside class in SAP ABAP
METHOD interface_name~interface_method_name
satements
ENDMETHOD
Explanation: we use METHOD keyword to implement method of interface inside implementing class, interface_name~interface_method_name refers to the name of interface along with method name declare in interface.
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
Program Execution - Creating Objects 'object1' and 'object2'
Creating object 1 and calling add method
*creating object
start-OF-SELECTION.
data : object1 TYPE REF TO my_class1.
create OBJECT object1.
WRITE : 'Addition of given number is'.
call METHOD object1->my_interface1~add.
Output
Addition of given number is 15
Here we have created an object object1 of class my_class1 and call the add method of interface my_interface1 implemented in my_class1. After calling the output will be 15.
Creating object2 and calling subtract method
*creating object2
start-OF-SELECTION.
data : object2 TYPE REF TO my_class1.
create OBJECT object2.
WRITE : 'Subtraction of given number is'.
call METHOD object1->my_interface1~subtract.
Output
Subtraction of given number is 10
Here we have created an object object2 of class my_class1 and call the subtract method of interface my_interface1 implemented in my_class1. After calling the output will be 10.
Benefits and Application of Interface in SAP ABAP
- Same Method but Different Functionality: The method declare in interface can have different functionality when use in different class this is because we can not implement method in interface we can only declare that method. class implement or use that function according to their need. It also group common functionality which makes a program more readable.
- Multiple Inheritance: Inheritance support multiple inheritance, which means two or more class have same methods name but different functionality. A class can implement multiple interfaces, inherit the method declare in the interface whereas a class support only single inheritance.
- Polymorphism: It is a concept of oops, polymorphism is derived from a geek word poly means many and morph means form. The main purpose of inheritance is to achieve run time polymorphism. Inheritance provide a base for polymorphism as a single method declare in interface can be used by different class with different functionality.
Similar Reads
SAP Advanced Business Application Programming (ABAP)
SAP ABAP stands for Advanced Business Application Programming. SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which i
6 min read
What is SAP ABAP: A Brief Overview
SAP ABAP (Advanced Business Application Programming) is a high-level programming language created by the German software company SAP SE. ABAP is primarily used for developing and customizing applications within the SAP ecosystem, which includes enterprise resource planning (ERP) systems and other bu
9 min read
SAP ABAP | Basic Syntax & Statements
The German software company SAP created the high-level programming language, ABAP (Advanced Business Application Programming) primarily, this language serves as a tool for developing applications within the SAP R/3 system. Designed with simplicity and ease of learning in mind, ABAP syntax allows eff
9 min read
SAP ABAP | Understanding Variables
What is a Variable?Variable are named data objects that refer to the memory location allocated during the program execution for processing. As the name indicates, users can use refer to the range of values that may be stored inside and the range of actions that can be carried out on the variable. Sy
7 min read
SAP ABAP Keywords
The core of many global enterprises, SAP ABAP stands for Advanced Business Application Programming. It is the heart and soul of SAP systems. It gives users the ability to expand and modify SAP apps to satisfy particular business needs. The fundamental building blocks of SAP ABAP are its keywords, wh
5 min read
SAP ABAP | Constants & Literals Explained
In the world of SAP ABAP (Advanced Business Application Programming), the use of Constants and Literals is necessary for the effective handling of data. Literals are used to denote specific data types such as numbers, characters, strings, and boolean values. What are Literals?Literals in programming
7 min read
SAP ABAP | Data Types
Before Understanding the Data type first understand the Data object. Data objects are variables that we declare in the program. It occupies some memory where you can store the data from external sources. Data can be of different types, so data types are responsible for defining the type of data of t
6 min read
Relational operators in SAP ABAP
Relational operators in SAP ABAP are symbols or combinations of symbols that compare values and return a Boolean result (either true or false). These operators allow developers to establish relationships between variables, constants, or expressions, facilitating decision-making processes in the prog
6 min read
Operators in SAP ABAP
High-level programming languages like SAP ABAP (Advanced Business Application Programming) are used to create apps in the SAP environment. Operators are essential for the execution of many different operations in SAP ABAP, ranging from straightforward arithmetic computations to complex logical analy
7 min read