Open In App

Abstract Class Vs Interface in SAP ABAP

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn the difference between an Abstract Class and an Interface in SAP ABAP. It is created by SAP which is a domain-specific language, it is 4th generation programming language. It is also called ABAP/4(Fourth Generation Language” or 4GL).

Abstract-Class--Vs--Interface-in-SAP-ABAP

Abstract Class Vs Interface in SAP ABAP

Abstract Class in SAP ABAP

Abstract class in SAP ABAP is a special type of class, which can not be instantiated, which means we can not create objects of that class. it needs to be subclassed by another class to use its properties. An abstract class can include one or more abstract methods, which are method declarations without an implementation. When a class inherits from an abstract class, it is required to provide implementations for all the abstract methods declared in the abstract class.

Example:

CLASS abstract_class DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: abstract_method ABSTRACT.
ENDCLASS.

CLASS abstract_class IMPLEMENTATION.
METHOD abstract_method.
* Implementation of the abstract method goes here
ENDMETHOD.
ENDCLASS.


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.

Example:

CLASS Your_Class DEFINITION.
PUBLIC SECTION.
INTERFACES: Your_Interface.
ENDCLASS.

CLASS Your_Class IMPLEMENTATION.
METHOD Your_Interface~method1.
" Implementation for method1
ENDMETHOD.

METHOD Your_Interface~method2.
" Implementation for method2
ENDMETHOD.
" ... Implement other methods from the interface
ENDCLASS.


Difference between Interface and Abstract class in SAP ABAP:

Here we are providing you a list of difference between Abstract class and Interface in SAP ABAP:

Interface

Abstract Class

Method declaration

If we declare a new method in SAP ABAP interface, then it is mandatory for all the implementing class to implement that method.

In SAP ABAP abstract class if you declare a non abstract method then it is not mandatory to inherit that method for all class.

Method Implementation

In SAP ABAP Interface, we can only declare class we can’t write instructions inside that method.

In SAP ABAP abstract method, we can have default method and ,

Access Modifier

By default, all the elements are PUBLIC in interface.

In SAP ABAP abstract class, we can set the access modifier to each element according to need.

Multiple inheritance

By using Interface in SAP ABAP, we can achieve multiple inheritance.

While, Abstract classic in SAP ABAP does not support multiple inheritance, as Only one abstract class may be used as the Super class because ABAP does not support multiple Super classes.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads