Tuesday, 31 May 2016

Object Orientation



Definitions of Some object Oriented Concepts ( Terms ).

  • Encapsulation :
  1. Objects restrict the visibility of their resources (attributes and methods ) to other users.
  2. Every object has an interface, which determines how other objects can interact with it.
  3. The implementation of the object is encapsulated, that is, invisible outside the object itself.

  • Classes :
  1. Class is a section of source code that contains data and provides services into a Single Unit.
  2. The data from the attributes and the services are known as methods.
  3. Encapsulation is supported through Classes.
  4. Global Class is an ABAP object which can be accessed via SAP Class Builder, T-code for SAP Class Builder is SE24.
  5. Local classes are classes that are available in ABAP programs, we can access them via ABAP editor SE38.

  • Objects :
  1. From a technical point of view, objects are run-time instances of a class.
  2. We can create any number of objects based on a single class.
  3. Each instance(object) of a class has a unique identity and its own set of values for its attributes.
  • Polymorphism :
  1. Identical ( identically-named ) methods behave differently in different classes.
  2. object-oriented programming contains constructions called interfaces. They enable you to address methods with the same name in different objects.
  3. Although the form of address is always the same, the implementation of the method is specific to a particular class.
  4. Method overwriting: Same method name with the same signature can exist in 2 or more classes.
  5. Method overloading: Inside the class, 2 or more methods can have the same but different signatures.
  • Inheritance :
  1. We can use an existing class to drive a new class. Derived classes inherit the data and methods of the superclass.
  2. However, they can overwrite existing methods, and also add new ones.
Object-Oriented Approach - key features
  • Better Programming Structure.
  • Real-world entities can be modeled very well.
  • Stress on data security and access.
  • Reduction in code redundancy.
  • Data encapsulation and abstraction.
                             more information..................


Field symbols


  • Field symbols are pointers in C.
  • They are used to store the address of variable.
  • Used to increase the performance .
  • Syntax :
                 Field symbols :  <fs> [typing].
  • Assigning  is the keyword to assign the field symbol to variable.

Ex :

Steps :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


REPORT  ZFIELDSYMBOL3.

DATA IT_MAKT TYPE  TABLE OF MAKT.

FIELD-SYMBOLS <FS> TYPE MAKT.


SELECT-OPTIONS S_MATNR FOR <FS>-MATNR.

SELECT FROM MAKT INTO TABLE IT_MAKT WHERE MATNR IN S_MATNR.


LOOP AT IT_MAKT ASSIGNING <FS>.


WRITE / <FS>-MATNR <FS>-SPRAS <FS>-MAKTX.
ENDLOOP.



Input


Output



For all entries


  • Syntax
... FOR ALL ENTRIES IN itab WHERE ... col operator itab-comp ...

  • If the addition FOR ALL ENTRIES is specified before the language element WHERE of the SELECT statement, the components comp of the internal table itab specified there can be used in sql_cond as the operands of comparisons with relational operators.
  • The specified component comp must be compatible with the column col.
  • The internal table itab can have a structured or an elementary row type. For an elementary row type, the pseudo component table_line must be specified for comp.

Note


  1. Check the for all entries table  is not empty  or not.
  2. Write all key field in selection criteria.
  3. Data type and length of for all entries matching field should be match for both the table.
Ex :

Steps

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code




REPORT  ZFORALLENTRIES.

TYPESBEGIN OF TY_SA,
       VBELN TYPE VBAK-VBELN,
       ERDAT TYPE VBAK-ERDAT,
       ERNAM TYPE VBAK-ERNAM,
       NETWR TYPE VBAK-NETWR,
       VKORG TYPE VBAK-VKORG,
       POSNR TYPE VBAP-POSNR,
       MATNR TYPE VBAP-MATNR,
       END OF TY_SA.


TYPES BEGIN OF TY_MAT,
        MATNR TYPE MAKT-MATNR,
        MAKTX TYPE MAKT-MAKTX,
        END OF TY_MAT.

