Gyan Factory

Gyan Factory
SAP Technical Project Support
Showing posts with label SAP ABAP OOPS. Show all posts
Showing posts with label SAP ABAP OOPS. Show all posts

Friday, January 29, 2016

OOABAP PROGRAMS

*********** CLASS CONSTRUCTOR***************

CLASS test_11 DEFINITION.

  PUBLIC SECTION.
    CLASS-METHODS : class_constructor.
    METHODS : constructor IMPORTING var TYPE i,
              display.
ENDCLASS.                    "TEST_11 DEFINITION


CLASS test_11 IMPLEMENTATION.

  METHOD constructor .

    WRITE :/ 'VAR = ', var.
WRITE :/ 'CONST METHOD IS AUTOM CALLED WHEN OBJECT OF CLASS IS CREATED'.

  ENDMETHOD.                    "CONSTRUCTOR

  METHOD display.
    WRITE :/ 'DISP METHOD IS CALLED EXPLICITLY'.

  ENDMETHOD.                    "DISPLAY

  METHOD class_constructor.

    WRITE :/ 'CLASS CONST IS EXECUTED ONLE ONCE PER CLASS'.

  ENDMETHOD.                    "CLASS_CONSTRUCTOR

ENDCLASS.                    "TEST_11 IMPLEMENTATION
START-OF-SELECTION.

  DATA : obj1 TYPE REF TO test_11.
  DATA : obj2 TYPE REF TO test_11.

  CREATE OBJECT obj1 EXPORTING var = 10.
  ULINE.
  CREATE OBJECT obj2 EXPORTING var = 20.
  ULINE.
  CALL METHOD obj1->display.

-------------------------------------------------------------------

*******CLASS EVENTS*********

CLASS test_12 DEFINITION.

  PUBLIC SECTION.
    DATA : num TYPE i VALUE 0.

    METHODS : display.

    EVENTS : overflow EXPORTING value(var) TYPE i.

ENDCLASS.                    "TEST_12 DEFINITION


CLASS test_12 IMPLEMENTATION.
  METHOD display.

    num = num + 1.
    IF num > 5.

      RAISE EVENT overflow EXPORTING var = num.

    ENDIF.

    WRITE :/ ' NUM = ', num.
  ENDMETHOD.                    "DISPLAY

ENDCLASS.                    "TEST_12 IMPLEMENTATION

CLASS event1 DEFINITION.
  PUBLIC SECTION.

    METHODS handle_over FOR EVENT overflow OF test_12 IMPORTING var .
ENDCLASS.                    "EVENT1 DEFINITION


CLASS event1 IMPLEMENTATION.

  METHOD handle_over.
    ULINE.
    WRITE :/ ' ENEVT HANDLER METHOD IS CALLED FOR THE EVENT OVERFLOW'.
    WRITE :/ ' EXPORTING PARAMETER FORM EVENT TO HANDLER VAR = ', var.
    ULINE.
  ENDMETHOD.                    "HANDLE_OVER

ENDCLASS.                    "EVENT1 IMPLEMENTATION

START-OF-SELECTION.

  DATA obj TYPE REF TO test_12.

  DATA obj1 TYPE REF TO test_12.
  CREATE OBJECT obj.
  CREATE OBJECT obj1.
  DATA : o_eve TYPE REF TO event1.
  CREATE OBJECT o_eve.

  SET HANDLER o_eve->handle_over FOR ALL INSTANCES.

  DO 10 TIMES.
    CALL METHOD obj->display.
  ENDDO.

  ULINE.

  DO 10 TIMES.
    CALL METHOD obj1->display.
  ENDDO.

-------------------------------------------------------------------------------

***********INHERITANCE(1)************

CLASS test_100 DEFINITION.
  PUBLIC SECTION.
    DATA : num TYPE i VALUE 10.
    METHODS : display,
              constructor.

    protected section.
    data : num2 type i value 20.
    methods : calculate.

    private section.
    data : num3 type i value 30.
    methods : calc .
ENDCLASS.                    "test_100 DEFINITION


