Header Ads

ABAP programming basics 1

Good thing about ABAP is that you dont need a seperate IDE  or to cofigure 100s of things like what we need to do for Java or .Net for development  as the coding is done directly in the SAP environment . But this is an issue as well  because sometimes getting hold of SAP system is difficult . 
-> you can download trails version from the below link . 
http://www.sdn.sap.com/irj/scn/nw-downloads
As we studies earlier in the blog  https://sapcrmtutorial.blogspot.com/p/basics-of-ooabap.html The Object Oriented Programming has three major Components . 
  • Polymorphism
  • Encapsulation
  • Inheritance
Welcome to the world of SAP




Polymorphism: In simple terms when you over write some functionality it’s called polymorphism. In polymorphism, you can inherit methods from the parent class and can modify it by implementing it again (which is nothing but implementation of the inherited method).
We will see now how we can achieve this in ABAP ;
1. Go to transaction SE24 and build a class and name whatever like this one: ZCL_POLYMORPHISM_SUPER
1.JPG
Please remeber not to check the Final Checkbox. If you check this check box, any other class or sub-class cannot inherit from this class.
2. Go to Methods Tab and create a method under there:
2.JPG
3. Put cursor on Select_Method method and press parameters button to enter parameters as shown:
3.JPG
4. Then click on the Methods with back arrow button and double click on select_method and enter the required code as shown:
4.JPG
5. Put cursor on Display_Method method and press parameters button to enter parameters as shown:
5.JPG
6. Then go back to Methods by pressing the methods button, double click on the Display_Method and enter the required code as shown:
6.JPG
We are done creating the Super Class. Now let’s go and create the Sub-Class.
Go to t-code se24 and create the class: ZCL_POLYMORPHISM_SUB
Go to properties tab and press the SuperClass button and define the super class name we created:
7.JPG   
When we save the class then the methods along with parameters and logic are inherited in the sub class where the color of the inherited methods changes to blue color. We cannot change the methods,parameters and attributes of inherited class.
To redefine the existing functionality of the inherited methdod, we will put cursor on that method and press the Redifne button as shown:
8.JPG
Enhance the existing method by adding any write statement or anything like shown:
   CALL METHOD SUPER->SELECT_METHOD
  EXPORTING
    P_CARRID   =  P_CARRID
  IMPORTING
    WA_SFLIGHT = WA_SFLIGHT     .
select single from sflights into wa_sflights where carrid = p_carrid.
9.JPG
Create another method display_method1 with the exporting parameter wa_sflights, then double click on the method and implement the logic.
10.JPG
Create a program in se38 and provide the logic:
*Provide Object for Sub Class
data:obj1 type ref to ZCL_POLYMORPHISM_SUB.
*Provide Parameters
parameters: v_carrid type sflight-carrid.
*Provide Data Objects
data: wa_sflight type sflight.*     Wa1_sflights type sflights.
*Create Object instanceCREATE OBJECT OBJ1.
CALL METHOD obj1->SELECT_METHOD
  
EXPORTING
    P_CARRID   = v_carrid
  
IMPORTING
    WA_SFLIGHT = wa_sflight
    .
write:/ wa_sflight-CARRID,
        wa_sflight-CONNID.
WRITE: / obj1->wa_sflights-CARRID.
We are done with Polymorphism here.
Lets go and see what is Encapsulation.
Encapsulation : Wrapping up of data into single unit. Or, restriction on visibility of attributes and methods in the class. We have 3 levels of visibility:
     1. Private
     2. Protected
     3. Public
Methods or attributes defined as private are only visible and available to the class in which they are defined.
Methods or attributes defined as protected are visible to the class defined in and to the class which inherits from the class they are defined in.
Similarly the methods or attributes defined as public are available to all.
Lets start with an example again:
SE24 -> Create a Super class with name: ZCL_ENCAPSULATION
1.JPG
2.JPG
Create a Subclass ZCL_ENCAPSULATION_SUB by assigning super class
3.JPG
Now if you go and look in the attribute section, you will only be able to see the Public and Protected attribute. Private attaribute defined above is not visible as shown:
4.JPG
Similarly you will be able to see the methods of the super class but only the one which were declared as public and protected as shown:
5.JPG
THIS IS JUST A HIGH LEVEL VIEW ON HOW WE CAN SEE THE THINGS IN OO ABAP. DETAILED LEVEL WILL BE EXPLAINED LATER.
Now as we are done with Encapsulation, lets move on to Inheritance.
Inheritance: This can be defined from the word itself. That is to inherit properties from some parent class. Anything inherited will only be cisible if that is declared as public or protected in the super class.
Normally the OBJECT ORIENTED ABAp does not support the many to one inheritance, but this is made possible by using interfaces.
Interface is also a kind of class which contain the definitions only. Implementation of those defined methods will take part in the deriving classes only.
Lets goto SE24 and create and interface with name ZIF_INTERFACE.
Create a method SELECT_METHOD and define parameters for it as shown:
6.JPG
Now we will create a class named ZCL_INTERFACE and in the Interface tab of that class will put our interface name as shown:
7.JPG
The method will automatically be generated in the Methods tab as shown:
8.JPG
Double click on this method and write some logic like:
9.JPG
Now lets create an ABAP program and write the following code in that just to understand;
  DATA:sflight TYPE sflight,
     wa_sflight TYPE sflight,
     it_sflight TYPE z_sflight.
*providing data objects
DATA:obj TYPE REF TO zcl_interface.
*providing selection-screen
SELECT-OPTIONS:s_carrid FOR sflight-carrid.
START-OF-SELECTION.
  CREATE OBJECT obj.
  CALL METHOD obj->zif_interface~select_method
    EXPORTING
      p_carrid_low  = s_carrid-low
      p_carrid_high = s_carrid-high
    IMPORTING
      wa_sflight    = wa_sflight
      it_sflight    = it_sflight.
  DELETE ADJACENT DUPLICATES FROM it_sflight COMPARING carrid.
  LOOP AT it_sflight INTO wa_sflight.
    WRITE:/ wa_sflight-carrid,wa_sflight-connid,sflight-seatsmax.
  ENDLOOP.
The output comes as
10.JPG
So this is all, If I get some time, i will come up with one more document explaining something else.
Powered by Blogger.