0% found this document useful (0 votes)
5 views50 pages

Oops Abap Notes - L

The document outlines the features and principles of Object-Oriented Programming (OOP) in ABAP, including encapsulation, data abstraction, inheritance, and polymorphism. It provides detailed explanations on creating classes, attributes, methods, and constructors, as well as the visibility sections and the use of keywords like 'Me', 'Friend', and 'Deferred'. Additionally, it discusses inheritance types and the behavior of methods, including method overloading and overriding.

Uploaded by

sahuojaswi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views50 pages

Oops Abap Notes - L

The document outlines the features and principles of Object-Oriented Programming (OOP) in ABAP, including encapsulation, data abstraction, inheritance, and polymorphism. It provides detailed explanations on creating classes, attributes, methods, and constructors, as well as the visibility sections and the use of keywords like 'Me', 'Friend', and 'Deferred'. Additionally, it discusses inheritance types and the behavior of methods, including method overloading and overriding.

Uploaded by

sahuojaswi1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

OOPS ABAP NOTES

Features of OOPS Oriented Programming Languages


1. Encapsulation --> binding data and member functions into single unit
(class)
2. Data abstraction / Data hiding --> hiding the implementation details
3. Inheritance ---> reusability (inheriting the components of parent class
into child class)
4. Polymorphism--> many forms behavior

1. Encapsulation : Combining all the details in to a single unit (Class)


--> Local class (se38)
class <class name> defenition
endclass.
 Instantiate the class
Create an object
data <obj name> type ref to <class name>
create object <object name>
 Attribute :
1.) Instance attribute
data <attribute name> type <data type>
Instance attributes can be accessed only by using object name
Instance attributes are object dependent , whenever you create a new
object the old memory will be collapsed and new memory will be
allocated based on the data type of the attribute.
2.) Static attribute
class-data <attribute name> type <data type>
1. static attributes can be accessed either by using object name or by
using class name
2. We can create as many objects as we need for a single class

--> Global class

*&---------------------------------------------------------------------*
*& Report ZOOPS_CLASS1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZOOPS_CLASS1.

**********************************Creation of class

class lcl_abc DEFINITION.

PUBLIC SECTION.
data a type i. "Instance attribute
class-data b type i. "Static attribute

ENDCLASS.

*************** Instantiation of a class/ Object creation

data ob type REF TO lcl_Abc.


create OBJECT ob.

* a = 10. We cannot use

ob->a = 10.
* lcl_abc=>a = 10. we cannot use this
* ob->b = 20.
lcl_abc=>b = 20.
* WRITE a. We cannot use

WRITE ob->a.
WRITE ob->b.
uline.
data ob1 type REF TO lcl_abc.
create OBJECT ob1.

ob1->a = 10.

WRITE : ob1->a,
ob1->b.

2. Data Abstraction : Preventing the access of components of a class


from outside the class, by using visibility sections
1. Public section : The components under this section can be accessed
from the class, from the sub class and also from outside the class.
2. Protected section : The components under this section can be
accessed from the class, from the sub class but not from outside the
class.
3. Private section : The components under this section can be accessed
only from the class, but not from the sub class and also not from
outside the class
Note : There is no default visibility section, you have to specify any one
of them.

3. Inheritance : Using the components of one class in another class .


4. Polymorphism : Many forms behaviour
Procedure for creating local class:
1. Definition of class
Syntax:
class <class name> definition [public] [protected] [private]
[deferred] [final] [load] [abstract].
declaration of components.
endclass.
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
Object creation --> 2 steps
1. Declare reference (alias) for the class
Syntax:
data <ref.name> type ref to <class name>.
Note: reference doesn't allocate any memory, it only provides alias.
2. Create object based on reference
Syntax:
create object <ref.name>. --> Memory will be allocated
based on components of class
Attribute: is like a variable which can be used to store the data.
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:
In local classes they are declared by using the keyword “class-data”.
They are also called as “Class Variables / Class attributes”.
They are not specific to any object.
The memory for static attributes will be allocated whenever the class is
loaded in to the memory i.e. memory will be allocated only once which
will be shared by all the objects of the class.
They can be accessed either by using the object or by using the class
name.
Constant Attributes:
In Local classes, they are declared by using the keyword ‘constants’.
They must be initialized at the time of declaration itself. The value of
the constant attribute remains same throughout the program.
They are not specific to any object.
The memory for them will be allocated whenever the class is loaded in
to the memory i.e. memory will be allocated only once which will be
shared by all the objects of the class.
They can be accessed either by using the object or by using the class
name.
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: 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:
- In general, a method can return any no. of parameters.
- To restrict a method to return exactly one value, we must use
returning parameter.
- If a method contains returning parameter it cannot contain
exporting /changing parameters at the same time or vice versa
(prior EHP7)
- A method can contain only one returning parameter
- Returning parameter is always passed by value.
- Returning parameters are always optional.
Instance methods:
In local classes they are declared by using the keyword
“Methods”.
They can be accessed only by using the object.
They can access any kind of components of the class (instance /
static / constant / types).
Static methods:
In local classes they are declared by using the keyword “class-
Methods”.
They can be accessed either by using the object or class name.
They can access only static components, constant attributes and
types attributes. i.e they cannot access instance components.
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:
- It is special method used for initializing the attributes of the class
i.e. 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.

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.
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.