CLASS test_100 IMPLEMENTATION.

  METHOD display.

    WRITE :/ ' value of num in super class = ', num.
    calculate( ).
    calc( ).
  ENDMETHOD.                    "display


  method calculate.
  write :/ 'value of protected data object num2 = ', num2.
  endmethod.

  method calc.
  write :/ 'value of private data object num3 = ', num3.
  endmethod.

  method constructor.

  write :/ 'constructor of the super class'.
  endmethod.

ENDCLASS.                    "test_100 IMPLEMENTATION

CLASS test_200 DEFINITION INHERITING FROM test_100.
  PUBLIC SECTION.
    METHODS : show,
              constructor,
              display redefinition.

ENDCLASS.                    "test_200 DEFINITION

CLASS test_200 IMPLEMENTATION.
  METHOD show.

    WRITE :/ 'method show of subclass'.
    calculate( ).

  ENDMETHOD.                    "show


  METHOD display.
    WRITE :/ 'super class method implemented in sub class'.
  ENDMETHOD.                    "display

method constructor.
 call method super->constructor.
write :/ 'constructor of the sub class'.

endmethod.

ENDCLASS.                    "test_200 IMPLEMENTATION
START-OF-SELECTION.

  DATA obj1 TYPE REF TO test_100.
  DATA obj2 TYPE REF TO test_200.
  CREATE OBJECT : obj1.
  uline.
  create object obj2.

  CALL METHOD obj1->display.

  ULINE.
  CALL METHOD obj2->show.
  ULINE.
  CALL METHOD obj2->display.
  uline.

---------------------------------------------------------------------------------

*********INHERITANCE(2)******************

class lcl_a definition.

public section.
methods : display_a.
endclass.

class lcl_a implementation.
method display_a.
write :/ 'method of super class'.
endmethod.
endclass.


class lcl_b definition inheriting from lcl_a.
public section.
methods : display_b.

endclass.

class lcl_b implementation.
method display_b.
write :/ 'method of super and sub class'.
endmethod.
endclass.


class lcl_c definition inheriting from lcl_b.
public section.
methods : display_c.

endclass.

class lcl_c implementation.
method display_c.
write :/ 'method of sub class'.
endmethod.
endclass.

-------------------------------------------------------------------------------

*********INHERITANCE(3)******************

class lcl_a definition.
public section.
methods disp.
endclass.

class lcl_a implementation.
method disp.
write :/ 'display method of super class'.
endmethod.
endclass.


class lcl_b definition inheriting from lcl_a.
public section.
methods show.
methods : disp  redefinition.
endclass.

class lcl_b implementation.
method show.
write :/ 'show method of sub class'.
endmethod.


method disp.
write :/ 'disp method of super class is redefined in sub class'.
endmethod.
endclass.

start-of-selection.

data : obj1 type ref to lcl_a,
        obj2 type ref to lcl_b.
         create object obj1.
        create object obj2.
        call method obj1->disp.
        uline.

        call method obj2->show.
         call method obj2->disp.

-----------------------------------------------------------------------------

*********INHERITANCE(4)******************

CLASS test_100 DEFINITION.
  PUBLIC SECTION.
    DATA : num TYPE i VALUE 10.
    METHODS : display.
ENDCLASS.                    "test_100 DEFINITION
class test_100 implementation.

method display.
write :/ ' value of num in super class = ', num.
endmethod.
endclass.

class test_200 definition inheriting from test_100.
public section.
methods : show.

endclass.

class test_200 implementation.
method show.

write :/ 'method show of subclass'.
endmethod.
endclass.

start-of-selection.

data obj1 type ref to test_100.
data obj2 type ref to test_200.
create object : obj1, obj2.

call method obj1->display.
uline.
call method obj2->show.
uline.
call method obj2->display.

-------------------------------------------------------------------------------

*********INHERITANCE(5)******************

CLASS test_100 DEFINITION.
  PUBLIC SECTION.
    DATA : num TYPE i VALUE 10.
    METHODS : display.
