Good thing nearly ABAP is that yous dont take a seperate IDE or to cofigure 100s of things similar what we demand to make for Java or .Net for development every bit the coding is done direct inwards the SAP environment . But this is an event besides because sometimes getting concur of SAP organization is hard .
Welcome to the globe of SAP
Polymorphism: In simple price when yous over write approximately functionality it’s called polymorphism. In polymorphism, y'all can inherit methods from the raise course too tin can modify it past implementing it once again (which is naught simply implementation of the inherited method).
We will come across forthwith how we tin attain this inward ABAP ;
i. Go to transaction SE24 too build a form together with bring up whatsoever similar this ane: ZCL_POLYMORPHISM_SUPER
Please remeber non to cheque the Final Checkbox. If you cheque this bank check box, any other class or sub-grade cannot inherit from this form.
ii. Go to Methods Tab too make a method under at that place:
iii. Put cursor on Select_Method method in addition to press parameters button to get in parameters as shown:
iv. Then click on the Methods alongside dorsum arrow push button too double click on select_method in addition to enter the required code equally shown:
five. Put cursor on Display_Method method too press parameters push to move into parameters equally shown:
six. Then get back to Methods past pressing the methods button, double click on the Display_Method and go in the required code as shown:
We are done creating the Super Class. Now allow’second get together with create the Sub-Class.
Go to t-code se24 together with create the form: ZCL_POLYMORPHISM_SUB
Go to properties tab in addition to press the SuperClass button together with define the super course advert nosotros created:
When we relieve the course and so the methods along alongside parameters in addition to logic are inherited inward the sub course where the colour of the inherited methods changes to bluish colour. We cannot modify the methods,parameters and attributes of inherited course.
To redefine the existing functionality of the inherited methdod, nosotros will place cursor on that method too press the Redifne button equally shown:
Enhance the existing method by adding any write argument or anything similar shown:
CALL METHOD SUPER->SELECT_METHOD EXPORTING P_CARRID = P_CARRID IMPORTING WA_SFLIGHT = WA_SFLIGHT . choose single * from sflights into wa_sflights where carrid = p_carrid.
Create another method display_method1 with the exporting parameter wa_sflights, and then double click on the method and implement the logic.
Create a programme in se38 too furnish 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
information: wa_sflight type sflight.* Wa1_sflights type sflights.
Lets become together with run across what is Encapsulation.
Encapsulation : Wrapping upward of data into single unit. Or, restriction on visibility of attributes in addition to methods in the class. We take 3 levels of visibility:
Methods or attributes defined every bit private are solely visible as well as available to the form inwards which they are defined.
Methods or attributes defined equally protected are visible to the course defined in together with to the grade which inherits from the course they are defined inward.
Similarly the methods or attributes defined equally world are available to all.
Lets start with an example again:
SE24 -> Create a Super form alongside name: ZCL_ENCAPSULATION
Create a Subclass ZCL_ENCAPSULATION_SUB by assigning super class
Now if yous become as well as expect inwards the attribute department, you lot volition only live able to meet the Public in addition to Protected attribute. Private attaribute defined above is non visible equally shown:
Similarly y'all volition live able to run across the methods of the super course just only the ane which were declared equally public together with protected every bit 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 give-and-take itself. That is to inherit properties from some raise class. Anything inherited volition exclusively live cisible if that is declared equally public or protected inward the super form.
Normally the OBJECT ORIENTED ABAp does not support the many to 1 inheritance, only this is made possible past using interfaces.
Interface is besides a sort of form which incorporate the definitions alone. Implementation of those defined methods will have function in the deriving classes only.
Lets goto SE24 and make as well as interface amongst mention ZIF_INTERFACE.
Create a method SELECT_METHOD too define parameters for it equally shown:
Now nosotros will create a course named ZCL_INTERFACE too inward the Interface tab of that grade will position our interface refer equally shown:
The method will automatically be generated in the Methods tab equally shown:
Double click on this method and write some logic similar:
Now lets make an ABAP program too write the next code inward that only to sympathize;
DATA:sflight TYPE sflight, wa_sflight TYPE sflight, it_sflight TYPE z_sflight. *providing data objects DATA:obj TYPE REF TO zcl_interface. *providing choice-screen SELECT-OPTIONS:s_carrid FOR sflight-carrid.