SAP - OOABAP Oops Concept
SAP - OOABAP Oops Concept
Syntax:
1. Global class
2. Local class
➢ The global class and the attributes can be referred across the system
➢ The T-code SE24 is used to work with global class.
2. Local Class: The local class are defined and implemented in the same program.
➢ Definition section
➢ Implementation section
a)Classdefinition: This section is used to define (or) declare the different attributes such
as data objects, constants, methods etc.,
Syntax:
CLASS<class_name>DEFINTION
*visibility section
PUBLIC CLASS
DATA:<gv_var(10)> TYPE c,
METHODS:<method_name>,
ENDCLASS.
Syntax:
*class implementation
CLASS<class_name> IMPLEMENTATION.
METHOD:<method_name>...
*get data.
SELECT
........................
ENDSELECT.
ENDMETHOD
ENDCLASS
➢ The internal tables must not be declared with header line (or) occurs in the class.
➢ The ABAP statement ‘LIKE’ cannot be used to define the data objects in the
classes
➢ The selection-screens of the report statementcannot be defined within the class.
TYPEREFTOstatement: this statement is used to define the different type of objects such as
classes, interfaces, BADI definitions etc.,
i. PUBLIC SECTION .
ii. PROTECTED SECTION .
iii. PRIVATE SECTION .
➢ Public Section.
➢ Protected Section
➢ Private Section
VISIBILITY SECTION SAME SUBCLASS ANOTHER
CLASS EXTERNAL CLASS
Public section. YES YES YES
ENDCLASS.
Different Types OfAttrbutes:
i. Instance Attributes
ii. Static Attributes
1. Instance Attributes:
SYNTAX:
CLASS<class_name>DEFINITION.
* Visibility section
PUBLIC SECTION
* Define IntanceAttibuttes
.......................
PROTECTED SECTION.
EXPORTING
DATA:........
..............
ENDCLASS.
...............
START OF SELECTION
* Reference
* CREATE OBJECT
CREATE OBJECT<REF_CLASS>.
syntax:
2. Static Attributes :
➢ They are existed only once but can be used for any number of instances.
➢ The ABAP statement “CLASS --” is used to prefix to plain ABAP data declaration (or)
definitions statements to define static attributes.
➢ The class name should only be used to access the static attributes but not class
reference.
➢ The static attributes connector symbol (===>) is used to refer the static attributes.
syntax:
CLASS-DATA:<gv_static>....
CLASS-METHODS: <static_meth>....
METHOD:
METHOD<meth_name>.
*Process Block
.
.
.
SELECT ..............
.
.
.
ENDMETHOD.
NOTE: The function method have only one return parameter without any importing and
exporting parameters.
syntax:
IMPORTING<Im_param>...,
EXPORTING<Ex_param>...,
➢ They are the special methods and named as CONSTRUCTOR (Instance) and
CLASS_CONSRUCTOR (Static).
➢ Constructors are called at run time automatically while creating an object.
➢ The constructors should be defined under PUBLIC SECTION only.
➢ The instance constructor can have only Importing and exceptions but not Exporting
parameters.
➢ The static constructor cannot have any Importing and Exception parameters.
syntax:
* Instance constructor
METHODS: CONSTRUCTOR IMPORTING......., .
*Static constructor
CLASS_CONSTRUCTOR,
.
.
.
EVENTS IN OOABAP:
SYNTAX:-
CLASS<class_name> DEFINITION.
EVENTS : ,event_name>.
ENDCLASS.
IF SY-SUBRC NE 0.
* Raise Event.
Raise<Event_name>.
.
ENDIF.
Event Handler:
➢ It is used to call method of one class in another class to make use of same
functionality.
➢ The additional statement" FOR EVENT" is used to define the methods with event.
➢ The ABAP statement " SET HANDLER" is used to access attributes of event
handler.
SYNTAX:-
CLASS<cl_event_handler> DEFINITION.
PUBLIC SECTION.
ENDCLASS.
* IMPLEMENT <cl_event_handler>.
CLASA<cl_event_handler> IMPLEMENTATION.
METHOD<event_handler>.
*Bussiness logic
ENDMETHOD.
ENDCLASS.
TRY....CATCH...statements:
➢ This statements are used to try the specified bussiness logic (or) methods and
catch the specified messages in case of fail.
SYNTAX:
TRY
CALL METHOD..............
CATCH <message>.
ENDTRY.
Properties OfOoabap:
Class, objects,etc.
3.Inheritance:
➢ This property is used to adopt the properties of a parent class to the child classes
(subclasses).
➢ The ABAP statement “INHERITING FROM" is used to define the subclass by adopting
the properties of parent class.
CLASS<class_name> DEFINITION....
.
.
ENDCLASS.
ENDCLASS.
METHODS:<final_meth>.....FINAL.
ENDCLASS.
4.Polymorphism:
SYNTAX:
* SUBCLASS
METHOD: <meth_name>............REDEFINITION.
5. Interfaces InOOABAP:
➢ They are the independent structures which can be used to extend the scope of the
classes
➢ The ABAP statement “INTERFACE" is used to define the interfaces.
➢ The interfaces do not have their own implementation sections.
➢ The ABAP statement “INTERFACES" is used to access the interfaces in a class.
➢ The symbol "~ (tilt)" is used to refer the attributes of the interfaces.
Syntax:
*Define interfaces
INTERFACE<int_name>.
METHODS:<inter_meth>
ENDINTERFACE.
* Specify Interface
CLASS<class_name> DEFINITION.
PUBLIC SECTION.
ENDCLASS.
METHOD<int_name> ~ <int_meth>.........
ENDMETHOD.
ENDCLASS.
Up Casting:
Eg:-<cl_superclass> =<cl_subclass>.
NOTE: It is not possible to access the sublcass own methods incase of upcasting.
Down Casting:
NOTE:Incase of Down casting the subclass own methods can also be accesed.
➢ Create one new instance to access the attributes of a super class after up casting.
GLOBAL CLASS:-
NOTE:
1. Use the button create inheritance to create the subclass by inherting the existing
super class.
2. The default class is final class.
3. Uncheck the checkbox "FINAL" to create normal global class.
4. In case of persistent class no need to create an object and the sysytem generates
the defaults class.
➢ Click on yes button.
➢ Enter the package name.
➢ Click on save button.
➢ Create the "TR" by pressing an icon create request.
➢ Define the required methods.
EG:
NOTE:-Follow the similar steps to define the parameters for any number of methods.
NOTE: Attributes of same classcan be referred using self reference statement "me".
STEPS TO CALL THE METHODS FROM THE GLOBAL CLASS (OR) INTERFACE:
EG:
CREATE OBJECt<cl_ref>.
➢ The data types of the method parameters and data types by passing parameters
variables should be similar. Otherwise program goes to short dump.
USES OF OOABAP:-
It can be started with any letter. It should start with Z*/Y* in case of
custom.
It can be defined and implemented It must be defined and
in any source code. implemented using t-code se24.
1. The system provided standard classes which start with "CL_GUI" are used in OOALV
reports.
EG:
CL_GUL_CUSTOM*/DOCKING.............
2. The system provided standard table types and structures with start with "LVC*" are
used for different types which can be used for OOALV reports.
NOTE:
➢ The standard type group "SLIS" is used in case normal ALV reports.
3. The custom control in screen painter to be created to display the data in OOALV
reports.
NOTE:
➢ In the real time the standard system provided class "CL_GUL_ALV_GRID" is mostly
used to work with grid format.
➢ DOUBLE_CLICK:- This event handler method is used to provide the double click
functionality in OOALV reports.
➢ HOTSPOT_CLICK:-This event method is used to provide double click functionality
based on the hand symbol click.
➢ TOP_OF_PAGE:-It is used to populate and display header data in OOALV reports.
➢ MENU_BUTTON: It is used to call the menu status to define the pushbuttons in
OOALV.
➢ USER_COMMAND:- It is used to provide an interactive functionality based on the
performed active.
➢ SET_TABLE_FOR_FIRST_DISPLAY: This method is used to display the final data in grid
format.
3. Splitting container: It is used to split the container into different parts to display the
multiple grids in OOALV.
➢ I t is not possible to display the data in any position (Docking) in normal ALV
reports.
➢ It is not possible to display multiple grids in normal ALV reports
➢ The standard class “CL_GUI_SPLITER_CONTAINER" OR
"CL_GUI_EASY_SPLITER_CONTAINER". Is used to work with Splitter Container.
1.LVC_T_FCAT: This table type can be used for filed catalogue for OOALV.
NOTE: It can be compared with the type SLIS_LAYOUT_ALV of the type group SLIS in
normal ALV.
EXECUTE
Input/output field
Custom Control
Ex:
CREATE OBJECT<rf_grid>
EXPORTNG
SYNTAX:
CREATE OBJECT<rf_split>
EXPORTING
PARENT = <rf_split>bottom_right_container
ORIENTATION = CL_GUI_EASY_SPLTTER_CONTAINER →
ORIENTATION_HORIZONTAL/ ORIENTATION_VERTICAL.
SYNTAX:
CREATE OBJECT<rf_grid>
EXPORTING
➢ This method is used to generate dynamic internal table based on the fields
populated in a filed catalogue of the type "LVC_T_FCAT".
Eg:
*field symbols.
FILED-SYMBOLS: <<gs_line>>,
➢ Populte filed catalog with fixed and dynamic fileds by setting a loop.
Fixed coloumns.
<gs_dyn_fcat>-col_pos = '1".
*dynamic coulmns
<gs_dyn_fact>-filedname= <gs_data-filed>.
ENDLOOP.
EG:
EXPORTING
IMPORTING
Eg: