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

Saturday, August 15, 2020

Constructor Execution in sap abap (Class 17)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL17
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL17.
CLASS LCL_SUPER DEFINITION.
  PUBLIC SECTION.
  METHODS constructor.
  CLASS-METHODS class_constructor.

  ENDCLASS.

class lcl_super IMPLEMENTATION.
  METHOD constructor.
  WRITE 'Inside the Instance constructor of class lcl_super ....'.
  endmethod.

  METHOD class_constructor.
  WRITE 'Inside the static constructor of class lcl_super ....'.
  endmethod.
  ENDCLASS.

CLASS lcl_child DEFINITION INHERITING FROM LCL_SUPER .
  PUBLIC SECTION.
      class-methods class_constructor.


  ENDCLASS.

 CLASS LCL_CHILD IMPLEMENTATION.
     METHOD class_constructor.
  WRITE 'Inside the static constructor of class lcl_child ....'.
  endmethod.

   ENDCLASS.

  START-OF-SELECTION.

 uline.
  FORMAT COLOR 3.
  data ob1 type REF TO lcl_child.
  create OBJECT ob1.

  uline.
  FORMAT COLOR 7.
  data ob2 TYPE REF TO lcl_child.
  create OBJECT ob2.
  FORMAT COLOR 2.
  ULINE.
  FORMAT COLOR 5.

  data ob3 TYPE REF TO lcl_super.
  create OBJECT ob3.

 

 

 

Thursday, August 13, 2020

Method Over riding in local class (CLASS 16)

  Sample code to show method over riding in local class in case of instance , static and instance method which is declared final.

 

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL16
*& Method Overriding (Local Classes)
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL16.
CLASS ZLCL_ABC DEFINITION.
  PUBLIC SECTION.
  METHODS M1"instance method
  METHODS M2 FINAL"Declared final cannot be inherited
    CLASS-METHODS m3 .  "static metho

  ENDCLASS.

CLASS ZLCL_ABC IMPLEMENTATION.
  METHOD M1.
    WRITE :'Inside the instance method m1 of  super class zlcl_abc...'.
  ENDMETHOD.

  METHOD m2.
    WRITE 'Inside the instance method m2 final of super class zlcl_abc...so it cannot be redefined in sub class'.
 ENDMETHOD.

METHOD m3.
  WRITE 'Inside the static method m3 of  super class zlcl_abc.....so it cannot be redefined in sub class'.
ENDMETHOD.

ENDCLASS.

CLASS zlcl_xyz DEFINITION INHERITING FROM zlcl_abc.
  PUBLIC SECTION.
  METHODS M1 REDEFINITION.
*  METHODS M2." REDEFINITION. "error m2 cannot be re-defined as it's final method
*  CLASS-METHODS M3 REDEFINITION. "error you can redefine static method

  ENDCLASS.

  CLASS zlcl_xyz IMPLEMENTATION.
    METHOD m1.
            call METHOD super->m1)"calling the super class methods (not mandatory)
      WRITE 'Inside the instance method m1 of sub class zlcl_xyz ...'.


      ENDMETHOD.

    ENDCLASS.

    START-OF-SELECTION.
    DATA OB TYPE REF TO ZLCL_XYZ .
    CREATE OBJECT OB.
    CALL METHOD OB->M1).
    CALL  METHOD OB->M2).
    CALL METHOD OB->M3).
 

 

Output :

 

Tuesday, August 11, 2020

Method Overloading (Not Supported in OOABAP)(CLASS 15)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL15
*&Method Overloading (Not Supported in OOABAP)
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL15.
class lcl_ABC1 DEFINITION.
  public SECTION.
    methods ABC.
    methods ABC IMPORTING type i.
    methods ABC importing type c.
    methods ABC IMPORTING type type c.
endclass.
 

 

 

 

Inheritance – Local Classes (Class 14)

  REPORT ZOOABAP_CL13.
*class lcl_vehicle DEFINITION final. "cannot be inherited
class lcl_vehicle DEFINITION.
  public SECTION.
    methods display.
  protected SECTION.
    data wheels type i,
           brakes type i.