ENDCLASS.                    "test_100 DEFINITION


CLASS test_100 IMPLEMENTATION.

  METHOD display.
    WRITE :/ ' value of num in super class = ', num.
  ENDMETHOD.                    "display
ENDCLASS.                    "test_100 IMPLEMENTATION
CLASS test_200 DEFINITION INHERITING FROM test_100.
  PUBLIC SECTION.
    METHODS : show,
              display redefinition.

ENDCLASS.                    "test_200 DEFINITION

CLASS test_200 IMPLEMENTATION.
  METHOD show.

    WRITE :/ 'method show of subclass'.
  ENDMETHOD.                    "show
  METHOD display.
    WRITE :/ 'super class method implemented in sub class'.
  ENDMETHOD.                    "display

ENDCLASS.                    "test_200 IMPLEMENTATION

START-OF-SELECTION.

  DATA obj1 TYPE REF TO test_100.
  DATA obj2 TYPE REF TO test_200.
  CREATE OBJECT : obj1, obj2.

  CALL METHOD obj1->display.
  ULINE.
  CALL METHOD obj2->show.
  ULINE.
  CALL METHOD obj2->display.

-----------------------------------------------------------------------------

*********INHERITANCE(6)******************

CLASS lcl_a DEFINITION .
  PUBLIC SECTION.
    METHODS : display final.
ENDCLASS.                    "lcl_a DEFINITION

CLASS lcl_a IMPLEMENTATION.
method display.

write :/ 'display method called'.
endmethod.
ENDCLASS.                    "lcl_a IMPLEMENTATION

class lcl_b definition inheriting from lcl_a.

public section.
methods : show.
*  methods : display redefinition. " YOU CANT REDEFINE A FINAL METHOD
endclass.


class lcl_b implementation.
method show.
write :/ 'show method called'.
endmethod.
endclass.

start-of-selection..

data obj1 type ref to lcl_a.
data obj2 type ref to lcl_b.

create object obj1.
create object obj2.

obj1->display( ).
obj2->show( ).
obj2->display( ).

------------------------------------------------------------------------------

*********INHERITANCE(7) WITH ABSTRACT CLASS******************
class lcl_a definition abstract.
public section.
methods : display abstract.
class-methods : calc.
endclass.

class lcl_a implementation.
method calc.
write :/ 'non abstract method of abstract class'.
endmethod.
endclass.

class lcl_b definition inheriting from lcl_a.

public section.
methods : show,
                display redefinition.
endclass.

class lcl_b implementation.
method show.
write :/ 'method show of sub class'.
endmethod.

method display.
write :/ 'display method of abstract class'.
endmethod.
endclass.

start-of-selection.

data obj1 type ref to lcl_a.
data obj type ref to lcl_b.
create object obj.
call method obj->show.
call method obj->display.

call method lcl_a=>calc.

-----------------------------------------------------------------------------

***********FRIEND CLASS *****************

CLASS DEMO1 DEFINITION DEFERRED. " forward declaration


CLASS DEMO2 DEFINITION FRIENDS DEMO1.
  PUBLIC SECTION.
    DATA : num TYPE i VALUE 10.
    METHODS display.
  PRIVATE SECTION .
    DATA : p_num TYPE i VALUE 1000.
ENDCLASS.                    "DEFINITION

CLASS DEMO2 IMPLEMENTATION.

  METHOD display.

    WRITE :/ 'num =',num.
    WRITE :/ 'private data object p_num', p_num.
  ENDMETHOD.                    "display
ENDCLASS.                   


CLASS DEMO1 DEFINITION.
  PUBLIC SECTION.
    DATA : obj2 TYPE REF TO DEMO2.
    METHODS show.
ENDCLASS.                    " DEFINITION


CLASS DEMO1 IMPLEMENTATION.
  METHOD show.
  create object obj2.
    WRITE :/  obj2->p_num.
  ENDMETHOD.                    "show

ENDCLASS.                   