Hierarchy of constructor execution-scenario 2


- If a super class contains static and instance constructor and
subclass only with static constructor, and if we instantiate first
object of sub class before accessing any static components of
super/sub class and also before creating any objects for super
class, then SAP first executes static constructors from super class
to sub class and then instance constructor of super class and from
second object onwards, only instance constructor of super class
will be executed.

Hierarchy of constructor execution-scenario 3


- 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.

Hierarchy of constructor execution-scenario 5


If a super class contains instance constructor with mandatory
parameters and sub class without any instance constructor and
whenever we instantiate the sub class, make sure that you pass
values for the parameters of super class instance constructors
while instantiating sub class object.
ABSTRACT CLASS:
Abstract is a class which contains one or more Abstract methods.
- An abstract method is a method which is just declared but not
implemented.
- We declare a method as abstract when we are not sure about the
implementation.
- The implementation for the abstract methods can be provided as
part of subclasses.
- In local classes, abstract methods are declared by using a keyword
“abstract”.
- If a class contains an abstract method, the class also should be
declared as abstract.
- Abstract methods are declared only in public and protected
sections.
- Abstract methods are also called as non-concrete methods.
- Static methods cannot be declared as abstract as they cannot be
redefined inside subclasses
- Constructors cannot be declared as abstract as they cannot be
redefined inside subclasses
- The subclass whichever is inheriting the abstract class must
implement all the abstract methods of the abstract super class
otherwise the subclass also should be declared as abstract.
- We cannot create the objects for abstract classes because they are
not implemented completely. But once abstract methods are
implemented inside subclass, we can instantiate the subclass and
assign subclass object to abstract super class reference which is
called as narrow casting.
- Whenever narrow casting is done, by using super class object we
can access methods of super class only, in order to access direct
methods of subclass using super class object, we need to call the
subclass method using dynamic calling approach.
- Narrow casting is a process of switching the object from more
detailed view to less detailed view.
Visibility at Class Level - Public (default), protected, private and
abstract

Public classes:
- The default visibility at class level is public.

- Public class can be instantiated anywhere (within same class/


inside subclasses /outside classes).

- Public class can be inherited.

- The subclass inheriting the super public class is also created as


public by default.

- The sub class inheriting the super public class can be created as
explicit protected/private class.

Protected classes:

- Create protected is the keyword used as part of local class


definition to create the protected classes.

- Protected class can be inherited.

- Protected classes cannot be instantiated outside the classes but


they can be instantiated inside same classes as well as inside
subclass methods.

- The subclass inheriting protecting class is also created as


protected by default.

- The subclass inheriting protected class can be created as explicit


public class (or) private class.

- Private classes:

- Create private is the keyword used for creating the private classes.

- Private class can be inherited.


- Private class cannot be instantiated outside the classes and inside
subclasses, but they can be instantiated within the same class. In
order to instantiate private classes inside subclasses or inside any
other independent class, the private super class should consider
sub class / independent class as friend.

- The subclass inheriting the private class is also created as private


by default.

- By default, the subclass inheriting the private class cannot be


created as explicit public class / explicit protected class. This can
be made possible if the super private class considers subclass as
friend.

Friend keyword:

In local classes “Friend” is a keyword used as part of class


definition to consider another class as friend.

In this case friend class should be forward declared by using


“Deferred” keyword. This deferred keyword indicates that the
class is defined somewhere else in the program and not at the
beginning of the program.

ABSTRACT CLASS:
Abstract is a class which contains one or more Abstract methods.
- An abstract method is a method which is just declared but not
implemented.

- We declare a method as abstract when we are not sure about the


implementation.
- The implementation for the abstract methods can be provided as
part of subclasses.

