Professional Documents
Culture Documents
ENDCLASS.
ENDCLASS.
OBJECTS
Objects are instances of classes.
Each object has a unique identity i.e. memory and its own attributes.
Components of a Class
Components of a Class :
Attributes:- Any data variables), constants declared within a class
are called as attributes of the class.
Methods:- Methods contain a block of code, providing some
functionality for a class.
Events:-
Interfaces:-
exist
separately for each instance (object) of the class are called as
instance components,
that means for each object, instance components will be available
separately.
These are referred using the symbol .
Visibility of Components
PUBLIC
PROTECTED
PRIVATE.
Public section:
Data declared in public section can be accessed by the class itself,
by its subclasses as well as by other users outside the class.
Protected section:
Data declared in the protected section can be accessed by the class
itself, and also by its subclasses but not by external users outside
the class.
Private Section:
Data declared in the private section can be accessed by the class
only, but not by its subclasses and by external users outside the
class.
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
Local Class: Local classes are define in an ABAP program
(Transaction SE38) and can only be used in the program in which
they are defined.
Ex on Class
Go to SE24
Click on create
V_name instancepublictypechar25.
Creating Objects
To call any class component with the instance, use the below Syntax.
Syntax:
OBJ2v_name
Example program :-
Static variables
Steps:
Go to se24
Write : / sy_uline .
Note
In the real time , we use static variables with the class name only.
Steps:
Go to se24
Click on create
Creating Methods
STEPS:
Go to se24.
GET_CUST_DETINSTANCEPUBLIC
Click on the parameters button and & specify the parameters as below.
(or)
METHOD GET_CUST_DET.
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.
TABLES PARAMETER:
In the methods, we dont have any option for specifying internal tables
directly.
Whereas in function modules, we have an option by name TABLE where
we can specify the internal tables directly.
Go to se11.
Go to se24.
METHOD GET_CUST_LIST
ENDMETHOD.
EXPORTING
IM_LAND1 = P_LAND1
IMPORTING
EX_KNA1 = I_KNA1.
WRITE:/ WA_KNA1-KUNNR,
WA_KNA1- NAME1,
WA_KNA1-LAND1.
ENDLOOP.
Method GET_CUST_DETAILS
Select single * from KNA1
End Method.
Save, Activate and Test it.
Exporting
IM _KUNNR = P_KUNNR
Importing
EX_KNA1 = WA_KNA1.
EVENTS IN OO ABAP
Event:
Use set handler and register event handler method to a particular instance
in the program.
SET HANDLER:
It is the keyword which is used to register event handler method to a
particular instance of class where the event is raised.
For All Instances
Sometimes we want to execute event handler methods not to a particular
instance of class, but for all the instances of the class, then we use FOR
ALL INSTANCES.
TYPES OF EVENTS
Standard Events : Events which are defined and raised by SAP are called
Standard Events
Ex : Double_Click, Right_Click
For these events we are suppose to create event handler
methods and write the corresponding logic.
Custom Events : Events which are defined, raised and also handled by
creating event handler methods are called Custom Events .
Ex On Events
NO_KUNNRINSTANCEPUBLIC
Click on save.
EVT_HDLER_METH instancepublic.
A pop-up is rised.
Select the checkbox event handler &provide the class name and event
name
Event handler for
class/interface : ZCL-SAMPLE.
Event
: NO-KUNNR.
Click on the change button, which will convert the method into event
handler method.
The symbol >> will indicate that it is a 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:
SET HANDLER <event-handler method> FOR <instance>
Ex:- Set handler OBJ1XYZ FOR OBJ1
Ex: Go to the program and call the method without giving any customer no.
Data: wa_kna1 type kna1.
Data:obj1-type ref to zcl-sample.
Create object OBJ1.
CONSTRUCTORS
These methods are mainly used to set default values in a class. i.e. Used to
default values to attributes. (Ex V_Werks = 0001)
Types of CONSTRUCTORS
INSTANCE Constructor
STATIC Constructor
INSTANCE CONSTRUCTORS:
EXAMPLE ON CONSTRUCTORS:
Im_kunnr---->type--kna1-kunnr.
Double click on the constructor & write the code.
Save and activate.
We never call the constructor method bcoz they are executed automatically whenever
an object is created.
Select create object & provide instance name & class name.
Instance : obj1
Class
: zcl_sample.
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:
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.
1.
Interface
Interface
Interfaces are independent structure which are used in a class to
extend the functionality of a class.
1.
Polymorphism
Polymorphism
It is a concept by which the same method names will behave
differently in different classes i.e each method will have its own
implementation in different different classes but with the same
name.
1.
creating an interface
Go to SE24.
Click on create.
Go to SE24.
ZV_IF~get _mara.
ZV_IF~ get_KNA1.
End method.
Inheritance
Inheritance
Inheritance is the concept of passing the properties of one class to another class.
The class which passes the properties is called SUPER class or Parent class.
The class which receives the properties is called SUB class or Child class.
Derived class can overwrite the existing methods also add new code.
This means we can add additional features to an existing class without modifying it.
Super is the keyword used to represent the super class. You can access the methods
and attributes the super class using this word super.
REDIFINATION is the keyword which is used to overwrite the parent class methods
with new definition.
NOTE: In oo_ABAP, we have only single inheritance.
There is no multiple inheritance, But we have Multilevel inheritance
1.
Polymorphism
Polymorphism
It is a concept by which the same method names will behave
differently in different classes i.e each method will have its own
implementation in different different classes but with the same
name.
Interface Methods
Method overloading
Method Overriding.
Ex on Inheritance
Remove the checkbox final because we want to inherit the class in a child class
Method get_material_data.
End method.
Go to SE24.
Click on save.
functionality
And spras=EN.
Exporting.
Im_matnr = P_matnr.
Importing
Ex_mara = wa_mara.
Write: / wa_mara-matnr,
Wa_mara-mtart,
Wa_mara-meins .
Write : / OBJwa_makt_matnr,
OBJwa_makt_spras,
OBJwa_makt_maktx
Abstract class
Abstract class
It is a class which contains methods with implementation as well as
methods without implementation .
(or)
Abstract class is a class which contains at least one abstract method
Abstract method :It is a method which doesnt contain any implementation.
Abstract class are mainly used for creating inheritance.
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 .
Goto SE24
Click on Create
Steps:
Go to SE24
Click on Create
Note :- create an object to the child class as we cannot create the objects to the abstract class .
Exporting
im_kunnr =p_kunnr.
Importing
Ex_kna1 = wa_kna1.
Write :/ wa_kna1-kunnr , wa_kna1-name1.
LOCAL CLASS
LOCAL CLASS :
ENDCLASS.
ENDCLASS.
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.
PUBLIC SECTION.
EVENTS : <EVENTNAME>.
END CLASS.
Example:
OF <CLASSNAME>
END CLASS
METHODS : <Methodname>
END CLASS.
END CLASS.
DEFINATION DEFERRED :
CONSTRUCTORS
Introduction:
The ALV grid data is displayed on an UI element called as custom
container.
Advantages of OO ALV :
We have n no of events available in the classes when compared to
ALV with function modules which gives flexibility for the
programmer to develop ALVS for various scenarios.
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.
We can also place different UI elements like checkbox,
Radiobutton on the same screen in addition ALV grid data.
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.
START-OF-SELECTION .
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM DISPLAY_ALV .
ENDMODULE.
FORM CREATE_OBJECTS .
FORM GET_DATA .
SELECT * FROM MARA
INTO TABLE I_MARA UP TO 100 ROWS
ENDFORM.
FORM DISPLAY_ALV .
= I_MARA.
ENDFORM.
Save->activate->test it.
1.
END OF ty_mara.
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM create_fcat .
PERFORM DISPLAY_ALV .
ENDMODULE.
FORM create_fcat .
CLEAR wa_fcat .
wa_fcat-col_pos = '1' .
wa_fcat-fieldname = 'MATNR' .
wa_fcat-tabname = 'I_MARA' .
ENDFORM
FORM display_alv .
CHANGING
it_outtab
it_fieldcatalog = i_fcat.
ENDFORM.
= i_mara
INTERACTIVE ALV
Introduction :
So, to define a event handler method, we need to define a LOCAL CLASS as below.
ENDCLASS
Once we define a LOCAL CLASS, we need to define a event handler method as below.
PUBLIC SECTION .
METHODS : handle_double_click FOR EVENT double_click
OF cl_gui_alv_grid
IMPORTING e_row.
ENDCLASS
E_ROW-INDEX gives the index or row no where double click event is raised.
STEP1: Create data declarations for ALV/custom container, also define object or local class.
*BASIC ALV
*SECONADARY LIST
PUBLIC SECTION .
METHODS : handle_double_click FOR EVENT double_click
OF cl_gui_alv_grid
IMPORTING e_row.
ENDCLASS .
METHOD handle_double_click .
ENDMTHOD.
ENDCLASS.
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 .
METHOD handle_double_click .
"LCL_EVENT_HANDL
Screen 200.
Perform create_object2.
Perform create_Fcat2
Perform Display_ALV2
FORM create_objects2 .
IF alv_cont2 is initial .
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' .
ENDIF.
ENDFORM
FORM display_alv2 .
CHANGING
it_outtab
it_fieldcatalog = i_fcat2.
ENDFORM.
= i_makt
IF ALV_CONT1 IS INITIAL.
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM CREATE_CAT1 .
PERFORM DISPLAY_ALV1 .
ELSE.
PERFORM CREATE_OBJECTS2.
PERFORM CREATE_FCAT2.
PERFORM DISPLAY_ALV2.
ENDIF.
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 .
DATA : HEADING TYPE SDYDO_ATTRIBUTE .
CALL METHOD DD->ADD_PICTURE
EXPORTING
PICTURE_ID
= 'ARJU' .
= '25' .
HEADING = CL_DD_DOCUMENT=>HEADING .
EXPORTING
TEXT
SAP_STYLE
= HEADING .
= 'LOGO.
" DISP_LOGO
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.
PR_NAMEINSTANCEPRIVATETYPECHAR20.
Go to FRIENDS Tab.
WRITE :/ objPR_NAME.
ALIASES:-
Interview Questions
Interview questions:
What is the diff b/w instance and static comp?how do u access them?
Explain?
What is an event?
Interview questions:
Interview questions:
What is polymorphism?
What is aliases?
What is super?
What is redifination?
What is FINAL?
Interview questions:
What is a fieldcatelog?
SPLITTER CONTAINER
CL_GUI_CUSTOM_CONTAINER.
CL_GUI_SPLITTER_CONTAINER.
CL_GUI_CONTAINER.
CL_GUI_ALV_GRID.
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:
STEP4:
Click on flow-logic and create the PBO Module and write below code
form SPLIT_MAIN_CONT .
endform.
form DISP_ALV1 .
CREATE OBJECT o_grid1
EXPORTING
i_parent
= O_PART1 .
SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS .
CALL METHOD o_grid1->set_table_for_first_display
EXPORTING
i_structure_name
= 'MARA'
CHANGING
it_outtab
= I_MARA .
endform.
form DISP_ALV2 .
CREATE OBJECT o_grid2
EXPORTING
i_parent
= O_PART2 .
SELECT * FROM MAKT INTO TABLE I_MAKT UP TO 100 ROWS .
CALL METHOD o_grid2->set_table_for_first_display
EXPORTING
i_structure_name
= 'MAKT'
CHANGING
it_outtab
= I_MAKT .
endform.
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:
data : o_tree type ref to cl_gui_alv_tree_simple .
data : o_cc type ref to cl_gui_custom_container .
data : i_fcat type lvc_t_fcat .
data : wa_fcat like line of i_fcat .
data : i_sort type lvc_t_sort .
data : wa_sort like line of i_sort .
data : i_bseg type table of bseg .
data : wa_bseg type bseg .
STEP2: Create screen 100.
start-of-selection .
call screen 100.
STEP3:
STEP4:
Click on flow-logic and create the PBO Module and write below code
FORM create_objects .
CREATE OBJECT o_cc
EXPORTING
container_name
= 'TREE' .
CREATE OBJECT o_tree
EXPORTING
i_parent
= o_cc .
ENDFORM.
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.
LOOP AT i_fcat INTO wa_fcat .
CASE wa_fcat-fieldname .
WHEN 'DMBTR' .
wa_fcat-do_sum = 'X' .
ENDCASE .
MODIFY i_fcat FROM wa_fcat INDEX sy-tabix .
ENDLOOP .
ENDFORM.
FORM calc_subtotals .
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.
CALL METHOD o_tree->expand_tree
EXPORTING
i_level = '1'.
ENDFORM.
.THANK YOU