START-OF-SELECTION.

  DATA obj TYPE REF TO DEMO2.
  CREATE OBJECT obj.

  DATA obj1 TYPE REF TO DEMO1.
  CREATE OBJECT obj1.

  CALL METHOD obj->display.
  WRITE / obj->num.

  ULINE.
  CALL METHOD obj1->show. 

----------------------------------------------------------------------------

 *****************UNIT TEST OF A METHOD ********************

class lcl_demo definition."#EC *
  PUBLIC SECTION.
  METHODS : CALCULATE IMPORTING NUM1 TYPE I
                                                                  NUM2 TYPE I
                                      EXPORTING RES TYPE I."#EC *
  endclass.

  class lcl_demo implementation.
    METHOD CALCULATE.
      RES = NUM1 + NUM2.
      ENDMETHOD.
    endclass.

    START-OF-SELECTION.
    DATA : OBJ TYPE REF TO LCL_DEMO.

   DATA : RESULT TYPE I.
   DATA : VAL ."#EC NEEDED
   DATA : NN."#EC NEEDED
    CLASS TEST DEFINITION FOR TESTING."#AU Risk_Level Harmless
      PUBLIC SECTION.
      METHODS METHOD_TEST FOR TESTING.
      ENDCLASS.

      CLASS TEST IMPLEMENTATION.
        METHOD METHOD_TEST.
           CREATE OBJECT OBJ.

         OBJ->CALCULATE( EXPORTING NUM1 = 10
                                    NUM2 = 20
                         IMPORTING RES = RESULT ).
         call method cl_aunit_assert=>assert_equals
           exporting
             exp                  = '30'
             act                  = RESULT
             msg                  = 'TWO VALUES ARE NOT SAME'
*             level                = '2'
*             tol                  =
*             quit                 = METHOD
*             ignore_hash_sequence = ABAP_FALSE
           receiving
             assertion_failed     = VAL  .
          ENDMETHOD.
        ENDCLASS. 

------------------------------------xxxxx---------------------------------

EXCEPTION HANDLING

*****EXCEPTION WITH TRY CATCH & ENDTRY BLOCK ********

data : res type i.
parameters : num1 type i,
             num2 type i.

data : obj type ref to cx_root.
data : msg type string.
try.
  res = num1 / num2.
  write :/ 'result =', res.
  catch cx_root into obj.
    msg = obj->get_longtext( ).
    write :/ msg.
  endtry.

*****EXCEPTION WITH TRY CATCH & ENDTRY BLOCK *******

class lcl_a definition.
  public section.
  data : num1 type i,
            num2 type i,
            res type i.
  data : ref type ref to cx_root,
            msg type string.
  methods : meth importing var1 type i
                                             var2 type i.

  endclass.
  class lcl_a implementation.
    method meth.
      num1 = var1.
      num2 = var2.
      try.
      res = num1 / num2.
      write :/ 'result of division = ', res.
      catch cx_root into ref.
        msg = ref->get_longtext( ).
        write :/ msg.
      endtry.
      endmethod.
    endclass.

    start-of-selection.
    data obj type ref to lcl_a.
    create object obj.

    parameters : p_num1 type i,
                 p_num2 type i.
    obj->meth( var1 = p_num1
               var2 = p_num2 ).

******EXCEPTION WITH TRY CATCH & ENDTRY BLOCK *********

parameters : num type i.
data : RES TYPE P DECIMALS 2,
            o_ref type ref to cx_root,
            text type string.

START-OF-SELECTION.
WRITE :/ 'DIVISION AND SQUARE ROOT CHECKING'.
ULINE.
TRY .
IF ABS( NUM ) > 100.
  RAISE EXCEPTION TYPE CX_DEMO_ABS_TOO_LARGE.
  ENDIF.