- In local classes, abstract methods are declared by using a keyword


“abstract”.

- If a class contains an abstract method, the class also should be


declared as abstract.

- Abstract methods are declared only in public and protected


sections.

- Abstract methods are also called as non-concrete methods.

- Static methods cannot be declared as abstract as they cannot be


redefined inside subclasses

- Constructors cannot be declared as abstract as they cannot be


redefined inside subclasses

- The subclass whichever is inheriting the abstract class must


implement all the abstract methods of the abstract super class
otherwise the subclass also should be declared as abstract.

- We cannot create the objects for abstract classes because they are
not implemented completely. But once abstract methods are
implemented inside subclass, we can instantiate the subclass and
assign subclass object to abstract super class reference which is
called as narrow casting.

- Whenever narrow casting is done, by using super class object we


can access methods of super class only, in order to access direct
methods of subclass using super class object, we need to call the
subclass method using dynamic calling approach.

- Narrow casting is a process of switching the object from more


detailed view to less detailed view.

INTERFACES
- An interface is a pure abstract class i.e. by default all the methods
of interface are abstract.

- Interface components doesn’t contain any explicit visibility


section, by default all the components are public.

- Interfaces cannot contain method implementations, it contains


only method declarations.

- A local class whichever want to implement an


interface(local/global) , the class must declare the interface in the
public section by using ‘interfaces’ keyword and the class must
implement all the interface methods, Otherwise the class should
be declared as “abstract” and also the methods which are not
implemented should be flagged (declared) as abstract. In these
cases, we must consider another class inheriting the abstract class
and provide the implementation for the abstract methods.

- The class which ever implements the interface is called as


implementation class.

- Interface is always implemented in public section of local class.

- By using interfaces we can implement the concept of multiple


inheritances.
- I.e. a class can implement any number of interfaces.

- Whenever we refer to interface components outside the interface,


the component of the interface must be prefixed with the name of
the interface.

- Interfaces cannot contain constructors

- If a class implements any interface and if that implemented


interface contains any included interfaces, then class must also
implement all the methods of the included interfaces.

- If a global class has to implement one or more global interfaces,


then global class must declare those interfaces in the interfaces
tab.

Aliases
Aliases are the alternative names provided for interface
components .i.e. whenever we refer to the interface components
outside the interface, it must be prefixed with the name of the
interface. This lengthy naming representation can be avoided by
declaring “aliases” for interface components.
- Aliases can be declared in any of the visibility sections inside
implementation class
- Aliases declared inside interfaces are always public
- Aliases can be declared either in interfaces / in the
implementation class.
- If the aliases as to be declared in the interface it can be declared
only for included interfaces components and that alias will be
created as component of interface.
Note: The included interfaces can be referred directly in the
implementation class .i.e. the interface which is included in other
interface can be directly referred in the implementation class.
Note: As Part of global classes, If a global class Includes any
interface in the interfaces tab and the checkbox ‘final’ is selected,
it indicates that these interface methods cannot be further
implemented (re-defined) in other classes.

INTERRFACE:
Example: Local Interfaces
REPORT ZCLASS8_INTERFACE.

interface rectangle.
constants : length type i value 10,
breadth type i value 5.
methods : area,
perimeter.
endinterface.

interface square.
constants side type i value 10.
methods : area,
perimeter.
endinterface.
class lcl_impl DEFINITIOn. "implementation class
PUBLIC SECTION.
interfaces : rectangle,
square.
PROTECTED SECTION.
data res type i.
endclass.

class lcl_impl IMPLEMENTATION.

method rectangle~area.
res = rectangle~length * rectangle~breadth.
write :/ 'Area of rectangle is ',res.
endmethod.

method rectangle~perimeter.
res = 2 * ( rectangle~length * rectangle~breadth ).
write :/ 'Perimeter of rectangle is ',res.
endmethod.

method square~area.
res = square~side * square~side.
write :/ 'Area of square is ',res.
endmethod.

method square~perimeter.
res = 4 * square~side.
write :/ 'Perimeter of squareis ',res.
endmethod.

endclass.

START-OF-SELECTION.
data r type ref to rectangle.
*create object r. "syntax error

write :/ 'Implementation class object ob...'.


data ob type ref to lcl_impl.
create object ob.

call method : ob->rectangle~area,


ob->rectangle~perimeter.
uline.
write :/ 'Implementation class object ob --> assigned to interface
rectangle reference r.'.
r = ob. "narrow casting
call method : r->area,
r->perimeter.

