OOPs
OOPs
OOPs
BY SANJAY KUNAR
Basic OOPS Conecpt:
• Classes
• Methods
(a) Instance method
(b) Static method
• Attributes
(a) Instance attribute
(b) Static attribute
• Constructor
• Polymorphism
• Abstract classes
• Interfaces
• Friend class
Advance OOPS Concept:
• Persistent service
• Transaction service
• RTTS (ABAP Runtime Type service)
RTTF (Runtime Type Identification)
RTTC (Runtime Type Creation)
• Casting
• Event handling
• Design patterns
INTRODUCTION:
Note: If the class definition contains method declarations, then we need to implement the
class
2. Implementation of class:
Syntax:
class <class name> implementation.
implementation of methods.
endclass.
Note: Class definition and Class implementation doesn't allocate any memory,it only provides
template of the class
Instantiate the class ---> creating the object for the class--> memory will be allocated
Differences between Local & Global classes:
Object creation --> 2 steps :
1. Declare reference (alias) for the class :
Syntax:
data <ref.name> type ref to <class name>.
Instance Attributes:
• In local classes they are declared by using the keyword “Data”.
• They are specific to object i.e. for every object, separate memory will
be allocated for each instance attribute.
• They can be accessed only by using the Object.
Static Attributes:
Note: We go for instance attributes whenever we need to maintain unique (different) values
for each object. We go for static attributes, whenever we need to maintain a common
(same) value for all the objects of the class. We go for constant attributes, whenever we
need to maintain a fixed value for all the objects of the class.
Methods:
• A method is a set of statements which is implemented only once and can be
called any no. of times.
• It is similar to subroutine / function module.
Subroutines: - 2 sections ---> definition ---> form...endform
Calling ---> perform <subroutine>.
Subroutine Parameters keywords used using, changing, tables
F.M's: 2 sections ---> definition --> function..endfunction (source code tab)
calling ---> call function <f.m>
Function Module Parameters -> keywords used -> importing, exporting, changing,
tables
Local class methods:
There are 3 steps ->
• Declaration (Method prototype declaration) --> Inside Class definition
• Implementation --> Inside Class Implementation
• Calling --> outside class / inside other method implementation
Method Parameters:
• Types of parameters: importing, exporting, changing and returning.
• By Default, Importing parameters are obligatory.
• We can use the keyword ‘optional’ as part of local class method parameters to declare it as
optional parameter and in case of global class methods, select the checkbox ‘optional’
• Exporting parameters are always optional.
Procedure for using Local class methods:
1. Declaration
syntax:
methods/class-methods <method name> [parameters list].
2. Implementation
syntax:
method <method name>.
statements.
endmethod.
3. Calling
syntax 1:
call method <method name> [parameters list]
syntax 2:
<method name>( [parameters list] ).
Method Returning Values:
->In case of ABAP a method can return any no. of values. The no. of return values depends on
no of Exporting/changing Parameters.
Returning Parameters:
Note: In Local classes, the sequence of visibility sections (access specifiers) for the class
components should be in the order of public, protected, private sections.
Note: It is recommended to define and implement the local class at the beginning of
executable program and explicitly handle the event ‘start-of-selection’ after the class
implementation to indicate the starting point of program execution.
Me keyword:
• “Me” keyword refers to current object in execution i.e. as part of every instance method
execution (runtime), an implicitly created object (created by SAP) will be available and it
refers to the object which is currently executing the method.
• Note: If a class contains both attributes and methods, it is recommended to declare
attributes under protected/private sections and methods under public sections.
• Since we cannot access protected/private components outside the class, we need to access
them through one of the public method and call the public method outside the class.
Friend keyword:
• In General outside the class, A Object can access only public components of the class
directly. i.e protected and private components of the class cannot be accessed outside the
class using an object of a class.
• In order to enable the objects of the class to access all components of the class outside the
class irrespective of the visibility, then we can go for Friend classes.
• In local classes “Friend” is a keyword used as part of class definition to consider another class
as friend.
Deferred Keyword:
• As part of declaring local friend classes, we need to forward declare the friend classes by
using “Deferred” keyword. This deferred keyword indicates that the class definition is
provided somewhere else in the program and not at the beginning of the program.
• Note: In Global classes, we declare friend classes as part of ‘FRIENDS’ tab.
Load keyword:
• It is used to load the global classes or global interfaces explicitly in the executable program.
This is mandatory before the release 6.20 for accessing static components of the global class
or global interface before instantiating them.
• From 6.20, it is not required, but in case, if we get any compilation error while accessing the
static components / any other components of global class or global interface, then we can
load the global class or global interface explicitly from the class library by using load
keyword.
Constructor:
• Whenever an object is created, the attributes should be initialized with some initial values.
• It is special because it is executed/called automatically whenever an object is created
(instance const) or whenever a class is loaded in the memory (static const).
• They are always declared in public section.
• They never return any value.
• There are two types of constructors
1. Instance constructor.
2. Static constructor.
Instance constructor:
• They are declared by using the keyword “Constructor”.
• They can contain only importing parameters and exceptions.
• It is specific to object i.e. whenever a new object is created SAP executes “Instance
constructor”.
• Instance constructor is executed only once in the life time of every object.
Static constructor:
• It is declared by using keyword “class_constructor”.
• It cannot contain any parameters or exceptions.
• It is not specific to any object.
• It is executed only once in the life time of every class. I.e. it is executed in either of the
following cases.
Case 1: When we access any static components of the class before creating any objects for
the class. (or)
Case 2: when we create first object of the class before accessing any static components of the
class.
Note:
If a class contains static and instance Constructor and if we instantiate first object before
accessing any static components, than SAP first executes Static Constructor and then the
Instance Constructor will be executed on behalf of that first object, from the second object
onwards only instance constructor will be executed.
If an instance constructor contains any mandatory importing parameters, we must pass the
values to those parameters while creating objects itself.
Differences between Instance and Static Constructor:
Differences between normal method and s pecial
method (constructor).
NORMAL METHOD CONSTRUCTOR
It can declare any of section (Public, Protected,
It can declare in only public section
Private)
Can return any number of values Never written any value(exporting values)
INHERITANCE:
• It is a process of using the properties (components) of one class inside other
class.
• The main aim of Inheritance is Reusability of the components i.e. a
component declared in one class is accessible inside other class.
• In Inheritance, two classes are involved (super class/base class and sub
class/derived class).
• The class which gives properties is called as super class / base class.
• The class which takes the properties is called as sub-class/derived class.
• Only Public and Protected components can be inherited i.e. Private
Components cannot be inherited.
Types of inheritance:
1. SINGLE INHERITANCE: A Class acquiring properties from only one super class.
2. MULTIPLE INHERITANCE: A class acquiring properties from more than one entity
(interface).
Note: In ABAP, Multiple inheritance can be implemented using the combination of
class and interfaces.
3. MULTILEVEL INHERITANCE: A class acquiring the properties from another sub-
class.
Inheriting from – is a keyword used as part of local class definition for inheriting
another class.
A class declared as Final cannot be inherited i.e. it cannot have subclass. In case of
local classes, we use the keyword ‘final’ as part of class definition to declare a final
class.
Polymorphism:
Poly -> many,
Morph-> forms,
Ism-> behaviour
• It is a process of making an entity behaving in multiple forms.
• In this case, the entity is a method
Example:
Method overloading, Method Overriding.
Method Overloading:
• It is similar to function overloading in C++.
• It is a process of overloading the same method by passing different Number and different
types of parameters.
Eg: methods m1.
Methods m1 importing x type i.
Methods m1 importing x type c.
Methods m1 importing x type I y type i.
Note: ABAP does not support method overloading.
Method Overriding:
• If a subclass redefines a super class method it is called as Method Overriding.
• Whenever a local subclass wants to redefine the super class method, the sub class as to re-
declare super class method by using “redefinition” keyword.
• Whenever the super class method is redefined in subclasses, we cannot change the visibility /
category.
• Only public and protected instance methods can be redefined.
• Whenever a subclass redefines super class method, it is always recommended to call the super
class method implementation in the subclass and this is done by using “super” keyword.
• “Super” keyword is always used in subclass method implementations (inside redefined super
class method) to call the super class method implementation.
• A class declared as final cannot be inherited.
• Static methods cannot be redefined
• Instance public / Instance protected methods declared as final can be inherited but cannot be
redefined.
• A static public/static protected methods cannot be declared as final because by default, static
methods cannot be redefined.
Hierarchy of constructor execution-scenario 1:
If a super class contains static and instance constructor and subclass without any constructors,
and if we instantiate first object of super class/sub class before accessing any static
components of super/sub class ,then SAP first executes static constructor of super class and
then instance constructor of super class and from second object of super class/sub class only
instance constructor of super class will be executed.
• If a super class contains static and instance Constructor and subclass also with
static and instance constructor, then it is mandatory for sub class instance
constructor to call the super class instance constructor explicitly by using super
keyword.
• In this case, if we instantiate first object of sub class before accessing any static
components of super/sub class and before creating any objects for super class, then
SAP first executes static constructors from super class to sub class (top to bottom)
and then instance constructors from sub class to super class (bottom to top) and
from second object of sub class onwards, only instance constructors will be
executed from sub class to super class.
Hierarchy of constructor execution-scenario 4:
If a super class contains instance constructor with mandatory parameters and sub class also
contains instance constructor with mandatory parameters and whenever we instantiate the sub
class, make sure that you pass values for the parameters of sub class instance constructors and
from sub class instance constructor implementation we need to call super class instance
constructor by passing values for parameters of super class instance constructor.
Note: For excluding entire ALV toolbar in the Report, set the field ‘NO_TOOLBAR’ to ‘X’ as part
of Layout.
Procedure:
Step1:
Take an additional column in the final internal table which is of type char4.
Step 2:
Before displaying the final data in the ALV grid, loop the final internal table and set the color
coding for the additional field based on the condition.
As part of layout generation, set the field “info_fname” whose value is name of the additional
field which holds the row color coding.
ALV Cell Coloring:
Procedure:
Step1:
Take an additional column in the final internal table which is of type ‘LVC_T_SCOL’.
Step 2:
• Before displaying the final data in the ALV grid, loop the final internal table and set the
color coding for the additional field based on the condition.
• For this, we need to prepare work area of type ‘LVC_S_SCOL’ and append the same to the
additional field and update the internal table.
• As part of layout generation, set the field “ctab_fname” whose value is name of the
additional field which holds the cell color coding.
ALV Traffic Light columns:
• Traffic light columns acts as indicators in the ALV reports.
• These traffic lights by default are displayed as First column of the ALV report.
• Traffic light codes are 3 types (1-Red, 2-Yellow, 3-Green) and also we can refer to the corresponding constant
values in the ICON type-group.
Procedure:
Step 1:
Take an additional column in the final internal table which is of type character of “One”.
Step 2:
Before displaying the final data in the ALV GRID, loop the internal table and set the traffic light code for an
additional column based on a condition.
Step 3:
As part of layout, set the field “EXCP_FNAME”, the value of the field should be “Name of the additional column”
containing traffic light code.
Note: By default, Traffic light column is displayed in first column position. To display traffic light in a specific
column position, declare an additional column of type character with some length, generate field catalog for this
column including required column position and assign traffic light code to its column value along with static text.
In this case, we should not set the field ‘EXCP_FNAME’ in layout.
Splitter control:
• It is used for splitting the container in to “N” no of partitions.
• Each partition is called as a “PANE”.
• Each “PANE” should be associated with a container to hold an object. The object can be ALV
Grid, Image and Tree.
Procedure for displaying images in ALV Reporting using classes:
Step 1:
• Upload the picture by using t-code ‘SMW0’.
• In SMW0, choose the radio button ‘binary data for webrfc applications’, click on ‘FIND’
(appl.toolbar) provide package name(any package), object name (any name, unique id)
description(…), click on execute click on ‘create’ button (appl.toolbar),provide object name
(ZBT), description(…), click on ‘import button’ (status bar of dialog box)
Step 2:
• Call the function module “dp_publish_www_url”.
• This function module takes an image object id as input and returns the “URL” of picture
which is of type “cndp_url”.
Note: “cndp_url” is a types declaration declared in the type group “CNDP”.
Step 3:
Create the object of picture class ‘CL_GUI_PICTURE’ linking with a container.
Step 4:
call the instance method “load_picture_from_url” of the class “cl_gui_picture”. This method
takes URL of the picture as input and displays the picture in the container.
Procedure for tree control:
• A tree control is collection of nodes.
• A node can be a folder / it can be an action item.
• We can add the nodes to the tree control by using the instance method “add_nodes” of the
class “cl_gui_simple_tree”.
• This method contains two mandatory parameters.
1. Structure representing the nodes.
2. Internal table containing the nodes information.
Note: “ABDEMONODE” is one of the standard structure given by SAP which represents simple
tree (or) we can create custom dictionary structure by including std.structure ‘TREEV_NODE’
and a character field ‘text’ of any length.
Displaying ALV columns as Dropdown:
Generally we display ALV column as drop down when we have fixed set of values to be
shown to the user as a drop down so that the user can select the value in the runtime.
Procedure:
Step 1:
Take an additional column in the final internal table, this additional column needs to be
displayed as dropdown.
Step 2:
• Generate the field catalog for the additional column.
• As part of the field catalog, set the field “DRDN_HNDL” to some Numeric value and also
set the edit field to ‘X’ as the column should be editable, so that user can choose value
from the drop down in the runtime.
Step 3:
Prepare an internal table of type ‘LVC_T_DROP’ with drop down handle and dropdown
values and pass this internal table as a parameter to the method ‘SET_DROP_DOWN_TABLE’
of the class ‘CL_GUI_ALV_GRID’.
Custom event handling process:
1. Needs to be declared in custom class definition.
2. Event Business logic needs to be implemented as part of custom class implementation(inside
event handler method).
3. Raised in custom class methods.
4. Register the handlers for execution of event handler methods.
1. To Display ALV column values as link, we need to set the field ‘HOTSPOT’ as part of field
catalog for that particular field.
2. ‘HOTSPOT_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered
by SAP whenever the user clicks on ALV Cell value in the runtime.
3. ‘BUTTON_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered
by SAP whenever the user clicks on ALV Cell displayed as pushbutton
4. ‘DOUBLE_CLICK’ is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered by
SAP whenever the user double clicks on ALV Cell value in the runtime.
5. As part of interactive ALV reporting using classes, when the user interacts and navigates
from one screen to another screen, we need to refresh the grid with the corresponding
internal table data using the method ‘REFRESH_TABLE_DISPLAY’ of the class
‘CL_GUI_ALV_GRID’.
TOOLBAR Event:
It is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered when the ALV grid
is displayed. This event can be used to manage the ALV Toolbar for Enabling/Disabling
standard ALV Toolbar buttons, Adding custom buttons.
USER_COMMAND Event:
It is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered when the user
clicks on custom normal buttons on ALV toolbar. Before this event, SAP Triggers
‘BEFORE_USER_COMMAND’ and then ‘USER_COMMAND’ and after this it triggers
‘AFTER_USER_COMMAND’. These events are also triggered when the user clicks on Menu
items of Menu Buttons of ALV toolbar.
MENU_BUTTON Event:
It is the instance event of the class ‘CL_GUI_ALV_GRID’ which is triggered when the user
clicks on custom MENU buttons on ALV toolbar.
Note:
1. To enable multiple selection of rows on ALV grid, we need to set the field ‘SEL_MODE’ TO ‘A’
as part of layout
2. To identify the selected rows on the ALV grid, we need to use the instance method
‘GET_SELECTED_ROWS’ of the class ‘CL_GUI_ALV_GRID’. This method returns the internal
tables containing the indexes of selected rows.
Editing ALV Cells in runtime and updating to database:
Procedure:
1. For the ALV column to be editable, set the field edit to ‘X’ As part of field catalog
2. Handle the event ‘DATA_CHANGED’ of the class ‘CL_GUI_ALV_GRID’. This event is not triggered by
default as it is not recognized as system event, it requires explicit registration and it is done by calling
the instance method ‘REGISTER_EDIT_EVENT’ of the class ‘CL_GUI_ALV_GRID’. This method takes
event id as a mandatory input parameter. These event ids exist in the form of constant attributes of
the grid class and we can use any of the following attribute event ids.
a) Mc_evt_modified Allows only single cell editing, in this case the event ‘DATA_CHANGED’ is
triggered when the user shifts the focus from the first modified cell or when the user presses enter key in
the first modified cell.
b)Mc_evt_enter -> Allows multi cell editing, in this case the event is triggered when the user
presses enter key in the last modified cell.
3. As part of the event handler method, we need to import the event parameter ‘ER_DATA_CHANGED’
and using this event parameter (object) access the instance attribute (internal table)
‘MT_MODIFIED_CELLS’ which keeps track about the information of the modified cells which includes
row_id and modified value. Based on this, update the grid internal table as well as corresponding
database table.
Tree Control Events:
1. ‘NODE_DOUBLE_CLICK’:
It is the instance event of the class ‘CL_GUI_SIMPLE_TREE’ which is triggered when the user
double clicks on the tree node of the tree control. This event is not triggered by default; it needs
to be registered explicitly by using the instance method ‘SET_REGISTERED_EVENTS’ of the class
‘CL_GUI_SIMPLE_TREE’.
2. ‘NODE_CONTEXT_MENU_REQUEST’:
It is the instance event of the class ‘CL_GUI_SIMPLE_TREE’ which is triggered when the user
right clicks on the tree node of the tree control. This event can be handled to associate the
context menu with the tree node. This event is not triggered by default; it needs to be
registered explicitly by using the instance method ‘SET_REGISTERED_EVENTS’ of the class
‘CL_GUI_SIMPLE_TREE’.
3. ‘NODE_KEYPRESS’:
• It is the instance event of the class ‘CL_GUI_SIMPLE_TREE’ which is triggered when the user
presses a key on the tree node of the tree control. For this, we need to register the keys for
triggering this event. The keys can be registered by calling the method ‘ADD_KEY_STROKE’ of
the class ‘CL_GUI_SIMPLE_TREE’.
• All the keys exist in the form of constant attributes with the naming standard ‘KEY_....’ of the
class ‘CL_GUI_SIMPLE_TREE’. This event is not triggered by default; it needs to be registered
explicitly by using the instance method ‘SET_REGISTERED_EVENTS’ of the class
‘CL_GUI_SIMPLE_TREE’.
4. ‘NODE_CONTEXT_MENU_SELECT’:
It is the instance event of the class ‘CL_GUI_SIMPLE_TREE’ which is triggered when the user
selects a context menu item from the context menu of the node.
Note: For the method ‘SET_REGISTERED_EVENTS’, we need to pass event id as a parameter. All
the event ids exist in the form of constant attributes with the naming standard ‘EVENTID_.....’ of
the class ‘CL_GUI_SIMPLE_TREE’.
Generating TOP OF PAGE content for ALV Grid:
For this, we need to handle the event ‘TOP_OF_PAGE’. It is the instance event of the class
‘CL_GUI_ALV_GRID’ used for generating content in the TOP OF PAGE area of ALV Grid. This
event is not triggered by default; it should be registered explicitly by calling the instance
method ‘LIST_PROCESSING_EVENTS’ of the class ‘CL_GUI_ALV_GRID’.
Field catalogue:
It is an object which contains the information’s of the fields display in the “ALV Grid”. This
information includes name of the field, position, label, grid, appearance etc.
Note:
1. To specify the field catalogue information using the dictionary structure we need to use the
importing parameter “I_STRUCTURE_NAME” as part of the function module
“REUSE_ALV_GRID_DISPLAY”. Whenever this parameter is specified all the properties of the
field are taken from dictionary itself.
2. The structure name passed must match with the format of internal table i.e. the no of
fields and sequence of fields in the structure and in the internal table must match
otherwise it leads to runtime error.
USER_COMMAND:
• This event is triggered when the user double clicks on ALV cell value (or) single click on
hotspot cell value.
• This event contains two parameters.
i. Parameter of type – sy-ucomm.
ii. Parameter of type slis_selfield.
Note:
• To handle the events we need to specify the parameter “I_CALLBACK_PROGRAM” as part of
F.M ‘REUSE_ALV_GRID_DISPLAY’ so that whenever the event is triggered SAP will search for
the corresponding event subroutine definition in the specified calling program.
• “REUSE_ALV_COMMENTARY_WRITE” is a function module used for associating text and Logo
to the TOP OF PAGE Area of ALV report developed using F.M. This F.M should be called as
part of user defined subroutine definition associated with the event ‘TOP_OF_PAGE’.
Blocked ALV:
• It is used for displaying the data in the form of blocks.
• As part of this we use following function modules.
• REUSE_ALV_BLOCK_LIST_INIT
• REUSE_ALV_BLOCK_LIST_APPEND
• REUSE_ALV_BLOCK_LIST_DISPLAY
Procedure:
Step 1:
Initialize the ALV blocked list using the function module REUSE_ALV_BLOCK_LIST_INIT
Step 2:
Generate the field catalog for the block.
Step 3:
• Retrieve the data for the block.
• Append the field catalog and the data to the blocked list using the function module
REUSE_ALV_BLOCK_LIST_APPEND.
Note:
Repeat the steps 2, 3 for each of the block.
Step 4:
Display the ALV blocked list using the function module REUSE_ALV_BLOCK_LIST_DISPLAY.
Note:
REUSE_ALV_EVENTS_GET is the function module which returns list of events supported in ALV
function modules.
Exception Handling:
• An exception is a runtime error which is raised during the program execution.
• If the exception is not handled, the program execution will be terminated.
• Exception handling is a process of handling the Runtime Error’s and continuing the program
execution without termination.
• The exceptions which are raised by SAP are called as Standard Exceptions.
• These standard exceptions are raised from standard exception classes which start with
Naming convention “CX___”.
• We can handle these exceptions by using “TRY” and “CATCH” blocks.
• Inside the “TRY” Block we need to keep those statements where the possible exceptions
might be raised.
• The ‘CATCH’ block is placed immediately after the Try Block; it is responsible for handling the
exceptions by providing appropriate exception handling statements which can be system
defined exception message / user-defined exception message.
• “CX_ROOT” is common for any exceptions i.e it is a super class for all exception classes.
• As part of the catch block we need to provide the exception class name which is responsible
for raising the exception.
• If we are not sure about the exception class, we can give the exception class name as
“CX_ROOT”.
• “CX_ROOT” is super class for all the exception classes..
Note:
• Whenever an exception is raised in a “TRY” block, SAP creates the object of the exception
class which is raising the exception and the control will jump to catch block.
• As part of catch block, we need to receive the exception class object into our local reference
of exception class and using this referenced object we can access the appropriate methods
of Exception class to capture standard exception messages.
• Whenever an exception is raised in try block, SAP ignores the rest of the statements in the
try block and the control will jump to catch block.
Standard Exceptions:
Declared, Raised By SAP
Handling Developer (using try and catch block)
Cleanup block:
• Cleanup block is provided as part of try block to execute certain statements mandatorily
whenever an exception is raised in the try block.
• In general, whenever an exception is raised in the try block, the control jumps to catch
block ignoring the rest of the statements in the try block.
• In these cases, there might be certain statements which we need to execute whenever an
exception is raised, these statements we can provide as part of cleanup block.
• If a try block contains cleanup block, we cannot have catch block, so in order to handle the
exception, catch block should be provided as part of outer try block.
• As part of cleanup block, we can perform cleanup activities. i.e clearing the variables, free
internal tables, close the files…….
• We should not abruptly terminate the execution of cleanup block using the statements
stop, submit and call transaction statements.
User-defined exceptions in local class methods:
These exceptions are declared, raised and handled by developer.
Procedure:
1. Declare the exception in the method declaration by using “exceptions” keyword
Syntax: exceptions <exception name>.
3. Handle the exception while calling the method by checking the sy-subrc status.
Note: We can use ‘raise’ keyword to raise any exceptions i.e system defined or user-defined.
Raising keyword:
• Raising is a keyword used as part of local class method declaration to indicate that a method
might raise the exception but cannot handle the exception.
• In these cases, the caller of the method should take the responsibility of handling the
exception.
• The advantage of this approach is since a method can be called any no. of times at different
places in the object, at each place, we can handle the same exception in different way
(custom defined exception messages in one place, std.exception messages in another place..)
• In this, we need to consider a database table in which the primary key field/s of the table is
considered as “Business key identity”.
• This field is used for uniquely identifying the object.
Procedure for using persistence service using Business key :
1. Consider a db table
Table: z1130emp
Fields:
Empno (PK) zempno int4 10
Ename zename char 20
Empdesig zempdesig char 20
Note: By default, Persistence class implements the interface ‘if_os_state’ which is responsible
for storing the state of the object permanently.
3. Map the Persistence class with the Corresponding data base table. (‘goto’ menu persistence
representation)
Note: We must map all the fields of the db table to the persistence class (double click on each
field and click on ‘set attribute values’ button)
Note: Once the mapping is done, all the fields of the table will be added as attributes of the
persistence class.
• Apart from this, SAP GENERATES “GET_” Methods for all attributes and “SET_” methods for
non-primary key fields.
• Getter method is used for reading the value of the attribute and Setter method is used for
setting the value to the attribute.
4. Activate the persistence class so that dependent classes also get activated.
5. As part of this activation, SAP generates the following methods as part of base agent class.
i. Create _Persistent
ii. Delete_Persistent.
iii. Get_Persistent
To interact with the Persistent object we need to access above 3 methods:
• To access the above 3 instance Methods we require object of base agent class, but the base
agent class is created as “Abstract class”. So, it cannot be instantiated directly.
• The above 3 methods are public and they are inherited to sub class which is Actor Class or
Agent class.
• But, Actor class IS created as “Private Class”, so, it cannot be instantiated directly outside.
• Actor class is created as Singleton class, so there is a possibility of creating one and only one
object. So in order to get the object of the Actor class we must follow the following
Procedure.
• As part of every Actor class, SAP Provides Static Public Attribute “Agent”. This attribute is
marked as ‘Read only’ i.e this attribute will return a value whenever it is accessed.
• Whenever this attribute is accessed by using Actor class name , internally SAP Executes Static
Constructor of Actor class which is Responsible for creating singleton (single object) of actor
class, This object will be returned back to the static attribute agent, we need to receive this
returned object so that we can access the above 3 methods.
• “COMMIT WORK” statement will save changes permanently in the database.
Persistence Service Using GUID (global unique identifier):
• In this we need to consider a database table whose first field is GUID and the corresponding
data element should be “OS_GUID”. The value for this field “GUID” will be dynamically
generated by SAP itself.
• We need to map the persistence class with this database table. After Mapping, except
“GUID” field all the remaining fields will be added as attributes of the class.
• Apart from this, SAP Generates “Getter” &”Setter” methods as part of persistence class for
all the fields except “GUID”.
• When we activate the persistence class, SAP generates the following 3 Methods as part of
“BASE AGENT CLASS”.
1. Create_Persistent.
2. IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
3. IF_OS_FACTORY~DELETE_PERSISTENT.
Procedure for creating Persistence service using GUID:
Step 1: Consider a database table with GUID as first field.
Table: y745emp
Fields: Data element
GUID os_guid (Primary Key)
EMPNO zeMPno
ENAME zename
EMPDESIG zempdesig
Step 2: Create the persistence class( ycl_815emp)
Base agent class ycb_815emp
Actor class yca_815emp
Step 3: Map the persistence class with the above table.
In persistence service using GUID the uniqueness of the object is controlled based on the GUID
field.
GET_PERSISTENT_BY_OID:
This method takes GUID as input and returns the object of the object class. This object class in
the runtime points to persistence class, so we need to type cast the object class object to
persistence class object. Using the persistence class object access the required getter methods.
DELETE_PERSISTENT:
• This method takes the object of the interface ‘if_os_state’ as input value, but we cannot
directly create objects for interfaces, so we need to analyze what is the implementation
class which is implementing this particular interface.
• As per the analysis, it is the persistence class (ycl_emp) which is implementing the
interface ‘if_os_state’, so we can pass the object of this persistence class as a parameter
value. In order to get the object of persistence class, first of all we have to check
whether the “GUID” is existing or not this is done by calling the instance method
“Get_Persistence_By_Oid” .This method returns object of the object class, this object
class object needs to be type cast to persistence class object and pass that object as
parameter to “Delete_Persistent”.
TRANSACTION SERVICE :
It provides the methods to get the type defintion of data objects at runtime.
• It provides the methods to create the data objects at runtime with any type defintion.
• Basically ABAP RTTS provides a set of classes, whose methods can be used for runtime type
identification and runtime creation.
Narrow Casting & Widening Casting:
1. Narrow Casting:
If we want to access child class method from a parent class object is called narrow casting.
Syntax:
obj1 = obj2
call method obj1->(‘Adding’)
//Adding method is belong obj2
2. Widening Casting:
If we want to access parent class method from an child class object is called widening casting.
Syntax:
obj2 = obj1
call method obj2->Get Data
// Get data method is belong obj1
Thank You
Created By - Sanjaya Kunar
Follow Me - http://www.linkedin.com/in/sanjaya-kunar-aa9351254
Any Queries?
Connect - kanhasanjayk@gmail.com