endclass.

class lcl_vehicle IMPLEMENTATION.
  method display.
    write :/ wheels,brakes.
  endmethod.
endclass.

class lcl_cycle DEFINITION INHERITING FROM lcl_vehicle.
   PUBLIC SECTION.
     methods setcycle.
   PROTECTED SECTION.
      data color type string.
endclass.

class lcl_cycle IMPLEMENTATION.
  method setcycle.
    wheels 2.
    brakes 2.
    color 'Green'.
    write :'Color of cycle is ',color.
  endmethod.
endclass.

class lcl_bike DEFINITION INHERITING FROM lcl_cycle.
  PUBLIC SECTION.
    methods setbike.
  PROTECTED SECTION.
    data gears type i.
endclass.

class lcl_bike IMPLEMENTATION.
  method setbike.
    wheels 2.
    brakes 1.
    color 'Red'.
    gears 5.
    write :'color of bike :',color,
           / 'Gears in bike :',gears.
  endmethod.
endclass.

START-OF-SELECTION.
data ob1 type ref to lcl_cycle.
create object ob1.

format color 3.
call method ob1->setcycle,
              ob1->display.

uline.
format color 7.
data ob2 type ref to lcl_bike.
create object ob2.

call method ob2->setbike,
              ob2->display.
 

Inheritance using Global class in SE24 (class 13)

 Inheritance using Global class in SE24 (class 13)

Create a super class.

 
Its attributes 
 
Create method Display to display the data.

 
 
 

 

Now creating another class and inherit the super class zcl_vehicle class and don't make it final if you want to inherit the child class to another class.


Please change the data type of colors attribute to char10.


Now create another class ZCL_BIKE ,if you want to inherit the class zcl_cycle methods.







*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL14
*& For accessing above Global Inherited classes
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL14.

DATA OB1 TYPE REF TO ZCL_CYCLE.
CREATE OBJECT OB1.
FORMAT COLOR 3.

CALL METHOD OB1->SETCYCLE.
CALL METHOD OB1->DISPLAY.

ULINE.

FORMAT COLOR 7.

DATA OB2 TYPE REF TO ZCL_BIKE.
CREATE OBJECT OB2.
FORMAT COLOR 3.
CALL METHOD OB2->SETBIKE.
CALL METHOD OB2->DISPLAY.
 

 

 
 


Saturday, August 8, 2020

Instance Vs Static Constructor Execution (CLASS 12)

            Instance Vs Static Constructor Execution (CLASS 12):

 Instance constructor are invoked whenever object of class is created whereas static constructor is invoked only once during the entire class execution.

Constructors are basically used for assigning default values ,similar to INITILIZATION event in reports.

 

 *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL12
*& Instance Vs Static Constructor Execution
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl12.
CLASS zlcl_abc DEFINITION.
  PUBLIC SECTION .
    METHODS constructor "instance constructor
    CLASS-METHODS class_constructor"static constructors
    CLASS-DATA    TYPE i"Static attribute



ENDCLASS.

CLASS zlcl_abc IMPLEMENTATION.
  METHOD constructor .
    WRITE :'inside the instance constructor..........'.
  ENDMETHOD.
  METHOD class_constructor.
  WRITE :'inside the static   constructor..........'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  FORMAT COLOR 3.
  ULINE.

  WRITE 'Using First Object ob1....'.
  DATA ob1 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob1.

  ULINE.
  FORMAT COLOR 7.
  WRITE  'Using the Second Object OB2'.
  DATA ob2 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob2.

  ULINE.

  FORMAT COLOR 4.
  WRITE 'Using the Third Object OB3'.
  DATAob3 TYPE REF TO zlcl_abc.
  CREATE OBJECT ob3.

 

 

Parameters to instance constructors in abap (class 11)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL11
*& Parameters to Instance Constructors
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL11.
CLASS LCL_CUSTOMER DEFINITION.
  PUBLIC SECTION.
  METHODS CONSTRUCTOR IMPORTING IM_KUNNR TYPE KUNNR IM_NAME TYPE KNA1-NAME1,
            DISPLAY .
  PROTECTED SECTION.
  DATA KUNNR TYPE KUNNR,
         NAME1 TYPE NAME1.

  ENDCLASS.
