CLASS a1 DEFINITION.
PUBLIC SECTION.
DATA: num1 TYPE i VALUE 100.
METHODS:m1.
ENDCLASS.

CLASS a1 IMPLEMENTATION.
METHOD m1.
WRITE: 'a1:',num1.
ENDMETHOD.
ENDCLASS.

CLASS b1 DEFINITION INHERITING FROM a1.
PUBLIC SECTION.
METHODS:m2, m1 REDEFINITION.
ENDCLASS.

CLASS b1 IMPLEMENTATION.
METHOD m1.
num1 = num1 .
WRITE: 'b1:',num1.
ENDMETHOD.

METHOD m2.
WRITE: 'M2 in class b1'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: a TYPE REF TO a1.
DATA: b TYPE REF TO b1.
*data: c type REF TO c1.


****************************************************************
CREATE OBJECT b.
a = b. "upcasting
CALL METHOD a->m1( ).
*call METHOD a->m2( ). " we can't access the own sub class methods using super class ref.
NEW-LINE.

b ?= a. " down casting
CALL METHOD b->m1( ).
NEW-LINE.
CALL METHOD b->m2( ).

*****************************************************************
" error null ref
create OBJECT a.
b ?= a. "down casting still its giving dump
call METHOD b->m1( ).

*****************************************************************

CREATE OBJECT a.
TRY.
 b ?= a. "u r attempted to use a 'NULL' object reference dump
CATCH cx_sy_move_cast_error.
 CALL METHOD b->m1( ).
ENDTRY.

*****************************************************************

CREATE OBJECT a.
CREATE OBJECT b.
TRY.
 b ?= a.
CATCH cx_sy_move_cast_error.
 CALL METHOD b->m1( ).
 call METHOD b->m2( ).
ENDTRY.




CLASS lcl_shape DEFINITION.
  PUBLIC SECTION.
    METHODS draw.
ENDCLASS.

CLASS lcl_circle DEFINITION INHERITING FROM lcl_shape.
  PUBLIC SECTION.
    METHODS: draw REDEFINITION,
      calc_area.
ENDCLASS.

CLASS lcl_shape IMPLEMENTATION.
  METHOD draw.
    WRITE :/ 'Drawing any Shape'.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_circle IMPLEMENTATION.
  METHOD draw.
    WRITE :/ 'Drawing specific shape: Circle'.
  ENDMETHOD.

  METHOD calc_area.
    WRITE :/ 'Area Of Crcle = 2iiR'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA : o_cir TYPE REF TO lcl_circle.
  CREATE OBJECT o_cir.
  CALL METHOD o_cir->draw( ). " calls subclass Draw() method
  CALL METHOD o_cir->calc_area( ).
  ULINE.
  "--------- Narrow cast(Upcast)---------------"
  DATA : o_shp TYPE REF TO lcl_shape.
  o_shp = o_cir. "  Narrow cast(Upcast)
  CALL METHOD o_shp->draw( ). " calls sub class Draw() method
  "call METHOD o_shp->calc_area( ) . " compilation error
  ULINE.

  "---------- Widening Cast(Downcast) -----------"

  DATA : o_cir1 TYPE REF TO lcl_circle.

  " o_cir1 = o_shp. "  complilation error
  o_cir1 ?= o_shp.  " Widening Cast(Downcast)
  CALL METHOD o_cir1->draw( ). " calls subclass Draw() method
  CALL METHOD o_cir1->calc_area( ).

CLASS a1 DEFINITION.
PUBLIC SECTION.
DATA: num1 TYPE i VALUE 100.
METHODS:m1.
ENDCLASS.

CLASS a1 IMPLEMENTATION.
METHOD m1.
WRITE: 'a1:',num1.
ENDMETHOD.
ENDCLASS.

CLASS b1 DEFINITION INHERITING FROM a1.
PUBLIC SECTION.
METHODS:m2, m1 REDEFINITION.
ENDCLASS.

CLASS b1 IMPLEMENTATION.
METHOD m1.
num1 = num1 .
WRITE: 'b1:',num1.
ENDMETHOD.

METHOD m2.
WRITE: 'M2 in class b1'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

DATA: parent TYPE REF TO a1.
DATA: child TYPE REF TO b1.
*data: c type REF TO c1.


****************************************************************
CREATE OBJECT child.
parent = child. "upcasting
CALL METHOD parent->m1( ).
CALL METHOD child->m1( ).
CALL METHOD child->m2( ).
*call METHOD a->m2( ). " we can't access the own sub class methods using super class ref.
NEW-LINE.

child ?= parent. " down casting
CALL METHOD child->m1( ).
NEW-LINE.
CALL METHOD child->m2( ).

******************************************************************
*" error null ref
*create OBJECT a.
*b ?= a. "down casting still its giving dump
*call METHOD b->m1( ).

******************************************************************
*
CREATE OBJECT a.
TRY.
 b ?= a. "u r attempted to use a 'NULL' object reference dump
CATCH cx_sy_move_cast_error.
 CALL METHOD b->m1( ).
ENDTRY.

******************************************************************
*
CREATE OBJECT a.
CREATE OBJECT b.
TRY.
 b ?= a.
CATCH cx_sy_move_cast_error.
 CALL METHOD b->m1( ).
 call METHOD b->m2( ).
ENDTRY.

******** Unique Values******************
 
DATA : lv_string       TYPE string,
       lv_string2      TYPE string,
       lv_string_final TYPE string.
TYPES: BEGIN OF ty_data,
         auth TYPE c,
       END OF ty_data.
DATA: ls_user  TYPE ty_data,
      ls_user2 TYPE ty_data,
      lt_user2 TYPE TABLE OF ty_data,
      lt_user  TYPE TABLE OF ty_data.
lv_string = 'a,b,c,a'.
lv_string2 = 'd,b,c,a'.
SPLIT lv_string AT ',' INTO TABLE lt_user.
SPLIT lv_string2 AT ',' INTO TABLE lt_user2.
LOOP AT lt_user2 ASSIGNING FIELD-SYMBOL(<fs_field>).
  INSERT  <fs_field> INTO TABLE lt_user.
ENDLOOP.
  SORT lt_user BY auth.
  DELETE ADJACENT DUPLICATES FROM lt_user COMPARING auth.
  IF sy-subrc = 0.
    LOOP AT lt_user ASSIGNING FIELD-SYMBOL(<fs_user_table>).
        IF lv_string_final IS INITIAL.
          lv_string_final = <fs_user_table>-auth.
        ELSE.
          CONCATENATE  lv_string_final <fs_user_table>-auth INTO lv_string_final SEPARATED BY ','.
        ENDIF.
    ENDLOOP.
  ENDIF.
WRITE lv_string_final.

 

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »