Syntax

Template for making a class
Template for calling a class
Subclass
Template for calling a class
Using af class as a parameter for a method
Interfaces
Events

Template for making a class

Delete the parts that should not be used

******************************************
* Definition part
******************************************
CLASS xxx DEFINITION.

*------------------------------
* Public section
*------------------------------
  PUBLIC SECTION.
    TYPES:
    DATA: 
*   Static data
    CLASS-DATA:
*   Methods
    METHODS:
*     Using the constructor to initialize parameters
       constructor    IMPORTING xxx type yyy,
                                
*     Method with parameters
      mm1 IMPORTING iii   TYPE ttt.
*     Method without parameters
      mm2.
*   Static methods
    CLASS-METHODS:  
                    
*---------------------------------------------------*
* Protected section. Also accessable by subclasses
*---------------------------------------------------
  PROTECTED SECTION.
*---------------------------------------------------
* Private section. Not accessable by subclasses
*---------------------------------------------------
  PRIVATE SECTION.
ENDCLASS.

******************************************
* Implementation part
******************************************
CLASS lcl_airplane IMPLEMENTATION.
  METHOD constructor.
  ENDMETHOD.

  METHOD mm1.
  ENDMETHOD.

  METHOD mm2.
  ENDMETHOD.

ENDCLASS.

 

Template for calling a class

* Create reference to class lcl_airplane
DATA: airplane1 TYPE REF TO lcl_airplane.

START-OF-SELECTION.
* Create instance using parameters in the cosntructor method
  CREATE OBJECT airplane1 exporting im_name = 'Hansemand'
                                    im_planetype = 'Boing 747'.
*  Calling a method with parameters
  CALL METHOD: airplane1->display_n_o_airplanes,
               airplane1->display_attributes.

Subclass

CLASS xxx DEFINITIONINHERITING FROM yyy.

Using af class as a parameter for a method

The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:

METHODS:
  add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.

Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part, and do not have visibillity sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

 

Example of how to implement an interface:
INTERFACE lif_document
  DATA:          author type ref to lcl_author.
  METHODS:  print,
                     display.
ENDINTERFACE.
CLASS lcl_text_document DEFINITION.
   PUBLIC SECTION.
      INTERFACES lif_document.
      METHODS     display.
ENDCLASS.
CLASS lcl_text_document IMPLEMENTTION.
  METHOD lif_document~print.
  ENDMETHOD.
  METHOD lif_document~display
  ENDMETHOD.
  METHOD display.
  ENDMETHOD.
ENDCLASS.
REPORT zzz.
  DATA: text_doc TYPE REF TO lcl_document.
  Start-of-selection.
    CREATE OBJECT text_doc.
    CALL METHOD text_doc->lif_document~print.
    CALL METHOD text_doc->lif_document~display.
    CALL METHOD text_doc->display.

Events

For events of controls, refer to How to use controls.

Defining events:
CLASS <classname> DEFINITION.
  EVENTS: <event> EXPORTING VALUE (<ex_par>) TYPE type.
CLASS <classname> IMPLEMENTATION.
  METHOD <m>:
    RAISE EVENT <event> EXPORTING <ex_par> = <act_par>.
Handling events:
CLASS <classname> DEFINITION.
  METHODS: <on_event> FOR <event> OF <classname> ! <interface> IMPORTING <imp_par1>...<imp_parN> SENDER.
Setting handler  
SET HANDLER <ref_handle> <on_event> FOR <ref_sender> ! FOR ALL INSTANCES
                             [ACTIVATION <var>]

 

Converted from CHM to HTML with chm2web Standard 2.7 (unicode)