Professional Documents
Culture Documents
ABAP Quick Reference
ABAP Quick Reference
This quick reference is primarily focusing on ABAP’s object oriented programming features, like classes,
OOP-exceptions, and inheritance.
FUNCTION inc_age.
ADD 1 to age.
ENDFUNCTION.
* report content
PUBLIC SECTION.
DATA: mv_publicvar TYPE i.
PRIVATE SECTION.
DATA: mv_privatevar1 TYPE i READ-ONLY, " const
mv_privatevar2 LIKE mv_privatevar1.
ENDCLASS.
Methods
Normal methods are declared using the METHOD keyword. CLASS-METHODS are ABAP’s equivalent to static
methods, known from other programming languages. The actual implementation uses the same keyword, for
both types of methods: METHOD
CLASS lcl_classname.
" attributes
...
PUBLIC SECTION.
METHODS method_name
IMPORTING iv_par TYPE ANY
EXPORTING ev_par TYPE i
CHANGING cv_par TYPE type_name
RETURNING value(rv_par) TYPE i
EXCEPTIONS exception " sy-subrc id: 1
RAISING exception_class.
PROTECTED SECTION.
ENCLASS.
...
CLASS lcl_classname.
METHOD method_name.
" method body
...
ENDMETHOD.
METHOD static_method.
...
ENDMETHOD.
ENDCLASS.
Create an object
CLASS lcl_classname.
...
ENDCLASS.
...
START-OF-SELECTION.
* create reference
DATA:
go_objectname TYPE REF TO lcl_classname,
gt_objecttablename TYPE TABLE OF REF TO lcl_classname.
* create instance
CREATE OBJECT go_objectname.
Constructor
The this keyword from other programming languages is called me in ABAP Objects.
...
ENDCLASS.
ENDCLASS.
...
...
See section Run time type services for a clean way to realize down-casts without the TRY ...
CATCH construct.
Interfaces
While multiple inheritance is not available in ABAP, a class can implement multiple interfaces.
INTERFACE lif_interface.
METHODS interface_method.
ENDINTERFACE.
Events
Four steps are necessary to work with events:
Raise events
* event definition
CLASS raising_class DEFINITION.
EVENTS eventname [ EXPORTING value(ev_par) TYPE typename ].
...
ENDCLASS.
* event implementation
CLASS raising_class IMPLEMENTATION.
METHOD methodname.
RAISE EVENT eventname [ EXPORTING ev_par = lv_par ].
ENDMETHOD.
ENDCLASS.
Handle events
* abstract class
CLASS lcl_classname DEFINITION ABSTRACT.
* abstract method
METHODS ... ABSTRACT ... .
ENDCLASS.
Final classes
* final class
CLASS lcl_classname DEFINITION FINAL.
* final method
METHODS ... FINAL ... .
ENDCLASS.
Constructor visibility
Friend classes
Classes can allow other classes to access private and protected methods (including the constructor method;
see Constructor visibility) and attributes by declaring them as friends.
...
Exceptions
The traditional way of throwing exceptions in ABAP was RAISE, the new, object oriented style for throwing an
exception object uses the keywords RAISE EXCEPTION TYPE
* throw exception
RAISE EXCEPTION TYPE cx_exception
[ EXPORTING export1 = ... ].
* handle exceptions
TRY.
...
CATCH cx_exception INTO gx_exception_object.
gx_exception_object->get_text( ).
...
ENDTRY.
Class methods that trow exceptions need that information in their definition (the non oop
style RAISING became EXCEPTIONS):
METHODS:
method_name
...
EXCEPTIONS " oop style exceptions
cx_exception_class.
ABAP also features RETRY and CATCH BEFORE UNWIND ... RESUME . Whereas former restarts the
entire TRY block and latter continues right after the line where the exception was raised ( RAISE RESUMABLE
EXCEPTION).