uline.
data s type ref to square.
*create object s. "syntax error

write :/ 'Implementation class object ob...'.

call method : ob->square~area,


ob->square~perimeter.

uline.
write :/ 'Implementation class object ob --> assigned to interface square
reference s.'.
s = ob. "narrow casting
call method : s->area,
s->perimeter.
Example: Implementing interfaces partially
REPORT Z1030OOPS36.

interface intf1.
methods : m1,
m2,
m3.
endinterface.

class lcl_impl DEFINITION ABSTRACT.


public SECTION.
interfaces intf1 ABSTRACT METHODS m2 m3.
endclass.

class lcl_impl IMPLEMENTATION.


method intf1~m1.
write :/ 'Inside method m1...'.
endmethod.
endclass.

class lcl_impl2 DEFINITION INHERITING FROM lcl_impl.


PUBLIC SECTION.
methods : intf1~m2 REDEFINITION,
intf1~m3 REDEFINITION.
endclass.

class lcl_impl2 IMPLEMENTATION.

method intf1~m2.
write :/ 'Inside method m2...'.
endmethod.
method intf1~m3.
write :/ 'Inside method m3...'.
endmethod.

endclass.

START-OF-SELECTION.
data ob type ref to lcl_impl2.
create object ob.

call method : ob->intf1~m1,


ob->intf1~m2,
ob->intf1~m3.
Example: Aliases for Local Interface components
REPORT Z1030OOPS37.

INTERFACE intf1.
methods : m1,
m2.
endinterface.

class lcl_impl DEFINITION.


public SECTION.
interfaces intf1.
aliases a1 for intf1~m1.
PROTECTED SECTION.
aliases a2 for intf1~m2.
endclass.
class lcl_impl IMPLEMENTATION.
* method intf1~m1. "(or)
method a1.
write :/ 'Inside method m1...'.
call method a2.
endmethod.

* method intf1~m2. "(or)


method a2.
write :/ 'Inside method m2...'.
endmethod.

endclass.

START-OF-SELECTION.
data ob type ref to lcl_impl.
create object ob.

call method ob->a1.


*call method ob->a2. "syntax error
Example: Including a local Interface inside another local Interface
declaration
REPORT Z1030OOPS38.

interface intf1.
methods : m1,
m2.
endinterface.

interface intf2.
methods m3.
interfaces intf1.
endinterface.

class lcl_impl DEFINITION.


PUBLIC SECTION.
interfaces intf2.
endclass.

class lcl_impl IMPLEMENTATION.


method intf2~m3.
write :/ 'inside method m3 of intf2...'.
endmethod.

method intf1~m1.
write :/ 'inside method m1 of intf1...'.
endmethod.

method intf1~m2.
write :/ 'inside method m2 of intf1...'.
endmethod.

endclass.

START-OF-SELECTION.
data ob type ref to lcl_impl.
create object ob.

call method : ob->intf1~m1,


ob->intf1~m2,
ob->intf2~m3.
Procedure for Classical Reporting:
1. Generate a selection screen for reading
user input
2. Retrieve data from database tables
based on user input
3. Process the internal table for display
(write)
--> displays the output in L.P.S
Procedure for developing ALV reports using classes:
1. Create a Module pool screen
2. Place custom control component on
the module pool screen
3. Create the object for container class
'CL_GUI_CUSTOM_CONTAINER’ linking with custom control (physical
control on module pool screen toolbox)
4. Create the object for grid class
'CL_GUI_ALV_GRID' linking with container object
5. Retrieve the data to be displayed in the
ALV grid
6. Generate the field catalog for the fields of ALV grid
7. Generate the layout for the ALV grid
(optional)
8. Display the data in the ALV grid using
the method
'SET_TABLE_FOR_FIRST_DISPLAY' of the grid class 'cl_gui_alv_grid'
Field catalog Generation:
Whenever we display the data in the form of ALV report, we need to
provide field catalog information; otherwise the report execution leads
to ABORT error. In case of ALV reporting using classes, we can generate
field catalog in 2 ways.
1. Using the function module ‘LVC_FIELDCATALOG_MERGE’: As part
of this F.M call, we need to provide the dictionary structure
containing the required fields (fields provided as part of internal
table) , based on this dictionary structure fields SAP constructs the
fieldcatalog internal table and returns which is of type
‘LVC_T_FCAT’.
2. Manually

Note: In either of the above 2 process, once the fieldcatalog is