CLASS LCL_CUSTOMER IMPLEMENTATION.
 METHOD DISPLAY.
   WRITE :/ KUNNRNAME1.
   ENDMETHOD.


  METHOD CONSTRUCTOR.
    KUNNR IM_KUNNR.
    NAME1 IM_NAME.

    ENDMETHOD.

  ENDCLASS.

PARAMETERS P_KUNNR TYPE KUNNR.

START-OF-SELECTION.
DATA OB TYPE REF TO LCL_CUSTOMER.


SELECT SINGLE NAME1 FROM KNA1 INTO @DATA(LV_NAMEWHERE KUNNR @P_KUNNR .
  IF SY-SUBRC IS INITIAL.
    CREATE OBJECT OB
    EXPORTING
      IM_KUNNR P_KUNNR
      IM_NAME LV_NAME.

  ELSE.
    MESSAGE' Customer not found' type 'I'.
    ENDIF.

 


 

Instance Constructor initializing attributes of class (CLASS 10)

Sample code for instance constructor.  

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL10
*& Insiatilize the instance constructor and it will automatically called whenever
*& object is created
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL10.
CLASS ZLCL_CUSTOMER DEFINITION.
  PUBLIC SECTION.
  METHODS CONSTRUCTOR,
            DISPLAY.
  PROTECTED SECTION.
  DATA KUNNR TYPE KUNNR,
         STCD1 TYPE STCD1.

ENDCLASS.

CLASS ZLCL_CUSTOMER IMPLEMENTATION.
  METHODDISPLAY.
    WRITE:/ KUNNR,
           STCD1.
  ENDMETHOD.
  METHOD CONSTRUCTOR.
    KUNNR 1209000.
    STCD1 'NOIDA'.
    ENDMETHOD.

 ENDCLASS.

 START-OF-SELECTION.
 DATA OB TYPE REF TO ZLCL_CUSTOMER.
 CREATE OBJECT OB.
 CALL METHOD OB->DISPLAY).

 

 

 

Global classes (Types, Attributes, Methods) & accessing global class in report (class 9)

  

 i created a class in se24 and create a type structure for table and i have  used code based rather than form based.

and i declared a table of ty_vbak , which when we see in form based on attribute tab , we will see T_VBAK table in it and protected type .

and now creating methods :

i have created 2 methods : 1 for fetching data and other for displaying data.

get_data method : which instance and should be public 

display : which is instance and should be protected.

 now coming to the parameters of get_data methods.

 code for get_data.

 

 

 

 now write logic to display data in display method.

 

 Activate it  and ready to use it in reports.

 

 


 

 Create a report in se38 .

 *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL9
*& Calling Global class from local report
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL9"report name 

DATA OB TYPE REF TO ZOOABAP_CL9."CLASS NAME CREATED VIA SE24
CREATE OBJECT OB.

PARAMETERS P_KUNNR TYPE KUNNR
CALL METHOD ob->get_data "method 
  EXPORTING
    im_kunnr P_KUNNR  
    .

Friday, August 7, 2020

Friend classes (Local classes) Concept (Class 8)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL8
*& Friend Class Concept
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl8.
CLASS zlcl_emp DEFINITION DEFERRED"Definition deffered or we can forward declaration
CLASS zlcl_customer DEFINITION  FRIENDS zlcl_emp.
  PUBLIC SECTION.
    METHODS m1.
  PROTECTED SECTION.
    METHODS m2.
  PRIVATE SECTION .
    METHODS m3.
ENDCLASS.

CLASS zlcl_customer IMPLEMENTATION.
  METHOD m1.
    WRITE /'Inside the PUBLIC Method  M1 of class zlcl_customer.........'.
  ENDMETHOD.

  METHOD m2.
    WRITE /'Inside the PROTECTED Method M2 of class Zlcl_customer.........'.
  ENDMETHOD.

  METHOD m3.
    WRITE /'Inside the PRIVATE Method M3 of class zlcl_customer.........'.

  ENDMETHOD.