TYPES BEGIN OF TY_SO,
       VBELN TYPE VBAK-VBELN,
       ERDAT TYPE VBAK-ERDAT,
       ERNAM TYPE VBAK-ERNAM,
       NETWR TYPE VBAK-NETWR,
       VKORG TYPE VBAK-VKORG,
       POSNR TYPE VBAP-POSNR,
       MATNR TYPE VBAP-MATNR,
       MAKTX TYPE MAKT-MAKTX,
      END OF TY_SO.

 DATA WA_SA TYPE TY_SA,
        IT_SA TYPE SORTED TABLE OF TY_SA WITH  NON-UNIQUE KEY ERNAM,
        WA_MAT TYPE TY_MAT,
        IT_MAT TYPE TABLE OF TY_MAT,
        WA_SO TYPE TY_SO,
        IT_SO TYPE TABLE OF TY_SO.


SELECT-OPTIONS S_VBELN FOR WA_SA-VBELN.


SELECT A~VBELN
       A~ERDAT
       A~ERNAM
       A~NETWR
       VKORG
       POSNR
       MATNR  INTO TABLE IT_SA FROM VBAK AS A INNER JOIN VBAP AS B
       ON B~VBELN A~VBELN WHERE A~VBELN IN S_VBELN  .


  IF IT_SA  IS NOT INITIAL.

    SELECT MATNR MAKTX FROM MAKT INTO TABLE IT_MAT FOR ALL ENTRIES IN IT_SA

      WHERE MATNR IT_SA-MATNR AND  SPRAS 'EN'.


  ENDIF.



LOOP AT IT_SA INTO WA_SA.
  WA_SO-VBELN WA_SA-VBELN.
  WA_SO-ERDAT  WA_SA-ERDAT.
  WA_SO-ERNAM    WA_SA-ERNAM.
  WA_SO-NETWR  WA_SA-NETWR.
  WA_SO-VKORG WA_SA-VKORG.
  WA_SO-POSNR  WA_SA-POSNR.
  WA_SO-MATNR  WA_SA-MATNR.

 CLEAR WA_MAT.
  READ TABLE   IT_MAT  INTO WA_MAT WITH  KEY MATNR WA_SA-MATNR BINARY SEARCH.
 IF SY-SUBRC 0.
  WA_SO-MAKTX WA_MAT-MAKTX.

 ENDIF.


  APPEND WA_SO TO IT_SO.


ENDLOOP.



LOOP AT IT_SO INTO WA_SO.

  WRITE / WA_SO-VBELN ,
            WA_SO-ERDAT,
            WA_SO-ERNAM,
            WA_SO-NETWR,
            WA_SO-VKORG,
            WA_SO-POSNR,
            WA_SO-MATNR,
            WA_SO-MAKTX.

ENDLOOP.

Input


Output


Left Outer Join


  • In Left Outer Join , we can fetch similar records from  2 or more tables as well as  all the records from the left hand side table.
  • Keyword : Left Outer Join
  • Suppose  TABA and TABB are two tables, then
           



Ex :

Steps:

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.


Source Code


TYPES BEGIN OF T_STR,
        VBELN TYPE VBAP-VBELN,
        POSNR TYPE VBAP-POSNR,
        MATNR TYPE VBAP-MATNR,
        MAKTX TYPE MAKT-MAKTX,
      END OF T_STR.

DATA ITAB TYPE TABLE OF T_STR,
       WA TYPE T_STR.


SELECT-OPTIONS P_VBELN FOR WA-VBELN.

SELECT VBELN
       POSNR
       A~MATNR
       MAKTX
       INTO TABLE ITAB
       FROM VBAP AS A LEFT OUTER JOIN  MAKT AS ON B~MATNR A~MATNR
       WHERE VBELN in P_VBELN .

 LOOP AT ITAB INTO WA.
   WRITE / WA-VBELN WA-POSNR WA-MATNR WA-MAKTX.
 ENDLOOP.





Save -> Check->Activate ->Execute.

Input


Output




