Professional Documents
Culture Documents
NOTES WITH
Programs
VINAYAKA
Features Of OOPS: -
1) Encapsulation
2) Data Abstraction
3) Inheritance
4) Polymorphism
Application Of OOPS: -
1) BAPI’S
2) BADI’S
3) Enhancement Frame Work
4) Webdynpro
5) HR-ABAP
6) CRM-Technical
7) SRM
8) EP
9) BSP……………
Class: - A class is a user defined data type which is the collection of different type of components.
A class only provides a template it’s doesn’t allocate a memory.
Object: - An instance of a class is called as an object.
Whenever we instance a class memory will be allocated.
1|Pa g e Santosh P
Definition of class
Class <class name> definition.
Declaration of components.
Endclass.
Implementation class
Class <class name> implementation.
Implementation of methods.
Endclass.
Object Creation: -
1) Create reference for the class
Syntax: -
Data <ref.name> type ref to <class name>
Note: - Whenever an Object is created memory will be allocated for the attributes of the class and
attribute gets initialized to default values.
Instance Instance
Constructor
Instance Static
Static Static
Constructor
2|Pa g e Santosh P
1) OOPS Concepts
2) ALV Reporting Using Class
3) ALV Reporting Using Function Modules
REPORT Z915AM_OOPS1.
WRITE :/ K->EMPNO,
K->ENAME.
K->EMPNO = 1.
K->ENAME = 'abc'.
WRITE:/ K->EMPNO,
K->ENAME.
OUTPUT:
3|Pa g e Santosh P
Z915AM_CLASS1: (GLOBAL CLASS) SE24:
Z915AM_OOPS2:
REPORT Z915AM_OOPS2.
write :/ m->empno,
m->ename.
OUTPUT:
4|Pa g e Santosh P
Interacting with methods in local classes: -
Note: - Whenever a report program contains class implementation and explicitly we need handle
the event start-of-selection to indicate the starting point of program.
Z915AM_OOPS3:
REPORT Z915AM_OOPS3.
method m2.
write :/ empno,
ename.
endmethod.
endclass.
start-of-selection.
5|Pa g e Santosh P
k->m1( ).
k->m2( ).
OUTPUT:
In case of other object oriented language a method can return exactly one value which
done by using return keyword.
In case of ABAP a method can return any number of values by declaring those many
number o exporting (or) importing (or) changing parameters.
It the method contains returning parameters it cannot contains exporting (or) changing
parameters.
Z915AM_OOPS4:
REPORT Z915AM_OOPS4.
6|Pa g e Santosh P
protected section.
data : empno type i,
ename(20) type c.
endclass.
method m2.
write :/ empno,
ename.
endmethod.
endclass.
start-of-selection.
7|Pa g e Santosh P
Z915AM_CLASS2:
method M1.
empno = x.
ename = y.
call method m2.
endmethod.
method M2.
write :/ empno,
ename.
endmethod.
8|Pa g e Santosh P
Z915AM_OOPS5:
REPORT Z915AM_OOPS5.
OUTPUT:
Z915AM_OOPS6:
REPORT Z915AM_OOPS6.
class abc definition.
public section.
methods m1 importing x type i
y type i
exporting m type i
n type i.
endclass.
class abc implementation.
method m1.
m = x + y.
n = x - y.
endmethod.
9|Pa g e Santosh P
endclass.
start-of-selection.
data k type ref to abc.
create object k.
Z915AM_OOPS7:
REPORT Z915AM_OOPS7.
start-of-selection.
10 | P a g e Santosh P
p_y type i.
write :/ lv_z.
OUTPUT:
An exception is runtime error which is raised during the program execution if the
exception is not handling the program will be terminated.
It the exception is raised in try blocked SAP creates the appropriate exception class object
and the control is transfer to catch block.
Inside the catch block we need to handle the exception by writing the appropriate
exception handling statements.
All the exception classes provided by SAP start with the naming standard
‘CX_SY_...........’
11 | P a g e Santosh P
Note: - As part of catch block declaration we need to specify the exception class which is
reasonable for rising exception if the developer is not sure of the exception class we can use the
exception class ‘CX_ROOT’.
CX_ROOT is a super class for all the exception classes and it can handle any kind of
exception.
Z915AM_OOPS8:
REPORT Z915AM_OOPS8.
start-of-selection.
12 | P a g e Santosh P
OUTPUT:
Z915AM_OOPS9:
REPORT Z915AM_OOPS9.
CLEAR STR.
CALL METHOD K->IF_MESSAGE~GET_LONGTEXT
RECEIVING
RESULT = STR.
13 | P a g e Santosh P
PROGRAM_NAME = PRGNAME
INCLUDE_NAME = INCNAME
SOURCE_LINE = POS.
START-OF-SELECTION.
DATA OB TYPE REF TO ABC.
CREATE OBJECT OB.
CX_SY_ZERODIVIDE:
14 | P a g e Santosh P
Capturing system defined exception message: -
These exception are declared and raised SAP as a developer we need to handle try and catch.
These exceptions are declaring and handle are raised developers itself.
3) Handle the exception while calling the method by checking sy-subrc status.
Z915AM_OOPS10:
REPORT Z915AM_OOPS10.
start-of-selection.
data ob type ref to abc.
create object ob.
15 | P a g e Santosh P
x = 10
y=0
importing
z = lv_z
exceptions
divideerror = 1
others = 2.
if sy-subrc eq 0.
write :/ 'division is ',lv_z.
elseif sy-subrc eq 1.
write :/ 'Cannot divide by zero'.
elseif sy-subrc eq 2.
write :/ 'Unknown error'.
endif.
OUTPUT:
Z915AM_CLASS3:
method M1.
if y eq 0.
raise divideerror.
16 | P a g e Santosh P
else.
z = x / y.
endif.
endmethod.
Z915AM_OOPS11:
REPORT Z915AM_OOPS11.
if sy-subrc eq 0.
write :/ lv_z.
elseif sy-subrc eq 11.
message 'Cannot divide by zero' type 'I'.
elseif sy-subrc eq 22.
message 'Unknown error' type 'I'.
endif.
OUTPUT:
17 | P a g e Santosh P
Instance attribute: -
These attribute are specific to an object and they are declared by using the keyword data
in local classes.
For each instance attribute separate memory will be allocated they can be an accessed
only by using the object of the class.
Static attribute: -
They are not specific to any object and they are declared by using the keyword
‘CLASS_DATA’ in local classes.
For the static attribute memory will be allocated only when the first object is created the
remaining objects points to be same memory location they are also called as class
variables.
They can be accessed either by using the object are by using the class name.
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 both instance and static attributes.
Static methods: -
In local class they are declaring the keyword ‘CLASS-METHOD’ in the local classes they
can be accessed either by using object are by using class name. They access only static attributes.
Z915AM_OOPS12:
REPORT Z915AM_OOPS12.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
ob1->x = 10.
ob1->y = 20.
18 | P a g e Santosh P
ob1->y.
Z915AM_OOPS13:
REPORT Z915AM_OOPS13.
protected section.
data x type i.
class-data y type i.
endclass.
method m2.
y = 20.
* x = 10.
write: y.
endmethod.
endclass.
19 | P a g e Santosh P
start-of-selection.
call method abc=>m2.
abc=>m2( ).
*abc=>m1( ).
OUTPUT:
Constructors: -
A constructor is a special method used for initialized for attributes of class it special because
it cannot be called explicitly it will be called implicitly.
1) Instance
2) Static
1) When we accesses the static components of the class using the class name before creating any
objects.
2) When we create the first object of the class.
3) It is not specific to any object it cannot contains any parameters and exception.
Note: -
Instance constructor is executed only once in the life time of object.
Static constructor is executed only once in a life time of class.
20 | P a g e Santosh P
Z915AM_OOPS14:
REPORT Z915AM_OOPS14.
method m2.
write :/ empno,ename.
endmethod.
endclass.
start-of-selection.
* data ob type ref to abc.
* create object ob.
*
*call method ob->constructor. " u cannot specify constructor directly.
*call method ob->m2.
*call method ob->m2.
21 | P a g e Santosh P
Z915AM_OOPS15:
REPORT Z915AM_OOPS15.
method display.
write :/ empno,ename.
endmethod.
endclass.
start-of-selection.
22 | P a g e Santosh P
Z915AM_OOPS16:
REPORT Z915AM_OOPS16.
method class_constructor.
write :/ 'inside static const'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
Note: - If a class contains both instance and static constructor and when we create the first object.
1st the static constructor is executed and next instance constructor for rest of the objects only
instance constructor get executed.
23 | P a g e Santosh P
Z915AM_OOPS15:
REPORT Z915AM_OOPS15.
method display.
write :/ empno,ename.
endmethod.
endclass.
start-of-selection.
OUTPUT:
24 | P a g e Santosh P
Z915AM_OOPS16:
REPORT Z915AM_OOPS16.
method class_constructor.
write :/ 'inside static const'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
OUTPUT:
25 | P a g e Santosh P
Z915AM_OOPS17:
REPORT Z915AM_OOPS17.
method class_constructor.
write :/ 'inside static const'.
endmethod.
endclass.
start-of-selection.
*abc=>x = 10.
data ob1 type ref to abc.
create object ob1.
OUTPUT:
26 | P a g e Santosh P
NORMAL METHOD SPECIAL METHOD CONSTRUCTOR
A method can have any type o parameters. Instance constructor can have importing
parameters and static constructor cannot have
any parameters.
It can be called any know of times in the Instance constructor will be called only once
lifetime of an object. in the lifetime of every object where as static
constructor will be called only once in the
lifetime of class.
27 | P a g e Santosh P
Z915AM_OOPS18:
REPORT Z915AM_OOPS18.
28 | P a g e Santosh P
OUTPUT:
Z915AM_OOPS19:
REPORT Z915AM_OOPS19.
start-of-selection.
data ob type ref to abc.
create object ob.
data r type i.
try.
call method ob->m1
exporting
x = 10
y=0
importing
z = r.
catch cx_sy_zerodivide.
message 'Cannot divide by zero' type 'I'.
endtry.
29 | P a g e Santosh P
OUTPUT:
PRESS ENTER:
Z915AM_OOPS20:
REPORT Z915AM_OOPS20.
data z type i.
try.
perform abc using p_x p_y changing z.
catch cx_sy_zerodivide.
write :/ 'Cannot divide by zero'.
endtry.
write :/ 'Diviison is ',z.
OUTPUT:
30 | P a g e Santosh P
Friend classes: - By default outside the class a object can access only public components of the
class directly.
By using friend classes to enable the object to access any components of the class directly
irresponsibility of the visibility for this consider the following session.
If class A considers class B as friend by inside class B methods we can instantiate class A
and use the instance we can access all the component of class A directly irrespective of
the visibility.
Z915AM_OOPS21:
REPORT Z915AM_OOPS21.
method m2.
write :/ 'inside protected method m2'.
endmethod.
method m3.
write :/ 'inside private method m3'.
endmethod.
endclass.
start-of-selection.
data k type ref to pqr.
create object k.
In the above case the class B should be forward declared by using the keyword ‘deferred’.
Deferred keyword indicates to SAP that the class definition has been delayed and it has been
declared same where else in the program.
32 | P a g e Santosh P
method M1.
write :/ 'inside method m1'.
endmethod.
method M2.
write :/ 'inside method m2'.
endmethod.
method M3.
write :/ 'inside method m3'.
endmethod.
Z915AM_CLASS5:
33 | P a g e Santosh P
method M4.
write :/ 'inside m4'.
data ob type ref to z915am_class4.
create object ob.
call method : ob->m1,
ob->m2,
ob->m3.
endmethod.
Z915AM_OOPS22:
REPORT Z915AM_OOPS22.
Inheritance: -
It is the process o acquiring the properties of other entity (class). The advantage of
inheritance is reusability. They are three types of inheritance.
1) Single
2) Multiple
3) Multilevel
The class which gives the properties is called as super class are base class and the class
which takes the properties is called as subclass (or) derived class.
34 | P a g e Santosh P
Only public and protected components can be inherited.
In local classes we need to use the keyword inheriting from for achieving inheritance.
1) Single inheritance: -
2) Multiple inheritance: -
Note: - In ABAP we cannot implement multiple inheritance directly we can implemented indirectly
through the concept of interface.
3) Multilevel inheritance: -
CLASS B
CLASS B CLASS C
CLASS C
Single inheritance
Multilevel inheritance Multiple inheritance
Z915AM_OOPS24:
REPORT Z915AM_OOPS24.
35 | P a g e Santosh P
colour = 'green'.
endmethod.
method display.
write :/ wheels,brakes,colour.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to cycle.
create object ob1.
36 | P a g e Santosh P
create object ob3.
Z915AMCYCLE:
method SETCYCLE.
wheels = 2.
brakes = 2.
colour = 'blue'.
endmethod.
method DISPLAY.
write :/ wheels,brakes,colour.
endmethod.
37 | P a g e Santosh P
Z915AMSCOOTER:
method SETSCOOTER.
wheels = 2.
brakes = 4.
colour = 'red'.
endmethod.
38 | P a g e Santosh P
Z915AMCAR:
method SETCAR.
wheels = 4.
brakes = 5.
colour = 'cyan'.
endmethod.
39 | P a g e Santosh P
Z915AM_OOPS25:
REPORT Z915AM_OOPS25.
ob2->setscooter,
ob2->display,
ob3->setcar,
ob3->display.
OUTPUT:
40 | P a g e Santosh P
Z915AM_OOPS26:
REPORT Z915AM_OOPS26.
OUTPUT:
Polymorphism: -
Poly Many
Morph Forms
Ism behavior
Method overloading: -
If a class contains two method with the same name but different signature it is called as
method overloading.
Z915AM_OOPS27:
REPORT Z915AM_OOPS27.
41 | P a g e Santosh P
ENDMETHOD.
ENDCLASS.
method overloading
Z915AM_OOPS28:
REPORT Z915AM_OOPS28.
START-OF-SELECTION.
DATA OB TYPE REF TO PQR.
CREATE OBJECT OB.
OUTPUT:
42 | P a g e Santosh P
Method overriding: -
Whenever a sub class wants to override the super class method a sub class wants to
declare the super class method in the subclass by using REDEFINITION keyword.
While redefined the methods we cannot change the visibility of the method.
Whenever a subclass overrides the super class method it is always recommended to call
the super class method version in the subclass by using super keyword.
Super keyword is used for referring to super class components from the sub class.
Z915AM_CLASS6:
method M1.
write :/ 'inside method m1 of super class'.
endmethod.
Z915AM_CLASS7:
43 | P a g e Santosh P
method M1.
write :/ 'inside m1 of subclass'.
CALL METHOD SUPER->M1.
endmethod.
Z915AM_OOPS29:
REPORT Z915AM_OOPS29.
OUTPUT:
Final keyword: -
Z915AM_OOPS30:
REPORT Z915AM_OOPS30.
44 | P a g e Santosh P
CLASS ABC IMPLEMENTATION.
METHOD M1.
WRITE :/ 'inside m1 of super class'.
ENDMETHOD. "m1
ENDCLASS. "abc IMPLEMENTATION
START-OF-SELECTION.
DATA OB TYPE REF TO PQR.
CREATE OBJECT OB.
OUTPUT:
Z915AM_OOPS31:
REPORT Z915AM_OOPS31.
45 | P a g e Santosh P
class pqr definition inheriting from abc.
public section.
methods m2.
methods m1 redefinition.
endclass.
start-of-selection.
data ob type ref to pqr.
create object ob.
OUTPUT:
Z915AM_OOPS32:
REPORT Z915AM_OOPS32.
method class_constructor.
write :/ 'inside static const. of super class'.
endmethod.
endclass.
method class_constructor.
write :/ 'inside static const. of sub class'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to pqr.
create object ob1.
OUTPUT:
When a super class contains static and instance constructor and id sub class contains only the static
constructor and if sub class contains only the static constructor in this case if we instantiate the sub
class then the static constructor are executed from super class to sub class and then the instantiate
constructor of the super class will be executed.
Z915AM_OOPS33:
REPORT Z915AM_OOPS33.
47 | P a g e Santosh P
endmethod.
method class_constructor.
write :/ 'inside static const. of super class'.
endmethod.
endclass.
method class_constructor.
write :/ 'inside static const. of sub class'.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to pqr.
create object ob1.
OUTPUT:
Note: - If the super class and sub class contains respective instance constructor it must for subclass
instance constructor to call the super class instance constructor this is done by using super keyword.
This is the only place where the constructor can be are must be called explicitly.
Note: - If the super class and sub class contains respective static and instance constructor and if
instantiate the sub class first the static constructor are executed to super class to subclass and when
the instantiate constructor will executed subclass to super class.
48 | P a g e Santosh P
Me keyword: - Me keyword refer to current object execution it is used to differentiate both
attribute and method parameters whenever attribute and parameter names are same.
Z915AM_OOPS34:
REPORT Z915AM_OOPS34.
start-of-selection.
data ob type ref to abc.
create object ob.
data r type i.
OUTPUT:
49 | P a g e Santosh P
Visibility of component level: -
1) Public section
2) Protected section
3) Private section
1) Public
2) Protected
3) Private
4) Abstract
Protected classes: - Protected classes can be inherited but cannot be instantiated outside the class
but it can be instantiated within the sub class method.
The sub class inheriting the protected class is also created as protected by default to create
the sub class as explicit public class we need to use extension create public as part of sub
class definition.
Z915AM_OOPS36:
REPORT Z915AM_OOPS36.
start-of-selection.
data k type ref to pqr.
create object k.
call method k->m1.
50 | P a g e Santosh P
OUTPUT:
protected classes.
Private classes: -
Z915AM_OOPS37:
REPORT Z915AM_OOPS37.
OUTPUT:
51 | P a g e Santosh P
Z915AM_OOPS38:
REPORT Z915AM_OOPS38.
OUTPUT:
second usage of friend keyword
Abstract class: -
It is a class which contains at least one abstract method. Abstract method is a method
which is just declared but not implemented in local class they are declared by using the
keyword abstract.
It a class contains at least one abstract method then the entity class should be declared as
abstract.
Abstract methods are always declared in public (or) protected section.
We cannot instantiate the abstract classes because they are not fully implemented.
The class which ever inheritance the abstract class can implement the abstract method of
the abstract class otherwise the subclass will declared as abstract.
Abstract methods are also called as non-concerted methods.
We declared method as abstract when we are not sure about the implementation but we
are sure that the other classes as to use the same methods.
Z915AM_OOPS39:
REPORT Z915AM_OOPS39.
52 | P a g e Santosh P
CLASS RESTAURANT IMPLEMENTATION.
METHOD SET.
TABLENO = 123.
STEWARD = 'abc'.
ENDMETHOD. "set
METHOD DISPLAY.
WRITE :/ TABLENO,STEWARD.
ENDMETHOD. "display
ENDCLASS. "restaurant IMPLEMENTATION
START-OF-SELECTION.
53 | P a g e Santosh P
DATA R TYPE REF TO RESTAURANT.
OUTPUT:
54 | P a g e Santosh P
Z915AM_REST:
method SET.
tableno = 1.
steward = 'abc'.
endmethod.
method DISPLAY.
write :/ tableno,steward.
endmethod.
Z915AM_CHEQUE:
55 | P a g e Santosh P
method PAYMENT.
cqno = 123.
bank = 'yes'.
amount = 455.
write :/ cqno,bank,amount.
endmethod.
Z915AM_OOPS40:
REPORT Z915AM_OOPS40.
OUTPUT:
56 | P a g e Santosh P
Interfaces: - It is pure abstract class i.e. by default all methods of interface are abstract. By using
interfaces we can implement multiple inheritances.
A class can implement any number of interfaces which is nothing but multiple inheritance
whenever the interface component are referred the outside of the interface they must be prefixed
with the name of the interface.
Z915AM_OOPS41:
REPORT Z915AM_OOPS41.
INTERFACE RECTANGLE.
CONSTANTS : LENGTH TYPE I VALUE 10,
BREADTH TYPE I VALUE 5.
METHODS : AREA,
PERIMETER.
ENDINTERFACE. "rectangle
INTERFACE SQUARE.
CONSTANTS SIDE TYPE I VALUE 5.
METHODS : AREA,
PERIMETER.
ENDINTERFACE. "square
57 | P a g e Santosh P
CLASS ABC IMPLEMENTATION.
METHOD RECTANGLE~AREA.
RES = RECTANGLE~LENGTH * RECTANGLE~BREADTH.
WRITE :/ 'Area of rectangle is :',RES.
ENDMETHOD. "rectangle~area
METHOD RECTANGLE~PERIMETER.
RES = 2 * ( RECTANGLE~LENGTH + RECTANGLE~BREADTH ).
WRITE :/ 'perimeter of rectangle is :',RES.
ENDMETHOD. "rectangle~perimeter
METHOD SQUARE~AREA.
RES = SQUARE~SIDE * SQUARE~SIDE.
WRITE :/ 'Area of square is :',RES.
ENDMETHOD. "square~area
METHOD SQUARE~PERIMETER.
RES = 4 * SQUARE~SIDE.
WRITE :/ 'perimeter of square is :',RES.
ENDMETHOD. "square~perimeter
START-OF-SELECTION.
DATA R TYPE REF TO RECTANGLE.
DATA S TYPE REF TO SQUARE.
58 | P a g e Santosh P
OUTPUT:
Aliases: - aliases are the alternative names provided to the interface components i.e. whenever the
interface components is referred outside the interface declaration it must be prefixed with the name
of interface we can avoid the lengthy naming standard by declaring the aliases by the interface
components these aliases must be declared in the definition of a class whichever the implementing
the interface. By using aliases we can also change the visibility of the interface components.
Can contain both abstract and non-abstract Can contain only abstract methods.
methods.
Explicitly we need to use abstract keyword. By default all methods are abstract.
Abstract methods can be declared in public or All components of interface by default are
protected section. public.
A class can inherit only one abstract class. A class can implement any know of interfaces.
Abstract class components are directly referred Interface components must be prefixed with the
in subclass. name of the interface.
59 | P a g e Santosh P
Z915AM_OOPS42:
REPORT Z915AM_OOPS42.
INTERFACE ABC.
METHODS : M1,
M2.
ENDINTERFACE. "abc
METHOD A2.
WRITE :/ 'inside m2'.
ENDMETHOD. "a2
START-OF-SELECTION.
DATA K TYPE REF TO PQR.
CREATE OBJECT K.
OUTPUT:
60 | P a g e Santosh P
Z915AM_INT1:
Z915AM_IMPL:
method Z915AM_INT1~M1.
write :/ 'inside m1'.
call method a2.
endmethod.
method Z915AM_INT1~M2.
write :/ 'inside m2'.
endmethod.
61 | P a g e Santosh P
Z915AM_OOPS43:
REPORT Z915AM_OOPS43.
OUTPUT:
Z915AM_OOPS44:
REPORT Z915AM_OOPS44.
INTERFACE ABC.
METHODS : M1,
M2,
M3.
ENDINTERFACE. "abc
62 | P a g e Santosh P
CLASS PQR IMPLEMENTATION.
METHOD ABC~M1.
WRITE :/ 'inside m1'.
ENDMETHOD. "abc~m1
ENDCLASS. "pqr IMPLEMENTATION
METHOD ABC~M3.
WRITE :/ 'inside m3'.
ENDMETHOD. "abc~m3
ENDCLASS. "xyz IMPLEMENTATION
START-OF-SELECTION.
DATA K TYPE REF TO XYZ.
CREATE OBJECT K.
OUTPUT:
63 | P a g e Santosh P
Z915AM_OOPS45:
REPORT Z915AM_OOPS45.
interface pqr.
methods : m1,
m3.
endinterface.
method m1.
write :/ 'inside m1'.
endmethod.
method pqr~m3.
write :/ 'inside m3'.
endmethod.
endclass.
start-of-selection.
data ob type ref to xyz.
create object ob.
64 | P a g e Santosh P
OUTPUT:
Persistence service: - It is used for storing the state of an object formality it is similarly to
serialization java and .net. This service is implemented by using persistence classes. This service is
implemented in two ways.
1) By using business key identity.
2) By using GUID (global unique identifier)
Base agent class is always created as abstract the class and it is the friend of persistence
class.
Actor class is always created as private class and it is a sub class of base agent class.
Once the persistence class is created it needs to mapped with the corresponding database
table.
In this we consider the primary key fields of the database table as business key identity
which is used for identity the object uniquely.
In this case when the persistence class is mapped with the database tables SAP adds the
fields of the database as the attributes of the persistence class.
Also it creates the following methods as part of the base agent class.
1) Create_persistence.
2) Delete_persistence.
3) Get_persistence.
The above three method are public instance methods which gets inherited to actor class.
We need to use the above methods to interact with the persistence service.
A part from this SAP also generates getter and setter methods as part of persistence class.
Getter method is generated for all the fields of the database and setter methods are
generated for non-primary key fields of the table.
65 | P a g e Santosh P
To access the above three methods we require the object of base agent class. But the base
agent class is always created as abstract class and therefore cannot be instated.
Since the above three method are inherited to actor class we need to instantiate the actor
class and access these methods. But actor class is created as private class and therefore
cannot be instated.
We use the following mechanize to access these methods.
Actor class is created as singleton class.
As part of the actor class SAP as provided a public static attribute agent.
We need to access this public static attribute agent using the actor class name. When
accessed internal it execute the static constructor of actor class it is reasonable for
creating the object. This object is return back using which we access the above three
methods.
Singleton class: -
Creating a class in such way so that we can create exactly one object is called as
singleton.
Z915AM_OOPS47:
REPORT Z915AM_OOPS47.
method m1.
write :/ 'inside static method m1,about to return object'.
m = k.
endmethod.
method m2.
write :/ 'inside instance method m2'.
endmethod.
endclass.
start-of-selection.
data r type ref to abc.
66 | P a g e Santosh P
*r = abc=>m1( ).
call method abc=>m1
receiving
m = r.
OUTPUT:
Z915AM_OOPS48:
REPORT Z915AM_OOPS48.
START-OF-SELECTION.
ACTOR = ZCA_915DEPT=>AGENT.
IF R1 = 'X'.
TRY.
CALL METHOD ACTOR->CREATE_PERSISTENT
EXPORTING
I_DEPTNO = P_DEPTNO
I_DNAME = P_DNAME
I_LOC = P_LOC
RECEIVING
RESULT = PERS.
67 | P a g e Santosh P
COMMIT WORK.
ENDIF.
CATCH CX_OS_OBJECT_EXISTING .
WRITE :/ 'Exception raised while creating'.
ENDTRY.
ELSEIF R2 = 'X'.
TRY.
CALL METHOD ACTOR->DELETE_PERSISTENT
EXPORTING
I_DEPTNO = P_DEPTNO.
COMMIT WORK.
CATCH CX_OS_OBJECT_NOT_EXISTING .
WRITE :/ 'Object not found'.
ENDTRY.
ELSEIF R3 = 'X'.
TRY.
CLEAR PERS.
CALL METHOD ACTOR->GET_PERSISTENT
EXPORTING
I_DEPTNO = P_DEPTNO
RECEIVING
RESULT = PERS.
IF PERS IS NOT INITIAL.
CLEAR : P_DNAME,
P_LOC.
TRY.
CALL METHOD PERS->GET_DNAME
RECEIVING
RESULT = P_DNAME.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Exception in getter of dname'.
ENDTRY.
TRY.
CALL METHOD PERS->GET_LOC
RECEIVING
RESULT = P_LOC.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Exception in getter of loc'.
ENDTRY.
68 | P a g e Santosh P
OUTPUT:
Persistence service using GUID: - In this we need to consider database table which contains
GUID as the first field. The data element of this field can be GUID/OS-GUID.
The data type of this field raw data type.
Note: - when a persistence class is mapped with the database table containing GUID also the field
expects the GUID are added as attribute of class and also getter and setter method are generated for
the entire field except GUID.
Z915AM_OOPS49:
REPORT Z915AM_OOPS49.
START-OF-SELECTION.
ACTOR = YCA_915DEPT=>AGENT.
IF R1 = 'X'.
TRY.
CALL METHOD ACTOR->CREATE_PERSISTENT
EXPORTING
I_DEPTNO = P_DEPTNO
I_DNAME = P_DNAME
69 | P a g e Santosh P
I_LOC = P_LOC
RECEIVING
RESULT = PERS.
IF PERS IS NOT INITIAL.
COMMIT WORK.
ENDIF.
CATCH CX_OS_OBJECT_EXISTING .
WRITE :/ 'Exception raised'.
ENDTRY.
ELSEIF R2 = 'X'.
TRY.
CALL METHOD ACTOR->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = P_GUID
RECEIVING
RESULT = OB.
PERS ?= OB.
IF PERS IS NOT INITIAL.
TRY.
CALL METHOD ACTOR->IF_OS_FACTORY~DELETE_PERSISTENT
EXPORTING
I_OBJECT = PERS.
COMMIT WORK.
CATCH CX_OS_OBJECT_NOT_EXISTING .
WRITE :/ 'Object not found'.
ENDTRY.
ENDIF.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Object not found'.
CATCH CX_OS_CLASS_NOT_FOUND .
WRITE :/ 'class not found'.
ENDTRY.
ELSEIF R3 = 'X'.
TRY.
CALL METHOD ACTOR->IF_OS_CA_PERSISTENCY~GET_PERSISTENT_BY_OID
EXPORTING
I_OID = P_GUID
RECEIVING
RESULT = OB.
PERS ?= OB.
IF PERS IS NOT INITIAL.
CLEAR : P_DEPTNO,
P_DNAME,
P_LOC.
TRY.
CALL METHOD PERS->GET_DEPTNO
RECEIVING
70 | P a g e Santosh P
RESULT = P_DEPTNO.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Exception in getdeptno'.
ENDTRY.
TRY.
CALL METHOD PERS->GET_DNAME
RECEIVING
RESULT = P_DNAME.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Exception in getdname'.
ENDTRY.
TRY.
CALL METHOD PERS->GET_LOC
RECEIVING
RESULT = P_LOC.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Exception in getloc'.
ENDTRY.
WRITE :/ 'Department no :',P_DEPTNO,
/ 'Department name :',P_DNAME,
/ 'Department loc :',P_LOC.
ENDIF.
CATCH CX_OS_OBJECT_NOT_FOUND .
WRITE :/ 'Object not found'.
CATCH CX_OS_CLASS_NOT_FOUND .
WRITE :/ 'class not found'.
ENDTRY.
ENDIF.
OUTPUT:
71 | P a g e Santosh P
Z915_TRANS:
method M1.
data : trans_mng type ref to
if_os_transaction_manager,
trans type ref to
if_os_transaction.
72 | P a g e Santosh P
pers type ref to zcl_915dept.
actor = zca_915dept=>agent.
if actor is not initial.
TRY.
CALL METHOD ACTOR->CREATE_PERSISTENT
EXPORTING
I_DEPTNO = '77'
I_DNAME = 'ABAP'
I_LOC = 'Ameerpet'
RECEIVING
RESULT = pers.
CATCH CX_OS_OBJECT_EXISTING .
write :/ 'Exception in create persistent'.
ENDTRY.
endif.
endmethod.
Transaction service: - It is use for managing the object oriented transaction involving database
operations. As part of this we need to use the following class and interfaces.
1) CL_OS_SYSTEM CLASS
2) IF_OS_TRANSCATION_MANGAER INTERFACE
3) IT_OS_TRANSACTION INTERFACE
1) Start the object oriented transaction by calling the start method of the interface.
IT_OS_TRANSACTION
The interface IF_OS_TRANSACTION which cannot be done directly. So we need to access the
instance method Create_Transaction of the interface IF_OS_TRANSACTION_MANAGER to
access this method we required the object of transaction manger interface. To get these object of
transaction manger interface. To get this object we need to access the static method
GET_TRANSCATION_MANAGER of the class CL_OS_SYSTEM.
End the transaction by calling the end method of the interface IF_OS_TRASACTION.
When the transaction is successfully completed SAP issues commit work statement
internally for saving the transaction permanently.
If the transaction fails SAP raise the exception as part of this exception handling we need
to cancel the transaction by calling the undo method of the interface
IF_OS_TRANSCATION.
73 | P a g e Santosh P
Z915AM_OOPS50:
REPORT Z915AM_OOPS50.
OUTPUT:
IT IS CONSIDERING IT AS TWO DIFFERENT PROGRAMS THAT’S WHY IT IS NOT
EXECUTING.
ZTR10:(TRANSACTION CODE)
Since transaction service is implemented in global classes we need to attach a T-code for a
transaction class method so that everything will be executed as single process. If we access the
transaction class method form local program it executed the different process which are assuming
as two different translations.
74 | P a g e Santosh P
Casting: - It is the process of converting a variable from one data type to another data types they
are two types.
1) Wide casting
2) Narrow casting
Wide casting: - It is the process of converting an object from a less detailed view to more detailed
view.
Narrow casting: - It is a process of converting an object from a more details view to be less
detailed view.
Check the existent persistent object using the method gets persistent OID. If the persistent object is
available it returns the object of object class which needs to be type casted to the corresponding
persistent class object. Pass the persistent class object as an input to the method.
‘DELETE_PERSISTENT.’
Event handling in object oriented: - As part of ABAP objects SAP as provided many events as
part of standard classes. These events are used in ALV reporting work flow customization CRM
technical DSP and webdynpro programming.
As part of custom classes we can declare user defined events. These event are declared are raised
and handle by the developer itself.
Step1 Syntax: -
Step2 Syntax: -
Methods/class-methods <method name> for event <event name> of <class name> [importing
parameter list].
Step3 Syntax: -
75 | P a g e Santosh P
Step4 Syntax: -
1) Instant
2) Static
Note: - if the handler is not register events can be triggered but no actions can be performed
because the event handler methods will not be executed.
Z915AM_OOPS52:
REPORT Z915AM_OOPS52.
START-OF-SELECTION.
DATA OB TYPE REF TO ABC.
76 | P a g e Santosh P
CREATE OBJECT OB.
OUTPUT:
Z915AM_OOPS53:
REPORT Z915AM_OOPS53.
method m2.
write :/ 'Inside m2,About to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
77 | P a g e Santosh P
OUTPUT:
Z915AM_OOPS54:
REPORT Z915AM_OOPS54.
method m2.
write :/ 'Inside m2,About to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
set handler ob->m1 for ob.
call method ob->m2.
OUTPUT:
78 | P a g e Santosh P
Z915AM_OOPS55:
REPORT Z915AM_OOPS55.
method m1.
write :/ 'inside first event handler method m1'.
endmethod.
method m3.
write :/ 'inside m3,about to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
OUTPUT:
79 | P a g e Santosh P
Z915AM_OOPS56:
REPORT Z915AM_OOPS56.
method m2.
write :/ 'inside second event handler method m2'.
endmethod.
method m3.
write :/ 'inside m3,about to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
80 | P a g e Santosh P
Z915AM_OOPS57:
REPORT Z915AM_OOPS57.
method m2.
write :/ 'inside second event handler method m2'.
endmethod.
method m3.
write :/ 'inside m3,about to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
OUTPUT:
81 | P a g e Santosh P
Z915AM_OOPS58:
REPORT Z915AM_OOPS58.
method m2.
write :/ 'inside m2, about to raise event'.
raise event e1.
endmethod.
endclass.
class pqr definition.
public section.
methods m3 for event e1 of abc.
endclass.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
82 | P a g e Santosh P
OUTPUT:
For all instance: - while register the handler for register events as part of set handler statement we
need to specify the object name after for keyword. This is reasonable for raising the event. This as
to be done for every object separately which is raising the event instead of this we can use ‘FOR
ALL INSTANCE’ keyword.
As part of handler such that the event handler method will executed irrespective of object
used for raising the event.
Z915AM_OOPS59:
REPORT Z915AM_OOPS59.
method m2.
write :/ 'inside m2, about to raise event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
data ob2 type ref to abc.
create object ob2.
*set handler ob1->m1 for ob1.
*set handler ob1->m1 for ob2.
83 | P a g e Santosh P
set handler ob1->m1 for all instances.
OUTPUT:
Static event: - While registering the handlers for static even we should not specify the object which
is reasonable raising the static event. Because static event is not specific to on object i.e. for
keyword is not allowed for the static event part of set handler this similarly for all instances in case
of instance event.
Z915AM_OOPS60:
REPORT Z915AM_OOPS60.
method m2.
write :/ 'inside m2 about to raise static event'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob1 type ref to abc.
create object ob1.
84 | P a g e Santosh P
create object ob2.
Z915AM_OOPS61:
REPORT Z915AM_OOPS61.
method m2.
write :/ 'inside m2'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
Z915AM_OOPS62:
REPORT Z915AM_OOPS62.
method m2.
write :/ 'inside m2'.
raise event e1.
endmethod.
endclass.
start-of-selection.
data ob type ref to abc.
create object ob.
86 | P a g e Santosh P
OUTPUT:
Z915AM_OOPS63:
REPORT Z915AM_OOPS63.
METHOD M2.
WRITE :/ 'inside m2,about to raise event'.
RAISE EVENT E1
EXPORTING
X = Y.
ENDMETHOD. "m2
ENDCLASS. "abc IMPLEMENTATION
START-OF-SELECTION.
DATA OB TYPE REF TO ABC.
CREATE OBJECT OB.
PARAMETERS R TYPE I.
Z915AM_CLASS11:
method M1.
WRITE :/ 'INSIDE EVENT HANDLER'.
WRITE :/ 'PARAMETER RECEIVED IS ',P1.
endmethod.
method M2.
WRITE :/ 'INSIDE M2'.
RAISE EVENT E1
EXPORTING
P1 = 10.
endmethod.
88 | P a g e Santosh P
Z915AM_OOPS64:
REPORT Z915AM_OOPS64.
OUTPUT:
89 | P a g e Santosh P
Z915AM_RESTAURANT:
method SET.
TABLENO = 10.
STEWARD = 'ABC'.
endmethod.
method DISPLAY.
WRITE :/ TABLENO,STEWARD.
endmethod.
Z915AMCYCLE:
90 | P a g e Santosh P
method SETCYCLE.
wheels = 2.
brakes = 2.
colour = 'blue'.
endmethod.
method DISPLAY.
write :/ wheels,brakes,colour.
endmethod.
Z915AMSCOOTER:
91 | P a g e Santosh P
method SETSCOOTER.
wheels = 2.
brakes = 4.
colour = 'red'.
endmethod.
Z915AMCAR:
92 | P a g e Santosh P
method SETCAR.
wheels = 4.
brakes = 5.
colour = 'cyan'.
endmethod.
93 | P a g e Santosh P