In the previous post Object Oriented ABAP [1] : Introduction to ABAP Class we saw Class Definition and Class Implementation. In this post we will go into bit more details.
A new class is created and a method call_abap_class is created for this. Refer the class below.
Related Video:
Creating Object and Calling Methods
To create a new object one of below ways can be used.
"Old Way / Before 7.40
DATA : lo_abap TYPE REF TO zjp_abap.
CREATE OBJECT lo_abap.
"New Way / From 7.40
DATA(lo_abap_new) = NEW zjp_abap( ).
To call an instance method, object of a class is required and operator -> is used. To call a static method, object is not required. A static method can be called with the class itself using operator =>.
"Call methods
CALL METHOD lo_abap->instance_method.
CALL METHOD zjp_abap=>static_method.
"Call methods another way
lo_abap_new->instance_method( ).
zjp_abap=>static_method( ).
Method Parameters
Method can have IMPORTING, EXPORTING, CHANGING and RETURNING parameters. A method with first 3 types of parameter looks like below.
METHODS
method_with_parameters
IMPORTING
iv_var_a TYPE i
iv_var_b TYPE i
EXPORTING
ev_sum TYPE i
CHANGING
cv_result TYPE i.
A method call to this method is as below.
DATA : lv_sum TYPE i,
lv_result TYPE i.
lv_result = 4.
lo_abap_new->method_with_parameters(
EXPORTING
iv_var_a = 10
iv_var_b = 20
IMPORTING
ev_sum = lv_sum
CHANGING
cv_result = lv_result
).
Notice that, similar to function module calls, method’s importing parameter are sent as exporting from the call and vice-a-versa while changing remains changing.
For RETURNING parameters, we need to use key word RECEIVING in the call. Also, a method can have only one returning parameter and hence such methods can be called in a better and simpler way as below.
"Definition
METHODS
functional_method
RETURNING VALUE(rv_val) TYPE i.
"Call
DATA(lv_val) = lo_abap_new->functional_method( ).
Notice that returning parameters are always passed by value and important thing to remember is that only one returning parameter is allowed.
For more posts on Object Oriented ABAP, please visit the page OOABAP.
If you like the content, please subscribe…