Inner Join


  • With an inner join, you only get the records of the cross-product for which there is an entry in all tables used in the view.
  • Keyword  : Inner Join .
  • Suppose  TABA and TABB are two tables, then

  •  Inner join result will be

   
Ex :

Steps

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


TYPES BEGIN OF T_STR,
        VBELN TYPE VBAP-VBELN,
        POSNR TYPE VBAP-POSNR,
        MATNR TYPE VBAP-MATNR,
        MAKTX TYPE MAKT-MAKTX,
      END OF T_STR.

DATA ITAB TYPE TABLE OF T_STR,
       WA TYPE T_STR.


SELECT-OPTIONS P_VBELN FOR WA-VBELN.

SELECT VBELN
       POSNR
       A~MATNR
       MAKTX
       INTO TABLE ITAB
       FROM VBAP AS A INNER JOIN MAKT AS ON B~MATNR A~MATNR
       WHERE VBELN in P_VBELN AND SPRAS 'EN'.

 LOOP AT ITAB INTO WA.
   WRITE / WA-VBELN WA-POSNR WA-MATNR WA-MAKTX.
 ENDLOOP.






Save -> Check -> Activate -> Execute.

Input



Output





Internal Table with header Line




  • Syntax 

         DATA: <Internal table name> like <table structure/ structure name > occurs 0 with header line.
  

                                               OR
   

        DATA : <Internal table name> like standard table of <structure name> with header line.
         

Ex 1 :


Data: itab like mara occurs 0 with header line.
or
Data :itab like standard table of ekko with header line.

Ex 2:

Steps :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>.
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code



