Showing posts with label OOABAP. Show all posts
Showing posts with label OOABAP. Show all posts

Thursday, January 30, 2020

Details about Class and Method with OOABAP.


OOABAP
Class: Class is one type of user-define data type with Attributes, Methods, Events, and interface for the application.
Ø  Type of Class:
1. Local Class.
2. Global Class.
·         Local Class: It can be define in an ABAP Program and can only use only with in the program.
·         Global Class: It can be define globally it can access all the ABAP program in SAP system BUT it Created T-Code: SE24.
v  How we create class and define a class.
When we create a class there is a two section
1. Definition.
2. Implementation.
ü  How to Define Class and Method.
CLASS <class name> DEFINITION.
..
END CLASS.
***************************************************
*   START                                                                                  *
***************************************************
CLASS CL_TEST DEFINITION.
             PUBLIC SECTION.
                             DATA        : LV_NAME TYPE CHAR30.
                             METHODS: SHOW_NAME.
             PROTECTED SECTION.
                             “No Declarations.
             PRIVATE SECTION.
                            “No Declarations.
END CLASS.
***************************************************
*   END                                                                                      *
***************************************************
ü  How to Implementation Class and Method.

CLASS <class name> IMPLEMENTATION.
………
END CLASS.

***************************************************
*  START                                                                                   *
***************************************************
CLASS CL_TEST IMPLEMENTATION.
              METHOD SHOW_NAME.
                         WRITE: / ‘This is the SHOW_NAME Method’.
                          WRITE: /5 LV_NAME.
              ENDMETHOS.
END CLASS.
**************************************************
*    END                                                                                    *                                               
**************************************************

Structure of Class
 Components of a Class:
1.    Attributes
·        Static Attributes
·        Instance Attributes
2.    Methods
·        Static Methods
·        Instance Methods
3.    Events
4.    Interfaces
********************************************
*REPORT USING OO ABAP                            *
********************************************
Report: YRAM_OOABAP1
======================================================
 Project:  SAP Object Oriented ABAP
 Description: Displaying the name by using OO ABAP
======================================================
REPORT yram_ooabap1.
CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
          ** Declaration of Instance Variable
    DATA: lv_name TYPE char20 VALUE.
          ** Declaration of Instance Method
    METHODS: show_name.
  PROTECTED SECTION.
         "No Declaratons
  PRIVATE SECTION.
         "No Declaratons
ENDCLASS.
*************************************************
* Class Implementation                                                               *
*************************************************

CLASS lcl_class IMPLEMENTATION.
** Implementation of method
     METHOD show_name.
             WRITE: / 'This is the SHOW_NAME method'.
             WRITE: /5 lv_name.  “Public Variable
  ENDMETHOD.
ENDCLASS.

**************************************************
* START of TREATMENT                                                                 *
**************************************************
START-OF-SELECTION.
** Declaration of Object
  DATA: obj1 TYPE REF TO lcl_class.
**Creation of the Object
  CREATE OBJECT obj1.
** Calling the method
  CALL METHOD obj1->show_name.