6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24OOP0109 – A Simple Guide to Declaring, Implementing and Calling Methods in OO ABAP

  1. Method Declaration

In OO ABAP, method declaration is done within the class definition. This is where you specify the method’s signature, which includes the method name, parameters and the returning value (if any).

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

       METHODS:

        add_numbers

        IMPORTING

        iv_num1 TYPE i

        iv_num2 TYPE i

           RETURNING

             VALUE(rv_sum) TYPE i.

   ENDCLASS.

   Here:

   – `add_numbers` is the method name.

   – `iv_num1` and `iv_num2` are the importing parameters of type integer.

   – `rv_sum` is the returning value, also of type integer.

  1. Method Implementation

After declaring the method in the class definition, the next step is to implement it in the class implementation section. This is where the actual logic of the method is defined.

Example:

   CLASS lcl_example IMPLEMENTATION.

     METHOD add_numbers.

        rv_sum = iv_num1 + iv_num2.

     ENDMETHOD.

   ENDCLASS.

 

   In this example, the `add_numbers` method adds the two input numbers and assigns the result to the returning parameter `rv_sum`.

 

  1. Calling a Method

To use the method you’ve declared and implemented, you need to create an instance of the class and then call the method using that instance.

   Example:

   DATA(lo_example) = NEW lcl_example( ).

   DATA(lv_result) = lo_example->add_numbers( iv_num1 = 5 iv_num2 = 10 ).

   WRITE: / ‘The sum is:’, lv_result.

 

   In this Example:

   – `lo_example` is an instance of the `lcl_example` class.

   – The `add_numbers` method is called with `iv_num1` set to `5` and `iv_num2` set to `10`.

   – The result, which is `15`, is stored in `lv_result` and then printed.

 

  1. Why Methods in OO ABAP Are Important

Methods in OO ABAP enable you to encapsulate functionality, making your code modular and reusable. By breaking down complex operations into methods, you make your ABAP programs easier to understand, maintain and extend.

 

  1. Best Practices for ABAP Method Design

Clarity: Use clear and descriptive names for methods and parameters.

Single Responsibility:  Each method should perform a single, well-defined task.

Reusability:  Write methods that can be reused across different classes or programs.

Documentation:  Use comments to describe the purpose and functionality of your methods, especially for complex logic.

Author : Aniket Pawar, 9373518385                                                     

24OOP0109 – A Simple Guide to Declaring, Implementing and Calling Methods in OO ABAP

  1. Method Declaration

In OO ABAP, method declaration is done within the class definition. This is where you specify the method’s signature, which includes the method name, parameters and the returning value (if any).

   Example:

   CLASS lcl_example DEFINITION.

     PUBLIC SECTION.

          METHODS:

          add_numbers

           IMPORTING

          iv_num1 TYPE i

          iv_num2 TYPE i

           RETURNING

           VALUE(rv_sum) TYPE i.

   ENDCLASS.

   Here:

   – `add_numbers` is the method name.

   – `iv_num1` and `iv_num2` are the importing parameters of type integer.

   – `rv_sum` is the returning value, also of type integer.

  1. Method Implementation

After declaring the method in the class definition, the next step is to implement it in the class implementation section. This is where the actual logic of the method is defined.

Example:

   CLASS lcl_example IMPLEMENTATION.

     METHOD add_numbers.

        rv_sum = iv_num1 + iv_num2.

     ENDMETHOD.

   ENDCLASS.

 

   In this example, the `add_numbers` method adds the two input numbers and assigns the result to the returning parameter `rv_sum`.

 

  1. Calling a Method

To use the method you’ve declared and implemented, you need to create an instance of the class and then call the method using that instance.

   Example:

   DATA(lo_example) = NEW lcl_example( ).

   DATA(lv_result) = lo_example->add_numbers( iv_num1 = 5 iv_num2 = 10 ).

   WRITE: / ‘The sum is:’, lv_result.

 

   In this Example:

   – `lo_example` is an instance of the `lcl_example` class.

   – The `add_numbers` method is called with `iv_num1` set to `5` and `iv_num2` set to `10`.

   – The result, which is `15`, is stored in `lv_result` and then printed.

 

  1. Why Methods in OO ABAP Are Important

Methods in OO ABAP enable you to encapsulate functionality, making your code modular and reusable. By breaking down complex operations into methods, you make your ABAP programs easier to understand, maintain and extend.

 

  1. Best Practices for ABAP Method Design

Clarity: Use clear and descriptive names for methods and parameters.

Single Responsibility:  Each method should perform a single, well-defined task.

Reusability:  Write methods that can be reused across different classes or programs.

Documentation:  Use comments to describe the purpose and functionality of your methods, especially for complex logic.

Author : Aniket Pawar, 9373518385