TYPES BEGIN OF TY_MAT,
        NAME(12TYPE C,
        AGE TYPE I,
        PLACE(20TYPE C,
        END OF TY_MAT.

DATAIT_MAT TYPE TABLE OF TY_MAT WITH HEADER LINE.


IT_MAT-NAME 'MANI'.
IT_MAT-AGE  23.
IT_MAT-PLACE 'BANGALORE'.

APPEND IT_MAT .

CLEAR IT_MAT.

IT_MAT-NAME 'RAHUL'.

IT_MAT-PLACE 'CHENAI'.

APPEND IT_MAT .

CLEAR IT_MAT.



LOOP AT IT_MAT .

  WRITE / IT_MAT-NAME ,IT_MAT-AGE ,IT_MAT-PLACE.

ENDLOOP.




Save ->check->activate->execute.

Output



Ex 3 :

Steps :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>.
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


TYPES BEGIN OF TY_MAT,
        MATNR TYPE MAKT-MATNR,
        SPRAS TYPE MAKT-SPRAS,
        MAKTX TYPE MAKT-MAKTX,
        END OF TY_MAT.

DATAIT_MAT TYPE TABLE OF TY_MAT WITH HEADER LINE.


SELECT-OPTIONS S_MATNR FOR IT_MAT-MATNR.


SELECT MATNR SPRAS MAKTX FROM MAKT INTO TABLE IT_MAT
  WHERE MATNR IN  S_MATNR.


LOOP AT IT_MAT .

  WRITE / IT_MAT-MATNR IT_MAT-SPRAS IT_MAT-MAKTX.

ENDLOOP.




Save -> Check -> Activate-> Execute.


Input


Output




Ex 4:


Steps :


  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>.
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


DATA BEGIN OF TY_VBAK OCCURS 0,
        VBELN TYPE VBAK-VBELN,
        ERDAT TYPE VBAK-ERDAT,
        ERNAM TYPE VBAK-ERNAM,
        NETWR TYPE  VBAK-NETWR,
       END OF TY_VBAK.

SELECT-OPTIONS S_VBELN FOR TY_VBAK-VBELN.


SELECT VBELN
       ERDAT
       ERNAM
       NETWR FROM VBAK INTO TABLE TY_VBAK[] WHERE VBELN IN S_VBELN.


  LOOP AT TY_VBAK  .

    WRITE / TY_VBAK-VBELN TY_VBAK-ERDAT TY_VBAK-ERNAM TY_VBAK-NETWR.

  ENDLOOP.




Input


Output




Monday, 30 May 2016

Corresponding fields of Table

Corresponding fields of Table

  • Corresponding fields of table keyword used to place the selected  fields from database into corresponding fields of Internal Table.
  • Keyword : Corresponding fields of  Table.

Ex :

Steps

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code

TYPES BEGIN OF TY_VBRP,
        VBELN TYPE VBRP-VBELN,
        POSNR TYPE  VBRP-POSNR,
        MATNR TYPE VBRP-MATNR,
        ARKTX TYPE VBRP-ARKTX,
     END  OF TY_VBRP.

DATA WA TYPE TY_VBRP,
       IT TYPE TABLE OF TY_VBRP.

SELECT-OPTIONS S_VBELN FOR WA-VBELN.

SELECT *  FROM VBRP INTO CORRESPONDING FIELDS OF TABLE IT WHERE   VBELN IN S_VBELN.

  LOOP AT IT INTO WA.

    WRITE / WA-VBELN WA-POSNR WA-MATNR WA-ARKTX.

  ENDLOOP.




Input



Output







While .. Endwhile

While .. Endwhile


Ex :

Steps :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


 DATACOUNT  TYPE I.

PARAMETERS NUM TYPE I.

COUNT  1.

WHILE COUNT  <= NUM .
WRITE COUNT .
COUNT COUNT 1.
ENDWHILE.



Input



Output



Do...EndDo

Do...EndDo

Ex :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


Data :  count type I.
    Parameters num type I.
    count =  1.

    Do num times.
       write /  count.
      count count 1.
     enddo.



Input


Output




Case...EndCase

Case...EndCase


Ex :

  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.


Source Code



PARAMETERS color TYPE I.


CASE COLOR.
  WHEN '1'.
     WRITE 'COLOR 1 ' COLOR 1.
  WHEN '2'.
     WRITE 'COLOR 2 ' COLOR 2.

  WHEN '3'.
     WRITE 'COLOR 3 ' COLOR 3.

  WHEN '4'.
     WRITE 'COLOR 4 ' COLOR 4.

 WHEN '5'.
     WRITE 'COLOR 5 ' COLOR 5.

  WHEN '6'.
     WRITE 'COLOR 6 ' COLOR 6.

 WHEN '7'.
     WRITE 'COLOR 7 ' COLOR 7.
  WHEN OTHERS.

    WRITE 'ENTER COLORS WITHIN 7'.
ENDCASE.




Input


Output






If..Elseif..Endif

If..Elseif..Endif


Ex :

Steps :


  1. Go to transaction code SE38.
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.



Source Code

PARAMETERS color TYPE I.

IF COLOR 1.

 WRITE 'COLOR 1 ' COLOR 1.

 ELSEIF COLOR 2.
  WRITE 'COLOR 2 ' COLOR 2.

   ELSEIF COLOR 3.
  WRITE 'COLOR 3 ' COLOR 3.

   ELSEIF COLOR 4.
  WRITE 'COLOR 4 ' COLOR 4.

   ELSEIF COLOR 5.
  WRITE 'COLOR 5 ' COLOR 5.

   ELSEIF COLOR 6.
  WRITE 'COLOR 6 ' COLOR 6.

   ELSEIF COLOR 7.
  WRITE 'COLOR 7 ' COLOR 7.
ENDIF.




Input



Output



If..Else..Endif

If..Else..Endif


Ex :
  1. Go to transaction code SE38. 
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


PARAMETERS SAL TYPE I.

IF  SAL > 25000.

  WRITE 'software engineer'.

 else.

   WRITE 'IT support engineer'.

 ENDIF.




Input :



Output




Input




Output





If … endif

If … endif

Ex :

  1. Go to transaction code SE38. 
  2. Enter the program name Z or Y<program name>
  3. Select attribute type as Executable program.
  4. Click on Save.

Source Code


PARAMETERS SAL TYPE I.

IF  SAL > 25000.

  WRITE 'software engineer'.

ENDIF.





Save -> Check ->actvate -> Execute.

Input



Execute.

Output