Escolar Documentos
Profissional Documentos
Cultura Documentos
ENDCLASS.
ENDCLASS.
OBJECTS
Objects are instances of classes.
Each object has a unique identity i.e. memory and its own attributes.
Interfaces:-
PUBLIC
PROTECTED
PRIVATE.
Public section:
Protected section:
Private Section:
Global Class: Global classes and interfaces are defined in the Class
Builder (Transaction SE24) in the ABAP Workbench.
All of the ABAP programs in an R/3 System can access the global
classes
Go to SE24
Click on create
V_name instancepublictypechar25.
.
Creating Objects
Creating instance or objects for the class:-
OBJ2v_name
.
Example program :-
.
Similarly create another object
Go to se24
Write : / sy_uline .
*Set a value using obj2 .
In the real time , we use static variables with the class name only.
Using public, private , protected variables
Steps:
Go to se24
Click on create
Go to se24.
GET_CUST_DETINSTANCEPUBLIC
.
Click on the button code. (or)
Endmethod.
select the radio button call method and provide the values as below.
Call method
Instance : OBJ1
Class : ZCL_Sample
EXPORTING
IM_KUNNR = P_KUNNR.
IMPORTING
EX_KNA1 = WA_KNA1.
WRITE : / WA_KNA1-KUNNR ,
WA_KNA1-NAME1.
SaveActivateTest it.
Methods using table types
TABLES PARAMETER:
In the methods, we dont have any option for specifying internal tables
directly.
Go to se11.
Go to se24.
METHOD GET_CUST_LIST
ENDMETHOD.
EXPORTING
IM_LAND1 = P_LAND1
IMPORTING
EX_KNA1 = I_KNA1.
LOOP AT I_KNA1 INTO WA_KNA1.
WRITE:/ WA_KNA1-KUNNR,
WA_KNA1- NAME1,
WA_KNA1-LAND1.
ENDLOOP.
USER-DEFINED TYPES IN THE CLASSES
USING USER-DEFINED TYPES IN THE CLASSES:
Define an event .
Define a method for writing business logic for the action/event raised by the
user .
Link event and method and convert the method into event-handler method.
Use set handler and register event handler method to a particular instance
in the program.
SET HANDLER:
Standard Events : Events which are defined and raised by SAP are called
Standard Events
Ex : Double_Click, Right_Click
Custom Events : Events which are defined, raised and also handled by
creating event handler methods are called Custom Events .
NO_KUNNRINSTANCEPUBLIC
Click on save.
Step2: STEPS TO DEFINE A METHOD:-
EVT_HDLER_METH instancepublic.
Click on save.
Step3: STEPS TO LIK EVENT AND METHOD:-
A pop-up is rised.
Select the checkbox event handler &provide the class name and event
name
class/interface : ZCL-SAMPLE.
Event : NO-KUNNR.
Click on the change button, which will convert the method into event
handler method.
METHOD GET_CUST_DET.
If im_kunnr is initial .
Else.
Endmethod.
Step5: Use set handler and register event handler method to a particular
instance where the event is raised..
SYNTAX FOR SET HANDLER:
Ex: Go to the program and call the method without giving any customer no.
EXPORTING
Im_kunnr = P_kunnr
IMPORTING
Ex_kna1 = wa_kna1.
Write:/wa_kna1 kunnr,wa_kna1-name1.
These methods are mainly used to set default values in a class. i.e. Used to
default values to attributes. (Ex V_Werks = 0001)
INSTANCE Constructor
STATIC Constructor
INSTANCE CONSTRUCTORS:
These are special type of methods.
Im_kunnr---->type--kna1-kunnr.
We never call the constructor method bcoz they are executed automatically whenever
an object is created.
STEPS TO CREATE OBJECT:
This parameter takes the reference of custom container as a default value to display alv.
Note:
Constructor method is called for each instance created i.E.., Create 5 instances ,the
constructor method is called 5 times.
STATIC CONSTRUCTORS
STATIC CONSTRUCTORS:
STATIC CONSTRUCTORS:
It is a type of constructor method.
This method is executed automatically whenever a first call to the class is made.
Ex: obj1--get_cust_det.
Zcl_sample--v_name2.
Static constructor are used to set the default values globally .i.e.,Irrespective of
instances.
The static constructors will not have any importing / exporting parameters .
Method class_constructor
Write : /
endmethod.
ZV_IF~get _mara.
ZV_IF~ get_KNA1.
End method.
EXPORTING
IM_LAND1 = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
Interface Methods
Method overloading
Method Overriding.
Interface Methods : The Interface Methods can be implemented in
multiple classes with same name but with diff implementation which
is called as polymorphism.
Click on save.
The method Get_material_Data will be copied automatically as it is a super
class method.
Select the method name & click on the Redefine icon
So that we can redifine super class method with extra
functionality
Now ,double click on method & write the below code.
And spras=EN.
Exporting.
Im_matnr = P_matnr.
Importing
Ex_mara = wa_mara.
*Display material master data
Write: / wa_mara-matnr,
Wa_mara-mtart,
Wa_mara-meins .
Write : / OBJwa_makt_matnr,
OBJwa_makt_spras,
OBJwa_makt_maktx
Abstract class
Abstract class
(or)
Note :- we cannot create an object to the abstract class because they are
not fully implemented, Instead create an object to the child class which
inherits the abstract class and then call the methods .
steps to define an Abstract class :-
Goto SE24
Click on Create
Go to SE24
Click on Create
ENDCLASS.
ENDCLASS.
USING STATIC AND INSTANCE COMPONENTS IN LOCAL CLASS
USING PUBLIC,PROTECTED,PRIVATE VISIBILITY:
Syntax :
Public-section .
DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS
Protected-section .
DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS
Private-section .
DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS
ENDCLASS.
USING EVENTS IN LOCAL CLASSES:
1.Syntax to Define an Event
CLASS lcl_sample DEFINITION.
PUBLIC SECTION.
EVENTS : <EVENTNAME>.
END CLASS.
Example:
2.Syntax to Define event handler method
CLASS lcl_sample DEFINITION.
METHODS : <EVENT HANDLER METH>
FOR EVENT <EVENTNAME>
OF <CLASSNAME>
END CLASS
USING INHERITANCE IN LOCAL CLASS
CLASS <superclassname> DEFINITION.
METHODS : <Methodname>
END CLASS.
We can display more than one ALV grid data on a single screen.
The ALV grid data is displayed in the form of custom container with
which we can control the size of ALV grid Whereas we cannot
control the size of the ALV with function Modules.
CL_GUI_ALV_Grid
CL_GUI_CUSTOM_CONTAINER
CL_dd_document
CL_GUI_ALV_tree_simple
CL_GUI_container
CL_GUI_splitter_container.
The most commonly used method to display ALV grid data is,
SET_TABLE_FOR_FIRST_DISPLAY.
1. OOPS ALV with Structure.
Steps for a simple OO-ALV:
STEP1: Create data declarations for ALV/custom container.
DATA : ALV_CONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA : ALV_GRID TYPE REF TO CL_GUI_ALV_GRID
DATA : I_MARA TYPE TABLE OF MARA .
DATA : WA_MARA TYPE MARA .
PERFORM GET_DATA.
PERFORM create_fcat .
PERFORM DISPLAY_ALV .
ENDMODULE.
STEP5 : Define each subroutine as previous program .
FORM create_fcat .
CLEAR wa_fcat .
wa_fcat-col_pos = '1' .
wa_fcat-fieldname = 'MATNR' .
wa_fcat-tabname = 'I_MARA' .
ENDFORM
Finally display ALV with fieldcatelog instead of structure.
FORM display_alv .
CHANGING
it_outtab = i_mara
it_fieldcatalog = i_fcat.
ENDFORM.
INTERACTIVE ALV
Introduction :
For Interactive ALVS, We have the event DOUBLE_CLICK in class CL_GUI_ALV_GRID.
For this event, we need to define a event handler method.
So, to define a event handler method, we need to define a LOCAL CLASS as below.
CLASS lcl_event_handler DEFINITION .
ENDCLASS
Once we define a LOCAL CLASS, we need to define a event handler method as below.
CLASS lcl_event_handler DEFINITION .
PUBLIC SECTION .
METHODS : handle_double_click FOR EVENT double_click
OF cl_gui_alv_grid
IMPORTING e_row.
ENDCLASS
PUBLIC SECTION .
OF cl_gui_alv_grid
IMPORTING e_row.
ENDCLASS .
METHOD handle_double_click .
ENDMTHOD.
ENDCLASS.
STEP4 : Create screen 100.
STEP4:Click on flow-logic and create the PBO Module and write below code
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM create_fcat .
PERFORM DISPLAY_ALV .
ENDMODULE.
Write the custom logic for DOUBLE_CLICK event
CLASS lcl_event_handler IMPLEMENTATION.
METHOD handle_double_click .
READ TABLE i_mara INTO wa_mara INDEX e_row-index .
SELECT * FROM makt
INTO CORRESPONDING FIELDS OF TABLE i_makt
WHERE matnr = wa_mara-matnr .
Perform create_object2.
Perform create_Fcat2
Perform Display_ALV2
FORM create_objects2 .
IF alv_cont2 is initial .
CREATE OBJECT alv_cont2
EXPORTING
container_name = 'ALV_CONT2.
CREATE OBJECT alv_grid2
EXPORTING
i_parent = alv_cont2.
Endif.
ENDFORM.
FORM create_fcat2 .
If I_FCAT IS INTIAL .
CLEAR wa_fcat2 .
wa_fcat2-col_pos = '1' .
wa_fcat2-fieldname = 'MATNR' .
wa_fcat2-tabname = 'I_MAKT' .
wa_fcat2-scrtext_m = 'Material No' .
APPEND wa_fcat2 TO i_fcat2 .
ENDIF.
ENDFORM
FORM display_alv2 .
CALL METHOD alv_grid2->set_table_for_first_display
CHANGING
it_outtab = i_makt
it_fieldcatalog = i_fcat2.
ENDFORM.
INTERACTIVE ALV WITH 2 ALV GRIDS ON
A SINGLE SCREEN
STEP1: Create data declarations for ALV/custom
container, also define object or local class.
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM CREATE_CAT1 .
PERFORM DISPLAY_ALV1 .
PERFORM CREATE_FCAT2.
PERFORM DISPLAY_ALV2.
ENDIF.
DISPLAYING LOGO & TOP OF PAGE IN OO-ALV
CL-DD-DOCUMENT : It is the class used to display logo & top-of-page.
Add - picture
New line
Add gap
Add text
Display document
Steps :
STEP1 :Upload logo into SAP document server using OAER Tcode.
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM CREATE_FCAT .
PERFORM DISP_LOGO.
PERFORM DISPLAY_ALV1 .
FORM DISP_LOGO .
EXPORTING
PICTURE_ID = 'ARJU' .
EXPORTING
WIDTH = '25' .
HEADING = CL_DD_DOCUMENT=>HEADING .
EXPORTING
SAP_STYLE = HEADING .
CALL METHOD DD->DISPLAY_DOCUMENT
EXPORTING
CONTAINER = 'LOGO.
If FINAL option is selected, we cannot create inheritance (or) the class cannot be inherited.
FRIENDS:-
By default , the child class cannot access the private variables of a SUPER class.
FRIEND is a concept which is used to access the PRIVATE variables of a SUPER class.
So, To access the PRIVATE variables of a super class, the SUPER class has to treat the
child as a friend.
To do this, go to the super class Go to FRIENDS tab Enter child class name
press Enter.
From now onwards the child class can access the PRIVATE variables of a SUPER class.
Steps for using FRIEND concept:-
PR_NAMEINSTANCEPRIVATETYPECHAR20.
Go to FRIENDS Tab.
WRITE :/ objPR_NAME.
ALIASES:-
What is an event?
Interview questions:
What is polymorphism?
What is aliases?
What is super?
What is redifination?
What is FINAL?
Interview questions:
What is a fieldcatelog?
CL_GUI_CUSTOM_CONTAINER.
CL_GUI_SPLITTER_CONTAINER.
CL_GUI_CONTAINER.
CL_GUI_ALV_GRID.
STEP4:Click on flow-logic and create the PBO Module and write below code
PERFORM CREATE_OBJECTS.
PERFORM SPLIT_MAIN_CONT.
PERFORM DISP_ALV1 .
PERFORM DISP_ALV2.
ENDMODULE.
STEP1 :
DATA : O_CC TYPE REF TO cl_gui_custom_container .
DATA : O_PART1 TYPE REF TO CL_GUI_CONTAINER.
DATA : O_PART2 TYPE REF TO CL_GUI_CONTAINER.
DATA : O_SC TYPE REF TO CL_GUI_SPLITTER_CONTAINER.
DATA : O_GRID1 TYPE REF TO CL_GUI_ALV_GRID .
DATA : O_GRID2 TYPE REF TO CL_GUI_ALV_GRID .
DATA : I_MARA TYPE TABLE OF MARA .
DATA : I_MAKT TYPE TABLE OF MAKT .
STEP2 :
START-OF-SELECTION .
CALL SCREEN 0300.
STEP3:
Design screen with custom container by name MAIN_CONT
STEP4:
Click on flow-logic and create the PBO Module and write below code
module STATUS_0300 output.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
PERFORM CREATE_OBJECTS.
PERFORM SPLIT_MAIN_CONT.
PERFORM DISP_ALV1.
PERFORM DISP_ALV2.
endmodule.
form CREATE_OBJECTS .
CREATE OBJECT o_cc
EXPORTING
container_name = 'MAIN_CONT'.
endform.
form SPLIT_MAIN_CONT .
endform.
form DISP_ALV1 .
CREATE OBJECT o_grid1
EXPORTING
i_parent = O_PART1 .
endform.
OO ALV WITH TREE
OO ALV WITH TREE
STEP4:Click on flow-logic and create the PBO Module and write below code
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM create_fcat .
PERFORM CALC_SUBTOTALS.
PERFORM DISPLAY_ALV .
ENDMODULE.
STEP1:
start-of-selection .
call screen 100.
STEP3:
STEP4:
Click on flow-logic and create the PBO Module and write below code
FORM get_data .
SELECT * FROM bseg
INTO TABLE i_bseg UP TO 100 ROWS .
ENDFORM.
FORM create_fcat .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'BSEG'
CHANGING
ct_fieldcat = i_fcat.
wa_sort-spos = '1' .
wa_sort-fieldname = 'BUKRS' .
wa_sort-up = 'X' .
wa_sort-subtot = 'X' .
APPEND wa_sort TO i_sort .
wa_sort-spos = '2' .
wa_sort-fieldname = 'BELNR' .
wa_sort-up = 'X' .
wa_sort-subtot = 'X' .
APPEND wa_sort TO i_sort .
wa_sort-spos = '3' .
wa_sort-fieldname = 'GJAHR' .
wa_sort-up = 'X' .
APPEND wa_sort TO i_sort .
ENDFORM.
FORM display_tree .
CALL METHOD o_tree->set_table_for_first_display
CHANGING
it_outtab = i_bseg
it_fieldcatalog = i_fcat
it_sort = i_sort.