TRY.
RES = 1 / NUM.
WRITE :/ 'RESULT OF DIVISION=', RES.
RES = SQRT( NUM ).
WRITE :/ 'RESULT OF SQUARE', RES.
CATCH CX_SY_ZERODIVIDE INTO O_REF.
  TEXT = O_REF->GET_TEXT( ).
  CLEANUP.
    CLEAR RES.
    ENDTRY.
    CATCH CX_SY_ARITHMETIC_ERROR INTO O_REF.
      TEXT = O_REF->GET_TEXT( ).
      CATCH CX_ROOT INTO O_REF.
        TEXT = O_REF->GET_TEXT( ).
        ENDTRY.
        IF NOT TEXT IS INITIAL.
          WRITE :/ TEXT.
          ENDIF.

          WRITE :/ 'FINAL RESULT', RES.

***EXCEPTION WITH NESTED TRY CATCH & ENDTRY BLOCK ********

data : o_ref type ref to cx_root,
            text type string.
try.
    try.
        raise exception type cx_demo_constructor
                        exporting my_text = sy-repid.
      catch cx_demo_constructor into o_ref.
        text = o_ref->get_text( ).
        write / text.
        raise exception o_ref.
    endtry.
  catch cx_demo_constructor into o_ref.
    text = o_ref->get_text( ).
    write / text.
endtry.

***EXCEPTION WITH TRY CATCH & ENDTRY BLOCK IN A CLASS *****
class lcl_a definition.
public section.
methods : meth1 importing p type string
                    raising cx_demo_constructor
                                  cx_demo_abs_too_large.

endclass.               
class lcl_b definition.
  public section.
  data : obj_a type ref to lcl_a.
  methods : meth2 raising cx_demo_constructor.
  endclass.


    class  lcl_b implementation.
      method meth2.
        create object obj_a.
        try.
          obj_a->meth1( 'tricon' ).
          catch cx_demo_abs_too_large.
          endtry.
        endmethod.
      endclass.

      class lcl_a implementation.
        method meth1.
          raise exception type cx_demo_constructor.
          endmethod.
        endclass.


 start-of-selection.

data : obj_b type ref to lcl_b.

 create object obj_b.

  try.
    obj_b->meth2( ).
    catch cx_demo_constructor.
      write :/ 'catching cx_demo_constructor'.
    endtry.

*****EXCEPTION WITH INHERITANCE  IN A CLASS ***********

 class local_exp definition inheriting from cx_static_check.

  endclass.
  start-of-selection.
  TRY .
raise exception type local_exp.
  CATCH local_exp.
message 'caught' type 'I'.
  ENDTRY.

 ********EXCEPTION WITH INHERITANCE  IN A CLASS ***********

 class local_exp definition inheriting from cx_static_check.
  public section.
    data : text type string.
    methods : constructor importing f_text type string.
endclass.             

class local_exp implementation.
  method constructor.
    super->constructor( ).
    text = f_text.

  endmethod.                 

endclass.                  

data : o_ref type ref to local_exp.
start-of-selection.
TRY .
raise exception type local_exp exporting f_text = 'local exceptions'.
CATCH local_exp into o_ref.
message o_ref->text type 'I'.
ENDTRY.

 ******EXCEPTION WITH INHERITANCE  IN A CLASS ***********


class cx_local_exception definition inheriting from cx_sy_arithmetic_error.
  public section.
  methods : constructor importing situation type string.
  endclass.
  class cx_local_exception implementation.
   method constructor.
     super->constructor( operation = situation ).
     endmethod.
    endclass.

   data : o_ref type ref to cx_local_exception,
          text type string.

    start-of-selection.
    try.
      raise exception type cx_local_exception exporting situation = 'start-of-selection'.
      catch cx_local_exception into o_ref.
        text = o_ref->get_text( ).
        message text type 'I'.
      endtry.

 ********EXCEPTION WITH FILES *******************
data : o_ref type ref to cx_sy_file_open_mode,
            text type string.

TRY .
raise exception type cx_sy_file_open_mode
                 exporting textid = CX_SY_FILE_OPEN_MODE=>read_only
                                   filename = 'SS.DAT'.
CATCH CX_SY_FILE_OPEN_MODE INTO O_REF.
  TEXT = O_REF->GET_TEXT( ).
  MESSAGE TEXT TYPE 'I'.