generated, we need to pass it as a value to the parameter
‘IT_FIELDCATALOG’ as part of the method call
‘SET_TABLE_FOR_FIRST_DISPLAY’.
Excluding the standard ALV toolbar pushbuttons:
For this, we need to identify the function codes of the ALV toolbar
buttons and prepare an internal table with those function codes and
pass the internal table as a value to the parameter
“IT_TOOLBAR_EXCLUDING” of the method
“SET_TABLE_FOR_FIRST_DISPLAY” of the class ‘CL_GUI_ALV_GRID’. All
the ALV toolbar buttons function codes exist in the form of constant
public attributes with the naming standard ‘MC_FC…’ of the class
‘CL_GUI_ALV_GRID’.
Note: For excluding entire ALV toolbar in the Report, set the field
‘NO_TOOLBAR’ to ‘X’ as part of Layout.
ALV Column as pushbutton:
For displaying the entire ALV column as pushbutton, set the field ‘STYLE’
as part of field catalog. All the styles exist in the form of constant
attributes with the naming standard ‘MC_STYL’ as part of the class
‘CL_GUI_ALV_GRID’.
Displaying ALV cells as pushbutton:
Procedure:
Step1:
Take an additional column in the final internal table which is of table
type “LVC_T_STYL”.
Step 2:
Before displaying the final data in the ALV grid, loop the final internal
table and set the button style (MC_STYLE_BUTTON) to ALV cells based
on the condition.
As part of layout generation, set the field “stylefname” whose value is
name of the additional field which holds the style.
ALV Column Coloring:
For coloring the entire ALV column, set the field ‘EMPHASIZE’ to a color
coding as part of field catalog.
ALV Row Coloring:
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

Standard Event handling process in ALV Reports using classes:


1. They are declared by SAP itself as part of standard SAP classes
2. Event Business logic needs to implemented as part of custom class
implementation (inside event handler method)
3. Raised by SAP itself depending on user actions (from. The std.
methods)
4. Register the handlers for execution of event handler methods
Note:
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: 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: 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: 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’: 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’: 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’: 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’: 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’.

Layout Variant notes:


SAP users want to configure ALV grid layout settings and change the
standard ABAP report according to their requirements.
SAP users want to customize the ALV layout and create custom layouts
for own use or for global use.
For an ABAP developer this means enabling SAP system users to create
User or Global layout variants for the ALV grid display.
If the ABAP programmer is using method set_table_for_first_display of
cl_gui_alv_grid type ALV grid object, then it is very easy to let SAP users
create own layout variants and manage layout variants.
To manage this task, ABAP developers can use is_variant and i_save
export parameters of the set_table_for_first_display method call.
i_save parameter has 4 possible values.
U: An i_save parameter value "U" means that the user can only save
user-specific layouts.
X: An i_save parameter value "X" means that the users are enabled to
save only global layouts. So other users can also use these global
layouts too.
A: An i_save parameter value "A" means that the SAP user can save
both user specific and global layouts.
Space: An empty or space i_save parameter value which is default for
i_save means users cannot save own layouts either global or user
specific layout.

Persistence service:
It is used for storing the state of object (CURRENT values of the object)
permanently in the database.
By default, the life time of the object is local to the program i.e. once a
program execution is completed, memory allocated for the object will
be destroyed by the garbage collector.
Persistence object means -> permanent object
Any database operation (CRUD operations) can be achieved by using
“persistent service”.
Persistence service is implemented by creating persistence classes.
Persistence class is always created globally (class builder).
The naming standard of persistent class is ‘ZCL_<classname> ‘ /
’YCL_<classname>’.
Persistence class is always created as protected class.
Whenever a persistence class is created, SAP creates two more classes
internally.
a. Base agent class: (abstract class) i.e. ‘ZCB_<class name>’.
b. Actor /agent class: (Private class) i.e. “ZCA_<class name>”.
Base agent class: is a friend of persistence class and it is super class of
ACTOR / AGENT class.
Actor class: is a subclass of Base agent class
By Default, Persistence class implements a standard interface
‘IF_OS_STATE’ which is responsible for storing the state of the object
permanently in the database.

Persistence service can be implemented in two ways:


1. Business key identity.
2. GUID (Global Unique Identifier).
Persistence service using business key identity:
- 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
2. Create the persistence class.
Persistence class: zcl_1130_persistence_key
Base agent class: zcb_1130_persistence_key
Actor/agent class: zca_1130_persistence_key
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.
1. Create _Persistent
2. Delete_Persistent.
3. 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:


Step1: 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”.

You might also like