In modern SAP development, Object-Oriented Programming (OOP) in ABAP has become the standard approach for building scalable, modular, and maintainable applications. Since the introduction of ABAP Objects in SAP NetWeaver, leveraging ABAP Classes has empowered developers to adopt proven software design principles within the SAP environment.
This article provides an in-depth exploration of ABAP Classes and OOP concepts, highlighting how they transform traditional procedural ABAP programming into a more robust, flexible paradigm.
ABAP Objects is the extension of the ABAP language that supports object-oriented programming concepts such as:
These features enable developers to write reusable, extensible code that better models real-world business scenarios.
Classes can be defined in the ABAP Workbench (Transaction SE24) or via the ABAP Editor (SE80).
CLASS lcl_example DEFINITION.
PUBLIC SECTION.
METHODS: constructor,
display_message.
PRIVATE SECTION.
DATA: message TYPE string.
ENDCLASS.
CLASS lcl_example IMPLEMENTATION.
METHOD constructor.
message = 'Hello from ABAP OOP!'.
ENDMETHOD.
METHOD display_message.
WRITE: / message.
ENDMETHOD.
ENDCLASS.
DATA: obj TYPE REF TO lcl_example.
CREATE OBJECT obj.
obj->display_message( ).
Allows a class (subclass) to inherit attributes and methods from another class (superclass), promoting code reuse.
CLASS lcl_parent DEFINITION.
PUBLIC SECTION.
METHODS: greet.
ENDCLASS.
CLASS lcl_parent IMPLEMENTATION.
METHOD greet.
WRITE 'Hello from Parent'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.
PUBLIC SECTION.
METHODS: greet REDEFINITION.
ENDCLASS.
CLASS lcl_child IMPLEMENTATION.
METHOD greet.
WRITE 'Hello from Child'.
ENDMETHOD.
ENDCLASS.
Polymorphism allows method calls to execute different behaviors based on the object's actual class at runtime.
DATA: obj TYPE REF TO lcl_parent.
CREATE OBJECT obj TYPE lcl_child.
obj->greet( ). " Outputs: Hello from Child
Define method signatures without implementation. Classes implementing the interface must provide the implementation.
INTERFACE lif_example.
METHODS: display.
ENDINTERFACE.
CLASS lcl_impl DEFINITION.
PUBLIC SECTION.
INTERFACES lif_example.
ENDCLASS.
CLASS lcl_impl IMPLEMENTATION.
METHOD lif_example~display.
WRITE 'Interface Method Implementation'.
ENDMETHOD.
ENDCLASS.
Cannot be instantiated directly; designed to be a base class with abstract methods forcing subclasses to implement specific behavior.
CLASS lcl_abstract DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: do_something ABSTRACT.
ENDCLASS.
Example of raising and catching an exception:
CLASS lcx_error DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
TRY.
RAISE EXCEPTION TYPE lcx_error.
CATCH lcx_error INTO DATA(lx_error).
WRITE 'Exception caught'.
ENDTRY.
ABAP Classes and Object-Oriented Programming bring modern software engineering principles to SAP development. Mastering OOP enables ABAP developers to write cleaner, more efficient, and maintainable code, which is crucial as SAP landscapes grow in complexity.
For new ABAP developers, embracing OOP concepts early accelerates learning and aligns with SAP’s evolving technology stack, including SAP S/4HANA and SAP Cloud Platform.