ENDCLASS.
CLASS zlcl_emp DEFINITION.
  PUBLIC SECTION.
    METHODS m4.
ENDCLASS.

CLASS zlcl_emp IMPLEMENTATION.
  METHOD m4.
    WRITE /'Inside the Public Method of M4 of class Zlcl_Emp......'.
*--------Now creating object : for class ZLCL_CUSTOMER inside zlcl_emp-------------*
    DATA ob TYPE REF TO zlcl_customer.
    CREATE OBJECT ob .
    CALL METHOD ob->m1"accessing public class method
    CALL METHOD ob->m2"accessing protected class method
    CALL METHOD ob->m3"accessing private class method
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA:  ob1 TYPE REF TO zlcl_emp.
  CREATE OBJECT ob1.
  CALL METHOD ob1->m4."calling friend class method

 

 

Returning Parameters in Methods (CLASS 7)

  *&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL7
*&
*&---------------------------------------------------------------------*
*& use of Return statement : limitation over exporting
*& if returning is used then no export/changing is allowed
*& only one returning is allowed
*&---------------------------------------------------------------------*
REPORT zooabap_cl7.
CLASS zlcl_customer DEFINITION.
  PUBLIC SECTION.
    METHODS get_data IMPORTING im_kunnr       TYPE kunnr
                       RETURNING VALUE(ex_nameTYPE kna1-name1"returning parameter

ENDCLASS.

CLASS zlcl_customer IMPLEMENTATION.
  METHOD get_data.
    SELECT SINGLE name1 FROM kna1 INTO (ex_nameWHERE kunnr im_kunnr.

  ENDMETHOD.

ENDCLASS.
PARAMETERS p_kunnr TYPE kunnr.
DATA v_name1 TYPE name1.

START-OF-SELECTION.

  DATA ob TYPE REF TO zlcl_customer.
  CREATE OBJECT ob.


  CALL METHOD ob->get_data
    EXPORTING
      im_kunnr p_kunnr.

  WRITE /'Customer Name .......',v_name1.

  CLEAR v_name1.
  CALL METHOD ob->get_data
    EXPORTING
      im_kunnr p_kunnr
    RECEIVING
      ex_name  v_name1.

  WRITE /'Customer Name .......',v_name1.
  ULINE.

  CLEAR v_name1.

  v_name1 ob->get_dataEXPORTING im_kunnr p_kunnr ).

  WRITE 'Customer Name.....',v_name1.

 


 

Instance Vs Static Methods (Class 6)

Sample code to show how instance and static method access :  

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL6
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZOOABAP_CL6.
CLASS LCL_EMP DEFINITION.
  PUBLIC SECTION.
  METHODS M1"INSTANCE METHOD
  CLASS-METHODS M2" STATTIC METHOD
  PROTECTED SECTION.
  DATA TYPE I.  "INSTANCE ATTRIBUTE
  CLASS-DATA TYPE I"STATIC ATTRIBUTE
  CONSTANTS TYPE VALUE 10."CONSTANT ATTRIBUTE
  TYPES :D1 TYPE I.
  ENDCLASS.
 CLASS LCL_EMP IMPLEMENTATION.
   METHOD M1.
     FORMAT COLOR 3.
    write :'inside instance method m1...'.
    data v_d type d1.
    write :/ a,b,c,v_d.
   format color off.
  endmethod.

  METHOD m2.
    FORMAT COLOR 6.
    write /'Inside Static Method M2.....'.
    DATA V_D1 TYPE D1.
    WRITE /  BCV_D1"IN STATIC METHOD WE ONLY ACCESS STATIC , CONSTANTS BUT NOT INSTANCE
    FORMAT COLOR OFF.
    ENDMETHOD.

   ENDCLASS.


   START-OF-SELECTION.
   LCL_EMP=>M2).

   DATA OB TYPE REF TO LCL_EMP.
   WRITE :/'Acessing both static method and instance method via object'.
   CREATE OBJECT OB .
   cALL METHOD :OB->M1)
 ob->m2).

 

 

