ABAPintermediate
Object-Oriented ABAP (OO ABAP)
Classes, interfaces, inheritance, and polymorphism as implemented in the ABAP Objects paradigm.
Modern ABAP frameworks — RAP, the ALV object model, BOPF, most new BAdIs — are all class-based. Procedural ABAP still runs, but new code is written with classes.
A class is a blueprint for a house; an interface is a building code contract (must have working plumbing) that any house design can promise to follow regardless of its actual blueprint.
Key Concepts
1
The shape of a class. Definition and implementation are separate:
2
CLASS lcl_invoice DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING iv_id TYPE string.
METHODS post RETURNING VALUE(rv_doc) TYPE string.
PRIVATE SECTION.
DATA mv_id TYPE string.
ENDCLASS.3
CLASS lcl_invoice IMPLEMENTATION.
METHOD constructor.
mv_id = iv_id.
ENDMETHOD.
METHOD post.
rv_doc = |DOC{ mv_id }|.
ENDMETHOD.
ENDCLASS.4
PUBLIC is callable by anyone, PROTECTED by subclasses, PRIVATE only inside the class. CLASS-DATA
and CLASS-METHODS belong to the class rather than to an object.
PUBLICPROTECTEDPRIVATECLASS-DATACLASS-METHODS
5
An interface is a contract. It lists methods with no implementation:
6
INTERFACE lif_output.
METHODS render IMPORTING it_data TYPE ty_t_data.
ENDINTERFACE.7
Several unrelated classes can implement it, and code written against the interface works with all of them:
8
DATA(lo_out) = COND #( WHEN iv_mode = 'PDF' THEN NEW lcl_pdf( )
WHEN iv_mode = 'EXCEL' THEN NEW lcl_excel( ) ).
lo_out->render( lt_data ).9
Adding a third output format means a new class, not an edit to this code. That is polymorphism, and it is
the practical reason to use interfaces.
10
Single inheritance only. A class extends one superclass. For behaviour from several sources, implement
several interfaces — the same rule as Java.
11
CLASS lcl_credit_note DEFINITION INHERITING FROM lcl_invoice.
PUBLIC SECTION.
METHODS post REDEFINITION.
ENDCLASS.12
REDEFINITION declares that the subclass replaces the method. Inside it, super->post( ) calls the
original, and me-> refers to the current object.
REDEFINITIONsuper->post( )me->
13
An abstract class cannot be instantiated and may leave methods abstract for subclasses to fill in.
14
Exceptions replace sy-subrc. Class-based exceptions inherit from CX_ROOT:
sy-subrcCX_ROOT
15
TRY.
lo_invoice->post( ).
CATCH cx_posting_failed INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE 'E'.
CLEANUP.
" runs if an exception leaves this block
ENDTRY.16
The difference that matters: an unhandled exception stops the program, whereas an unchecked sy-subrc
silently continues with wrong data. That is why new code uses exceptions.
sy-subrc
17
Classes are what make ABAP testable. ABAP Unit runs test classes marked FOR TESTING, and a class that
takes its dependencies through an interface can be tested with a stub in place of the database.
FOR TESTING
18
METHODS constructor IMPORTING io_reader TYPE REF TO lif_order_reader.19
Pass the real reader in production and a test double in the test. A method that reads the database
directly cannot be tested without one.
20
What the interviewer is probing.1. "Why use classes instead of function modules?" Probing: the argument. Stalls: "It is
modern." Moves up: inheritance, interfaces and real encapsulation make the code testable and
substitutable, which function groups with global data are not.
21
2. "What does a local class in a report give you?" Probing: practical knowledge. Stalls:
"Nothing." Moves up: structure and ABAP Unit testability without a global repository object, which
is the usual starting point for cleaning up a report.
22
3. "How do exceptions differ from the old sy-subrc style?" Probing: the error model. Stalls:
"They are cleaner." Moves up: a class-based exception carries context and propagates until
handled, where sy-subrc must be checked after every call or is silently ignored.
23
4. "What is a friend class for?" Probing: the detail. Stalls: "Nothing." Moves up:
granting another class access to protected and private members, which is how factory and unit test
helper patterns are built.