F Method in OO ABAP | CodeTheta

Method in OO ABAP

June 20, 2024

Method is a set of logic or statements which perform a particular task. It can be called several times. It is similar to a function module or subroutine.
We will be able to implement local class methods and global class methods. Method should be declared in the visibility section of that class.

Method Parameter:
Import, Export, Changing, returning.

Instance Method:
In local class method, method declared using METHODS keyword.
Instance methods can be accessed by using objects of that class.

Syntax:
CLASS cl_main DEFINITION.
  PUBLIC SECTION.
    METHODS: m1.
ENDCLASS.

CLASS cl_main IMPLEMENTATION.
  METHOD m1.
    WRITE:/ 'Instance Method'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA ob TYPE REF TO cl_main.
  CREATE OBJECT ob.
  CALL METHOD ob->m1.

Static Method:
In local class method, method declared using CLASS-METHODS keyword.
Instance methods can be accessed by using objects of that class and as well as using class names also.

Syntax:
CLASS cl_main DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS: m2.
ENDCLASS.

CLASS cl_main IMPLEMENTATION.
  METHOD m2.
    WRITE:/ 'Static Method'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  CALL METHOD cl_main=>m2.

If you have any Question you can contact us or mail us. We will reply you as soon as possible.

Post a Comment