Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 36 ยท ABAP Basics

Modularization and Classes in ABAP

Structure ABAP code with subroutines, function modules, methods and global classes, then learn inheritance, interfaces, exceptions and ABAP Unit tests.

A report that grows to 2,000 lines in one block is hard to read and impossible to test. ABAP gives you several ways to split code into reusable units, from the old subroutines to function modules to the modern classes and methods. This lesson shows each form, explains why new code should use classes, and introduces exceptions and unit tests.

ABAP cannot run on this site, so listings are read-only. The runnable JavaScript blocks are stand-ins that mirror the object-oriented ideas.

Subroutines (legacy)

TEXT
PERFORM calc_tax USING lv_net CHANGING lv_tax.

FORM calc_tax USING pv_net TYPE p CHANGING pv_tax TYPE p.
  pv_tax = pv_net * '0.19'.
ENDFORM.

FORM/PERFORM are obsolete for new development (they are not allowed in ABAP Cloud). You will still meet them in old programs, so you must read them.

Function modules

A function module lives in a function group (SE37), has typed importing, exporting, changing and tables parameters, and can be called from any program or even remotely (RFC-enabled). SAP ships thousands, and BAPIs (Business Application Programming Interfaces) are stable, released function modules for business objects, such as BAPI_SALESORDER_CREATEFROMDAT2.

TEXT
CALL FUNCTION 'BAPI_MATERIAL_GET_DETAIL'
  EXPORTING
    material = lv_matnr
  IMPORTING
    material_general_data = ls_data
    return                = ls_return
  EXCEPTIONS
    OTHERS = 1.

After a BAPI call, always read the RETURN structure for messages (type E error, W warning, S success), and only then BAPI_TRANSACTION_COMMIT to save the change.

Classes and methods

Object-oriented ABAP is the modern standard.

TEXT
CLASS zcl_tax_calculator DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    METHODS constructor IMPORTING iv_rate TYPE p.
    METHODS tax_for IMPORTING iv_net TYPE p
                    RETURNING VALUE(rv_tax) TYPE p.
  PRIVATE SECTION.
    DATA mv_rate TYPE p LENGTH 5 DECIMALS 2.
ENDCLASS.

CLASS zcl_tax_calculator IMPLEMENTATION.
  METHOD constructor.
    mv_rate = iv_rate.
  ENDMETHOD.
  METHOD tax_for.
    rv_tax = iv_net * mv_rate / 100.
  ENDMETHOD.
ENDCLASS.

DATA(lo_calc) = NEW zcl_tax_calculator( 19 ).
DATA(lv_tax)  = lo_calc->tax_for( 100 ).

A class has a definition (the interface to the outside) and an implementation. Visibility sections are PUBLIC, PROTECTED and PRIVATE. NEW creates an instance, -> calls an instance method and => calls a static method. Global classes are created in SE24 or ADT and transported like other objects (see Transports and the System Landscape).

JS Stand-in: the tax class in JavaScript

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Inheritance and interfaces

TEXT
CLASS zcl_reduced_tax DEFINITION INHERITING FROM zcl_tax_calculator.
  PUBLIC SECTION.
    METHODS tax_for REDEFINITION.
ENDCLASS.

INHERITING FROM creates a subclass; REDEFINITION replaces behavior. An interface (INTERFACE zif_price_rule PUBLIC) declares methods without code, and classes INTERFACES zif_price_rule. Interfaces are favored over inheritance because they let tests substitute fake implementations, and SAP's own frameworks (BAdIs, for instance) are built on them.

JS Stand-in: one interface, two implementations

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Class-based exceptions

TEXT
TRY.
    DATA(lv_ratio) = lv_a / lv_b.
  CATCH cx_sy_zerodivide INTO DATA(lx_err).
    WRITE / lx_err->get_text( ).
ENDTRY.

Your own exceptions inherit from cx_static_check, cx_dynamic_check or cx_no_check. Raise them with RAISE EXCEPTION TYPE zcx_order_error. Class-based exceptions replaced the old sy-subrc convention in new code.

ABAP Unit

TEXT
CLASS ltcl_tax_test DEFINITION FINAL FOR TESTING
  DURATION SHORT RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    METHODS nineteen_percent FOR TESTING.
ENDCLASS.

CLASS ltcl_tax_test IMPLEMENTATION.
  METHOD nineteen_percent.
    cl_abap_unit_assert=>assert_equals(
      act = NEW zcl_tax_calculator( 19 )->tax_for( 100 )
      exp = 19 ).
  ENDMETHOD.
ENDCLASS.

Run with Ctrl+Shift+F10 in SE80 or Ctrl+Shift+F10 in ADT. Tests live in a local test class in the same include, and should be quick and independent.

JS Stand-in: a tiny unit test helper

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Enhancements: changing SAP without modifying it

Customers should not edit SAP standard code. SAP offers enhancement points: user exits, BAdIs (Business Add-Ins, interface-based) and implicit enhancements. You implement a BAdI in SE19 and activate it. In S/4HANA and the clean core approach, extensions are kept outside the core (side-by-side on SAP BTP) or limited to released, stable extension points, which you will see in S/4HANA: What Changed and Why.

Common mistakes

  • Writing new FORM routines instead of methods.
  • Using CALL FUNCTION for BAPIs but never reading RETURN.
  • Putting all logic in the report's event blocks, so it cannot be tested.
  • Overusing static methods and global state.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which unit is recommended for new ABAP code?

    Classes and methods are the modern standard and the only option in ABAP Cloud.
  2. 2. What should you check after calling a BAPI?

    RETURN holds success, warning and error messages. Commit only if there is no error.
  3. 3. How do you create an instance of a class?

    NEW (and the older CREATE OBJECT) instantiate a class.
  4. 4. What do BAdIs let you do?

    BAdIs are interface-based enhancement spots that avoid modifications of standard code.

Summary

  • Subroutines are legacy; function modules and BAPIs are for reuse and remote access.
  • Classes with methods, interfaces and exceptions are the modern way to structure ABAP.
  • ABAP Unit tests keep logic correct; design with interfaces so you can test.
  • Use BAdIs and clean-core extensions instead of modifying standard code.

Next: S/4HANA: What Changed and Why