ENDTRY.

TRY .
raise exception type cx_sy_file_open_mode
                 exporting textid = CX_SY_FILE_OPEN_MODE=>NOT_OPEN
                                    filename = 'SS.DAT'.
CATCH CX_SY_FILE_OPEN_MODE INTO O_REF.
  TEXT = O_REF->GET_TEXT( ).
  MESSAGE TEXT TYPE 'I'.

ENDTRY.

TRY .
raise exception type cx_sy_file_open_mode
                 exporting textid = CX_SY_FILE_OPEN_MODE=>INCOMPATIBLE_MODE
                                     filename = 'SS.DAT'.
CATCH CX_SY_FILE_OPEN_MODE INTO O_REF.
  TEXT = O_REF->GET_TEXT( ).
  MESSAGE TEXT TYPE 'I'.ENDTRY.

 -----------------------------------xxxxx---------------------------------

Sunday, January 17, 2016

SE24 Method Pre-Exit, Post-Exit and OverWrite-Exit

For any SE24 class method we can define pre-exit, post-exit and overwrite-exit method by enhancing the original method.

We can apply pre exit and post exit both  on a single method.
If per-exit or post-exit is applied to a method then we can not apply over-write exit on the same method.

Step1. Go to TCODE-SE24, Provide a class name and click on the Create button.












Step2. Provide the description and click on the SAVE button.




















Step3. In the methods section, create a new method 'SHOW_MESSAGE' with level, visibility and description as shown below. Now double click on the method name to implement it.












Step4. Provide some statement in the method. Activate it .













Step5. Now go to TCODE-SE38 and create a report progam.



















Step6. Create the object of the above created class, call the method and run it.















Step7. The out put of the method is as follows.














Step8. Enhancing the method to create a pre-exit method of the existing method. Select the method and from the menu Class select Enhance.















Step9. From the appearing screen provide the enhancement implementation name and short text and click on the continue button.















Step10. Now in the method right hand side some now tabs are added as a result of method enhancement.













Step11. Now select the method, click on the EDIT menu, go to Enhancement Operations and select insert Pre-Method.












Step12. Click YES to continue.













Step13. Now click on the Editor that inserted under PRE-EXIT tab against the method 'SHOW_MESSAGE'.










Step14. Provide some statement in the pre-exit method of the SHOW_MESSAGE method.




















Step15. Now run the above created report.













Step16. The Out Put is shown below. For any method if pre-exit method is there and the program calls the method, the pre-exit method is executed first and then the original method is called.













Step17. Creating Post-Exit . Select the method and from the EDIT menu select Enhancement Operations and click on Insert Post-Method.















Step18.  Now click on the Editor that inserted under Post-Exit tab against the method 'SHOW_MESSAGE'.










Step19.  Provide some statement in the post-exit method of the SHOW_MESSAGE method.


























Step20.  Now run the above created report.


















Step21.The Out Put is shown below. For any method if pre-exit and post-exit method exists and the program calls the method, the pre-exit method is executed first and then the original method is called and then the post exit method is called.













Step22. Now creates a over-write exit method. IF pre-exit and post-exit method exists for any method we can not create a overwrite-exit method for the same. So lets create another method 'DISP_MESSGAE'. Double click on it to implement it.












Step23. Provide some statement and activate it.












Step24. Now run the report by calling the method 'DISP_MESSGAE'.














Step25. Now the output is as shown below.
















Step26. Select the method 'DISP_MESSAGE' and form the menu 'CLASS' click on Enhance.













Step27. Select the above created enhancement implementation and click on continue button.

















Step28. Now form the EDIT menu, goto Enhancement Operations and then select Insert OverWrite-Exit Methods.




















Step29.  Now click on the Editor that inserted under Overwrite-Exit tab against the method 'DISP_MESSAGE'.













Step30. Click on YES to continue.














Step31. Provide some statement in the overwrite-exit method and activate it.






















Step32. Now run the report.















Step33. The over-write Exit method is called suppressing the original DISP_MESSAGE.