Why OOP in ABAP?
Object-oriented ABAP (ABAP OO) is now the recommended programming model by SAP. All new SAP frameworks ÔÇö RAP, BTP, Clean ABAP ÔÇö are built on OO principles. Understanding OOP is essential for modern ABAP development.
Defining a Class
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING iv_name TYPE string,
get_name RETURNING VALUE(rv_name) TYPE string.
PRIVATE SECTION.
DATA: mv_name TYPE string.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD constructor.
mv_name = iv_name.
ENDMETHOD.
METHOD get_name.
rv_name = mv_name.
ENDMETHOD.
ENDCLASS.
Interfaces
Interfaces define a contract that classes must implement. They enable polymorphism and loose coupling:
INTERFACE zif_printable.
METHODS: print.
ENDINTERFACE.
CLASS zcl_report DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_printable.
ENDCLASS.
Inheritance
ABAP supports single inheritance. Use it to build hierarchies of related classes:
CLASS zcl_manager DEFINITION INHERITING FROM zcl_employee.
PUBLIC SECTION.
METHODS: get_team_size RETURNING VALUE(rv_size) TYPE i.
ENDCLASS.
Key Design Patterns
Singleton Pattern
Ensures only one instance of a class exists. Useful for configuration managers and utility classes.
Factory Pattern
Creates objects without exposing creation logic. Essential for flexible, testable code.
MVC Pattern
Separates model (data), view (UI), and controller (logic). Used extensively in ALV reports and Web Dynpro.