Small Document on Concepts of OO ABAP for Beginners
First of all i have also started learning the OO ABAPS a week ago and thought it would be beneficial for the beginners to understand the basic concepts of it. Also help.SAP.com provides very good articles and explanation on the topic, so please refer the following link:
The Object Oriented Programming has three major Component around which i revolves. They are:
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;
1. Go to transaction SE24 and build a class and name whatever like this one: ZCL_POLYMORPHISM_SUPER
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:
3. Put cursor on
Select_Method method and press parameters button to enter parameters as shown:
4. Then click on the Methods with back arrow button and double click on select_method and enter the required code as shown:
Put cursor on Display_Method method and press parameters button to enter parameters as shown:
6. Then go back to Methods by pressing the methods button, double click on the Display_Method and enter the required code as shown:
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:
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:
Enhance the existing method by adding any write statement or anything like shown:
CALL METHOD SUPER->SELECT_METHOD
P_CARRID = P_CARRID
WA_SFLIGHT = WA_SFLIGHT
select single * from sflights into wa_sflights where carrid = p_carrid.
Create another method display_method1 with the exporting parameter wa_sflights, then double click on the method and implement the logic.
Create a program in se38 and provide the logic:
*Provide Object for Sub Class
data:obj1 type ref to ZCL_POLYMORPHISM_SUB.
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
P_CARRID = v_carrid
WA_SFLIGHT = wa_sflight
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:
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
Create a Subclass ZCL_ENCAPSULATION_SUB by assigning super class
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:
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:
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:
Now we will create a class named ZCL_INTERFACE and in the Interface tab of that class will put our interface name as shown:
The method will automatically be generated in the Methods tab as shown:
Double click on this method and write some logic like:
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.
SELECT-OPTIONS:s_carrid FOR sflight-carrid.
CREATE OBJECT obj.
CALL METHOD obj->zif_interface~select_method
p_carrid_low = s_carrid-low
p_carrid_high = s_carrid-high
wa_sflight = wa_sflight
it_sflight = it_sflight.
DELETE ADJACENT DUPLICATES FROM it_sflight COMPARING carrid.
LOOP AT it_sflight INTO wa_sflight.
The output comes as
So this is all, If I get some time, i will come up with one more document explaining something else.
iGATE Global Sol Ltd.