Thursday, August 6, 2020

Local class Methods with Importing and Exporting Parameters.(Class 5)

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL5
*& Local class Methods with Importing and Exporting Parameters
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl5.
CLASS LCL_CUSTOMER DEFINITION.
  PUBLIC SECTION.
  METHODS GET_DETAILS IMPORTING IM_KUNNR TYPE KUNNR
                                  EXPORTING EX_NAME TYPE NAME1
                                            EX_STCD1 TYPE STCD1.


  ENDCLASS.

CLASS LCL_CUSTOMER IMPLEMENTATION.
  METHOD GET_DETAILS.
    SELECT SINGLE NAME1  STCD1 
                  FROM KNA1 INTO (EX_NAME,EX_STCD1)
                  WHERE KUNNR IM_KUNNR.
    ENDMETHOD.
  ENDCLASS.
PARAMETERS p_kunnr TYPE kna1-kunnr.
START-OF-SELECTION.
dataob type ref to lcl_customer.

create object ob.

DATA v_name1 TYPE name1,
       v_stcd1 TYPE stcd1.


*---------Case 1  : when exporting nothing --------------------*
format color 3.
write :'Customer name :',v_name1,
       / 'Customer city :',v_stcd1.
format color off.

uline.


call METHOD ob->get_details
exporting
  im_kunnr p_kunnr.
*----------case 2 : when exporting just name1 attribute---------*

call method ob->GET_DETAILS
EXPORTING
  im_kunnr p_kunnr
IMPORTING
  ex_name v_name1.

format color 7.
write :'Customer name :',v_name1,
       / 'Customer city :',v_STCD1.
format color off.

uline.

*---------Case 3 : when you want to export both the attribute-------*
call method ob->get_details
EXPORTING
  im_kunnr p_kunnr
IMPORTING
  ex_name v_name1
  ex_stcd1 v_stcd1.

format color 1.
write :'Customer name :',v_name1,
       / 'Customer city :',v_stcd1.
format color off.


O/P: Image of Output.

Tuesday, August 4, 2020

Calling a Protected method from a Public Method (class 4)

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL4
*& Calling protected method from public method 
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl4.
CLASS lcl_emp DEFINITION .
  PUBLIC SECTION.
    METHODS set.
  PROTECTED SECTION.
    DATA empno   TYPE char10,
           empname TYPE char40,
           design  TYPE char10.
    METHODS display .

ENDCLASS.

CLASS lcl_emp IMPLEMENTATION.

  METHOD display.
    WRITE / empno,
             empname,
             design.
  ENDMETHOD.
  METHOD  set.
    empno 127.
    eMPname 'SOURAV'.
    DESIGN 'CEO'.
    CALL METHOD me->display).
  ENDMETHOD.
ENDCLASS.

DATA :ob1 TYPE REF TO lcl_emp.
start-of-selection.
  CREATE OBJECT ob1.
  OB1->SET).
 

  

Monday, August 3, 2020

Local class with Attributes and Methods, ME Keyword (Class 3)

*&---------------------------------------------------------------------*
*& Report  ZOOABAP_CL3
*& Test : Report to show execution and use of ME variable
*& Local class with Attributes and Methods, ME Keyword
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zooabap_cl3.
CLASS lcl_emp DEFINITION.
  PUBLIC SECTION .
    METHODS set,
      display.
  PROTECTED SECTION.
    DATA empno   TYPE i,
           empname TYPE char40,
           design  TYPE char10.


ENDCLASS.

CLASS lcl_emp IMPLEMENTATION.
  METHOD display.
    WRITE / empno,empname,design.
  ENDMETHOD.

  METHOD set .
    empno '127'.
    empname 'SOURAV'.
    design 'CEO'.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  DATA ob1 TYPE REF TO lcl_emp.
  CREATE OBJECT ob1.
  ob1->display).
  ob1->set).
  ob1->display).

  ULINE.
  DATA ob2 TYPE REF TO lcl_emp.
  CREATE OBJECT ob2.
  WRITE :'Values of object ob2...'.
  CALL METHOD ob2->display.