Class of an Object Instance

By applying the IS INSTANCE OF statement on an given object reference, it is possible to know the exact instance of a class or it’s subclass. This is quite helpful in many cases and now there is one more but yes a more easy of doing this is by using the TYPE OF construct with CASE.

Below is the use of IS INSTANCE OF


CLASS lcl_item_main DEFINITION.
PUBLIC SECTION.
METHODS: build.
ENDCLASS.

CLASS lcl_item_sub DEFINITION INHERITING FROM lcl_item_main.
PUBLIC SECTION.
METHODS: build REDEFINITION.
METHODS: price.
ENDCLASS.

CLASS lcl_item_main IMPLEMENTATION.
METHOD build.
WRITE:/ ‘main item’.
ENDMETHOD.
ENDCLASS.

CLASS lcl_item_sub IMPLEMENTATION.
METHOD build.
WRITE:/ ‘sub item’.
ENDMETHOD.
METHOD price.
WRITE:/ ‘sub item price’.
ENDMETHOD.
ENDCLASS.

CLASS main DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: build_item IMPORTING io_ref TYPE REF TO object.
ENDCLASS.

CLASS main IMPLEMENTATION.
METHOD build_item.
DATA: lo_sub TYPE REF TO lcl_item_sub.
DATA: lo_main TYPE REF TO lcl_item_main.
IF io_ref
IS INSTANCE OF lcl_item_sub.
lo_sub ?= io_ref.
lo_sub->build( ).
lo_sub->price( ).
ELSEIF io_ref
IS INSTANCE OF lcl_item_main.
lo_main ?= io_ref.
lo_main->build( ).
ENDIF.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: lo_ref TYPE REF TO object.
DATA: lv_main TYPE c VALUE ‘ ‘.
IF lv_main = abap_true.
CREATE OBJECT lo_ref TYPE lcl_item_main.
ELSE.
CREATE OBJECT lo_ref TYPE lcl_item_sub.
ENDIF.
main=>build_item( lo_ref ).


Execute the program-

Output-

Execute the program-

Output-

Use of TYPE OF construct with CASE-


CLASS lcl_item_main DEFINITION.
PUBLIC SECTION.
METHODS: build.
ENDCLASS.

CLASS lcl_item_sub DEFINITION INHERITING FROM lcl_item_main.
PUBLIC SECTION.
METHODS: build REDEFINITION.
METHODS: price.
ENDCLASS.

CLASS lcl_item_main IMPLEMENTATION.
METHOD build.
WRITE:/ ‘main item’.
ENDMETHOD.
ENDCLASS.

CLASS lcl_item_sub IMPLEMENTATION.
METHOD build.
WRITE:/ ‘sub item’.
ENDMETHOD.
METHOD price.
WRITE:/ ‘sub item price’.
ENDMETHOD.
ENDCLASS.

CLASS main DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: build_item IMPORTING io_ref TYPE REF TO object.
ENDCLASS.

CLASS main IMPLEMENTATION.
METHOD build_item.
CASE TYPE OF io_ref.
WHEN TYPE lcl_item_sub INTO DATA(lo_sub).
lo_sub->build( ).
lo_sub->price( ).
WHEN TYPE lcl_item_main INTO DATA(lo_main).
lo_main->build( ).
WHEN OTHERS.
” do nothing.
ENDCASE.

ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: lo_ref TYPE REF TO object.
DATA: lv_main TYPE c VALUE ‘X’.
IF lv_main = abap_true.
CREATE OBJECT lo_ref TYPE lcl_item_main.
ELSE.
CREATE OBJECT lo_ref TYPE lcl_item_sub.
ENDIF.
main=>build_item( lo_ref ).


Execute the program-

Output-

Execute the program

Output-

The comparison- Old Way Vs New Way


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s