Topicwise Examples
Topicwise Examples
Majumdar
SUBHENDU MAJUMDAR
Technical Consultant, IBM
Page : 1 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
PREFACE
Care has been taken to use simple examples which spawns not more than one page.
Discussion on an example is categorised under four major heads:-
The best way to learn anything is learning by examples. The entire content has been
designed and documented in such a way that the reader can easily grasp the matter
and implement it in his course of learning.
The author will remain grateful to the responsible readers if they can point out
mistakes in the documentation and suggest further improvements on this effort.
- Subhendu
Majumdar
Page : 2 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
INDEX
1 Class..................................................................................................................... 5
1.1 Accessibility of different sections of a class......................................................5
1.2 Subclass cannot access the private component of superclass......................8
1.3 External users cannot access protected/private components of a class.......9
1.4 Local Class can understand data and types in the global area of the
program................................................................................................................. 10
1.5 Class can be instantiated within implementation of another class..............12
1.6 Deferred Definition of a Class.....................................................................13
1.7 Place to put non-declarative statements.....................................................14
1.8 Use of Field Symbols in Class.....................................................................15
1.9 Use of Static Attributes...............................................................................16
1.10 Creation of Global class and using it in a local program..............................17
2 Methods.............................................................................................................. 22
2.1 Method with one import parameter/ only one non-optional parameter.......22
2.2 Import parameters passed by ref. can’t be changed inside the method .. . .23
2.3 Use of PREFERRED PARAMETER in a method..............................................24
2.4 Use of EXPORT and CHANGING parameters of a method............................25
2.5 Method using Internal Table as one of the parameters...............................26
2.6 Use of RETURNING parameters in method..................................................27
2.7 Demo on Static Method...............................................................................28
2.8 Static methods can only use static attributes, instance methods use both.29
2.9 Method Raising Exceptions.........................................................................30
2.10 Method can call itself..................................................................................31
2.11 Use of ME in methods.................................................................................32
2.12 Pointer Tables.............................................................................................33
2.13 Dynamic Method Calls................................................................................34
2.14 Use of parameter table...............................................................................35
2.15 Use of Exception Table................................................................................36
3 Constructors....................................................................................................... 37
3.1 Instance Constructors get fired at the time of class instantiation...............37
3.2 Instance Constructors can have import parameters...................................38
3.3 Constructors cannot have any export parameters......................................39
3.4 Instance Constructors can raise exceptions................................................40
3.5 Use of static constructor.............................................................................41
3.6 Static constructor can be triggered at the beginning of a processing
block(form /event/block/procedure).......................................................................42
3.7 Static/Class constructors cannot have any interface...................................43
4 Inheritance......................................................................................................... 44
4.1 Subclass can access public/protected components of superclass...............44
4.2 Subclass can re-implement inherited methods from superclass.................46
4.3 Objects cannot be created from an abstract class......................................47
4.4 Abstract methods cannot be implemented in abstract class.......................48
4.5 Final classes cannot have any subclass......................................................49
4.6 Final methods cannot be redefined in the subclasses.................................50
4.7 Static attributes exist only once per inheritance tree.................................51
4.8 Constructors of superclass flows down the chain........................................52
4.9 Subclass can have enhanced constructor than its superclass...................53
4.10 Static constructor of a class is called only once per program.....................55
4.11 Static type and Dynamic type of a variable................................................56
4.12 Static type should be more general than dynamic type of a reference
variable.................................................................................................................. 58
4.13 Method of a parent class, used from its subclass, uses attributes of the
parent class only, if the method is not re-defined in subclass..............................59
4.14 Demo on Widening Cast..............................................................................60
5 Interface............................................................................................................. 61
5.1 Simple use of an interface..........................................................................61
Page : 3 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
5.2 Interfaces can only be implemented in the public section of a class...........62
5.3 A class with an interface should implement all the methods of that interface
63
5.4 Values for interface attributes are assigned at the time of inclusion in a
class 64
5.5 Use of FINAL methods from Interface..........................................................65
5.6 Use of Abstract methods from Interface.....................................................66
5.7 Use of Interface Reference Variable............................................................67
5.8 Use of Nested Interface...............................................................................69
5.9 Using ALIASES............................................................................................. 70
5.10 Polymorphism via Interfaces.......................................................................71
6 Friendship........................................................................................................... 72
6.1 Friendship between Classes........................................................................72
6.2 Subclasses of friends can also become friends...........................................73
6.3 Friendship is one sided...............................................................................74
7 Events................................................................................................................ 75
7.1 Events with Handler Method in the same class...........................................75
7.2 Event with event handler method in different class....................................76
7.3 More than one event handler method can exist for same event.................77
7.4 Use of static event......................................................................................79
7.5 Events with export parameters...................................................................80
8 Class-Based Exceptions......................................................................................81
8.1 Using SAP provided exception class............................................................81
8.2 When both superclass and subclass are used to track error.......................82
8.3 Propagation of Class-Based exceptions in procedures to the caller............83
8.4 Program can raise exceptions based on SAP standard exception-classes. .84
8.5 Objects are created from exception classes when error is trapped.............85
8.6 Demo on Locally Defined Exception-Class..................................................86
8.7 Nested TRY…ENDTRY block........................................................................87
8.8 Use of CLEANUP section..............................................................................88
9 BADIs ( Business Add-Ins)...................................................................................89
9.1 Single Implementation of BADI...................................................................89
9.2 Multiple Implementation.............................................................................93
9.3 Searching for BADI in SAP Transaction and Implementing it.......................96
9.4 Menu Enhancements...................................................................................98
Page : 4 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
1 Class
1.1 Accessibility of different sections of a class
Theme From this program, one will learn:-
Page : 5 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS parentclass DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
METHODS : SHOWVAL.
PROTECTED SECTION.
DATA : protectdata(40) type c value 'Protected data'.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
Page : 6 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
DATA : parent type ref to parentclass ,
child type ref to childclass .
create object : parent ,
child .
call method : parent->showval ,
child->subval.
skip 2.
parent->commondata = ‘User changing public data’.
write:/5 parent->commondata.
Output
All data from parentclass shown:-
Accessible to all
Protected data
Private data
Accessible to all
Protected data
Page : 7 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Page : 8 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 DEFINITION .
PUBLIC SECTION.
DATA : commondata(30) type c value 'Accessible to all'.
PROTECTED SECTION.
DATA : protectdata(40) type c value 'Protected data'.
private section.
data : privatedata(30) type c value 'Private data'.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to c1.
create object : obj1.
write:/5 obj1->protectdata ,
obj1->privatedata.
Page : 9 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
1.4 Local Class can understand data and types in the global area of the
program.
Theme This program will demonstrate the following:-
CLASS c1 DEFINITION .
public section.
methods : meth1 .
DATA : l_num like num1 ,
it_tab type standard table of typ_tab ,
w_tab like line of it_tab.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
method : meth1 .
data : l_cnum(2) type c.
l_num = 0.
do 5 times.
l_num = l_num + 1.
l_cnum = l_num.
concatenate 'Student-'
l_cnum
into w_tab-name.
w_tab-age = num1 * l_num .
append w_tab to it_tab.
clear w_tab.
enddo.
loop at it_tab into w_tab.
write:/5 w_tab-name ,
w_tab-age.
endloop.
endmethod.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to c1.
Page : 10 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Output Student-1 5
Student-2 10
Student-3 15
Student-4 20
Student-5 25
Page : 11 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
start-of-selection.
data : my_obj type ref to class2.
create object : my_obj.
call method my_obj->method2.
Output 2
Page : 12 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2.
ENDCLASS.
CLASS C2 DEFINITION.
public section.
data : num type i value 5.
ENDCLASS.
start-of-selection.
data : obj1 type ref to C1.
CREATE OBJECT obj1.
create object obj1->o2.
write:/5 obj1->o2->num .
Output 5
Page : 13 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
data : obj1 type ref to c1 . data : obj1 type ref to c1 .
create object obj1. create object obj1.
call method obj1->m1. call method obj1->m1.
Page : 14 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class c1 definition .
public section .
* Instance attribute : inum declared below
data : inum type i value 5 .
* static attribute onum declared below
class-data : onum type i value 10 .
endclass.
class c1 implementation.
endclass.
start-of-selection.
data : oref1 type ref to c1 .
create object oref1.
* Assigning instance attribute to field symbol <fs>
assign oref1->inum to <fs> .
write:/5 <fs> .
* Assigning static attribute to field symbol
assign oref1->onum to <fs> .
write:/5 <fs> .
assign c1=>onum to <fs> .
write:/5 <fs> .
Output 5
10
10
Page : 15 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
That the value of the static attribute gets incremented each time
when the method M1 of different objects is called shows that this
variable is able to retain its value through the entire runtime.
Dump report ysubdel.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I .
METHODS : M1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1 .
num = num + 1.
write:/5 num .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
c1=>num = 3.
write:/5 c1=>num .
Output 3
4
5
Page : 16 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Go to transaction SE24.
Enter the name of the global class you want to create, with ‘Y’ or ‘Z’ at the
beginning.
Press Create pushbutton.
A dialog window shown above will appear. Check the radiobutton : Class.
Press Enter.
Another dialog window shown above will appear. Enter the description for the
class.
Select from the Instantiation listbox whether you want to create the class as
PUBLIC/PROTECTED/PRIVATE/ABSTRACT.
Check the radiobutton for Usual ABAP Class.
Check the checkbox for Final.
Press Save pushbutton.
Page : 17 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Page : 18 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Click the pushbutton for Parameters to navigate to the screen to enter parameters
for the method.
There will be one importing parameter : L_MTART and one exporting internal table :
MATERIAL_LIST. Create entries for them as shown above.
Click the pushbutton : Exceptions to make entry for Exceptions to be raised by the
method.
Page : 19 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Click on the pushbutton : Code( blue colored button) to implement the method.
An ABAP Editor will open up. Write the logic for code implementation.
Then, check and activate the code.
Page : 20 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
* Create object from the global class
DATA : oref TYPE REF TO zget_materials.
CREATE OBJECT oref.
* Call the method to get list of material code and name
CALL METHOD oref->list_materials
EXPORTING l_mtart = p_mtart
IMPORTING material_list = it_mat
EXCEPTIONS
material_not_found = 2.
if sy-subrc ne 0.
write:/5 'Material not found'.
else.
* Display the list
loop at it_mat into x_mat.
write:/5 x_mat-matnr ,
x_mat-maktg.
endloop.
endif.
Output Compile and run the program. There will be a parameter for material type
in the selection screen. Enter a valid value and get the list of material
codes and descriptions.
Page : 21 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
2 Methods
2.1 Method with one import parameter/ only one non-optional parameter
Theme This program will demonstrate different ways of calling a method
which has only one import parameter.
This strategy is also valid for cases where a method has more than
one import parameters, but only one of them being non-optional.
Program This program has a class , C1 with a method : meth1. This method
has only one import parameter(input1). Look at the method
implementation for details. The main purpose of this program is to
demonstrate the different ways of calling a method with single
import parameter.
Dump REPORT YSUBDEL .
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA : NUM TYPE I VALUE 5.
METHODS : METH1 IMPORTING INPUT1 TYPE I .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD : METH1.
num = NUM * INPUT1 .
WRITE:/5 NUM .
num = 5.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CREATE OBJECT : OREF1.
* Different ways of calling the method with one import parameter
CALL METHOD OREF1->METH1 EXPORTING INPUT1 = 4.
CALL METHOD OREF1->METH1( INPUT1 = 5 ).
CALL METHOD OREF1->METH1( 6 ).
Output 20
25
30
Page : 22 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 IMPLEMENTATION.
METHOD : METH1.
Input1 = 4.
write:/5 input1.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CREATE OBJECT : OREF1.
num = 3.
CALL METHOD OREF1->METH1 EXPORTING INPUT1 = 4
input2 = num.
Page : 23 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Notice the last line of the program and see the output. The output
will establish that the preferred parameter INPUT2 gets the
value passed to the method when it is called using the
syntax:-
CALL METHOD objref->meth(<val>).
CLASS C1 IMPLEMENTATION.
METHOD : METH1.
write:/5 input1 ,
/5 input2 .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CREATE OBJECT : OREF1.
CALL METHOD : OREF1->METH1( input1 = 5 input2 = 3 ).
skip 2.
write:/5 'Next call'.
call method oref1->meth1( 10 ) .
Output 5
3
Next call
0
10
Page : 24 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
2.4 Use of EXPORT and CHANGING parameters of a method
Theme This program will demonstrate the use of EXPORTING and CHANGING
parameters of a method.
Program The program contains a method TAX_CALC belonging to the class
description CTAX. It receives GRADE as IMPORTING parameter and SALARY as
CHANGING parameter. Based on the grade, the EXPORTING
parameter ITAX is calculated and the CHANGING parameter ,
SALARY is modified by deducting tax from it.
REPORT YSUBDEL .
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO CTAX.
CREATE OBJECT : OREF1.
w_salary = 30000.
w_tax = 0 .
write:/5 'Before method call, salary and tax are' ,
w_salary ,
w_tax .
CALL METHOD OREF1->TAX_CALC EXPORTING grade = 'A01'
IMPORTING itax = w_tax
CHANGING salary = w_salary.
write:/5 'After method call, salary and tax are' ,
w_salary ,
w_tax .
Output Before method call, salary and tax are 30,000.00 0.00
After method call, salary and tax are 24,000.00 6,000.00
Page : 25 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
DATA : w_mat TYPE REF TO get_materials.
CREATE OBJECT : w_mat.
CALL METHOD w_mat->getmara EXPORTING matgr = p_matkl
IMPORTING l_tab = itab .
LOOP AT ITAB INTO X_TAB.
WRITE:/5 X_TAB-MATNR , X_TAB-MEINS.
ENDLOOP.
Output One/more than one records with material number and basic unit,
depending on the material group entered in the selection-screen.
Page : 26 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class c1 definition .
public section.
methods : m1 importing input1 type i
input2 type i
returning value(result) type i .
endclass.
class c1 implementation.
method : m1.
result = input1 * 2 + input2.
endmethod.
endclass.
start-of-selection.
data : obj1 type ref to c1 .
create object obj1.
* Syntax 1
call method obj1->m1 EXPORTING input1 = 5
input2 = 4
RECEIVING result = w_num.
write:/5 w_num .
* Syntax 2
w_num = obj1->m1( input1 = 10 input2 = 20 ).
write:/5 w_num .
* Syntax 3
move obj1->m1( input1 = 2 input2 = 3 ) to w_num .
write:/5 w_num .
Output 14
40
7
Page : 27 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
start-of-selection.
call method testclass=>testmethod.
Output 5
Page : 28 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
2.8 Static methods can only use static attributes, instance methods use
both
Theme Static methods of a class can only use static attributes of that class. It
cannot use instance attributes. But, instance methods can use both.
Program The following program contains a class C1 which contains the
Description following:-
Component Type Static/Instance
stnum Data static
Instnum Data Instance
Stmeth Method Static
Instmeth Method Instance
Both the static and instance methods are attempting to display values
of the static and instance attributes: STNUM and INSTNUM .
CLASS C1 DEFINITION.
PUBLIC SECTION.
CLASS-DATA : STNUM TYPE I VALUE 5.
DATA : INSTNUM TYPE I VALUE 6 .
CLASS-METHODS : STMETH .
METHODS : INSTMETH .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD : STMETH .
WRITE:/5 STNUM .
WRITE:/5 INSTNUM .
ENDMETHOD.
METHOD INSTMETH.
WRITE:/5 STNUM .
WRITE:/5 INSTNUM .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CALL METHOD c1=>stmeth .
CREATE OBJECT OREF1.
CALL METHOD oref1->instmeth.
Remove the line in bold in the program and compile. It will get
successfully compiled and executed.
Page : 29 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
The class C1 in this program contains method M1 which imports value for
NUM1 , and returns five times of it through the export parameter,
NUM2.
However, if the value passed to NUM1 is lesser than 5, it raises an
exception E1 with some error message.
The method M1 is called after creating an object from the class. User-
entered value in the parameter field : P_NO is passed to importing
parameter NUM1.
Dump report ysubdel1 message-id 00.
class c1 definition .
public section.
methods : m1 importing num1 type i
exporting num2 type i
exceptions e1.
endclass.
class c1 implementation.
method : m1.
if num1 lt 5 .
message i398(00) with 'Should be >=5' raising e1.
else .
num2 = num1 * 5 .
endif.
endmethod.
endclass.
start-of-selection.
data : obj1 type ref to c1 .
create object obj1.
call method obj1->m1 exporting num1 = p_no
importing num2 = p_no
exceptions e1 = 1.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
write:/5 p_no .
ENDIF.
Output The program provides the user a selection-screen where the user enters
a numeric value. If the user entry is <5, he gets an information message
‘Should be >=5’. Else, five times of the value entered is displayed by the
program on execution.
Page : 30 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class c1 definition .
public section.
class-data : statnum type i .
methods : m1 .
endclass.
class c1 implementation.
method : m1.
statnum = statnum + 10.
if statnum gt 100.
exit.
endif.
write:/5 statnum .
call method m1.
endmethod.
endclass.
start-of-selection.
data : obj1 type ref to c1 .
create object obj1.
call method obj1->m1 .
Output 10
20
….
100
Page : 31 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
To access the variable I_NUM at the class level within the method,
the selector ME is used. Please see the ouputs of this program for
better understanding.
Dump REPORT YSUBOOPS17 .
endclass.
start-of-selection.
data : i_num type i.
data : my_obj type ref to testclass.
create object : my_obj.
call method my_obj->testmethod.
Output 5
2
Page : 32 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
start-of-selection.
data : myobj type ref to testclass ,
myobj_tab type table of ref to testclass.
do 5 times.
create object myobj .
append myobj to myobj_tab.
enddo.
Output 5
10
15
20
25
Page : 33 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class c1 definition.
public section.
class-methods : statm .
methods : instm .
endclass.
class c1 implementation.
method : statm .
write:/5 'I am static method'.
endmethod.
method : instm.
write:/5 'I am instant method'.
endmethod.
endclass.
start-of-selection.
data : oref type ref to c1.
create object oref.
* Name of instance method can be dynamic
f = 'INSTM'. call method oref->(f).
* Name of static method can be dynamic
f = 'STATM'. call method oref->(f).
* Name of the class can be dynamic for static method call
f = 'C1'. call method (f)=>statm.
* Name of the method can be dynamic for static method call
f = 'STATM'. call method c1=>(f).
* Both can be dynamic for static method call
g = 'C1'. call method (g)=>(f).
Page : 34 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
DEFINE : poptable.
ptab_line-name = &1.
ptab_line-kind = CL_ABAP_OBJECTDESCR=>&2.
GET REFERENCE OF &3 INTO ptab_line-value.
INSERT ptab_line INTO TABLE ptab.
IF sy-subrc ne 0.
EXIT.
ENDIF.
END-OF-DEFINITION.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 IMPORTING p1 TYPE i
exporting p3 type i .
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
p3 = p1 + 200.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
Output 205
Page : 35 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 EXCEPTIONS exc.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
RAISE exc.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
etab_line-name = 'EXC'.
etab_line-value = 4.
INSERT etab_line INTO TABLE etab.
IF sy-subrc ne 0.
EXIT.
ENDIF.
Output 4
3 Constructors
3.1 Instance Constructors get fired at the time of class instantiation
Theme This simple program will show you that instance constructor
methods of a class get triggered when an object is created from the
class.
Program This program contains a class C1 with a constructor method which
Description writes out something to indicate that it is triggered. In the START-
Page : 36 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
OF-SELECTION block, the class C1 is instantiated, which triggers the
instance constructor method( as is evident by the output as report).
This establishes the theme.
Dump REPORT YSUBOOPS1.
CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
WRITE:/5 'I am constructor'.
skip 2.
ENDMETHOD.
ENDCLASS.
*************** main program **************
START-OF-SELECTION.
DATA: obj1 TYPE REF TO c1.
CREATE OBJECT: obj1.
Output I am constructor
Page : 37 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Theme Instance constructors can have import parameters. Values to them are
passed at the time of CREATE OBJECT statement to create object from
the class containing the constructor.
Program The program contains a class C1 which has one instance constructor
Description with one import parameter. The constructor gets fired at the time of
CREATE OBJECT statement.
Dump REPORT YSUBOOPS2.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR importing today type d.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
Write:/5 'Today is : ' , today dd/mm/yyyy.
ENDMETHOD.
ENDCLASS.
*************** main program **************
START-OF-SELECTION.
DATA: obj1 TYPE REF TO c1.
CREATE OBJECT: obj1 exporting today = sy-datum.
Page : 38 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR exporting name type c.
ENDCLASS.
Page : 39 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
REPORT YSUBOOPS2.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR importing num type i
exceptions e1 .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
if num lt 7.
raise e1.
endif.
ENDMETHOD.
ENDCLASS.
*************** main program **************
START-OF-SELECTION.
DATA: obj1 TYPE REF TO c1.
CREATE OBJECT: obj1 exporting num = 5
exceptions e1 = 2.
if sy-subrc = 2.
write:/5 'Exceptions raised'.
endif.
Page : 40 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
These two programs will show you that a class constructor gets fired before
any of its static components are accessed, or an object is created from the
class.
Dump REPORT YSUBOOPS2. REPORT YSUBOOPS2.
CLASS c1 DEFINITION . CLASS c1 DEFINITION .
PUBLIC SECTION. PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5. CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS :CLASS_CONSTRUCTOR. CLASS-METHODS :CLASS_CONSTRUCTOR.
ENDCLASS. ENDCLASS.
START-OF-SELECTION. START-OF-SELECTION.
WRITE:/5 C1=>NUM. DATA : OREF TYPE REF TO C1.
CREATE OBJECT OREF.
Page : 41 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.
WRITE:/5 'I am class constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
write:/5 'Hello'.
write:/5 c1=>num.
Page : 42 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Page : 43 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
4 Inheritance
4.1 Subclass can access public/protected components of superclass
Theme This program will demonstrate:-
How to create a subclass from a superclass.
Subclass can access public/protected components( methods,
attributes etc) of superclass.
Prog. This program contains superclass C1 and its subclass C2. Class C1 has the
Descr. following components:-
Component Nature Section of Significance/action
Existence
NUM Attribute public Value = 6
METH1 Method Public Displays value of NUM
METH2 Method Protected Displays:- “I am meth2”
NUM2 Attribute Protected Value = 7
CLASS C1 IMPLEMENTATION .
METHOD : METH1.
WRITE:/5 num.
endmethod.
METHOD : METH2.
WRITE:/5 ' I am meth2 '.
ENDMETHOD.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
METHOD M1.
CALL METHOD : meth1, meth2.
write:/5 num2.
endmethod.
endclass.
START-OF-SELECTION.
DATA : OREF TYPE REF TO C2.
CREATE OBJECT OREF.
CALL METHOD : OREF->M1.
Output 6
I am meth2
Page : 44 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
7
Page : 45 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 IMPLEMENTATION .
METHOD : METH1.
WRITE:/5 'I am meth1 in class C1'.
CALL METHOD METH2.
ENDMETHOD.
METHOD : METH2.
WRITE:/5 ' I am meth2 in class C1 '.
ENDMETHOD.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
METHOD METH1.
WRITE:/5 'I am meth1 in class C2'.
call method meth2.
endmethod.
METHOD : METH2.
WRITE:/5 ' I am meth2 in class C2 '.
ENDMETHOD.
endclass.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2.
CREATE OBJECT : OREF1 , OREF2.
CALL METHOD : OREF1->METH1 ,
OREF2->METH1.
Page : 46 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
4.3 Objects cannot be created from an abstract class.
Theme Objects cannot be created from an abstract class. Only the subclasses of
such class can be instantiated.
Program This program contains an abstract class C1 and its subclass C2. Object
Descripti cannot be created from class C1, but possible from class C2.
on
Dump REPORT YSUBDEL. REPORT YSUBDEL.
START-OF-SELECTION. START-OF-SELECTION.
data : OREF1 TYPE REF TO
Instantiation of abstract class C1 , data : OREF1ofTYPE
Instantiation REFofTO
subclass anC1 ,
abstract class
OREF2 TYPE REF TO C2. OREF2 TYPE REF TO C2.
Output CREATE OBJECT
Instantiation oref1. class will be resisted
of abstract CREATE OBJECT oref2.
with error message at the
time of compilation.
Instantiation of subclass of an abstract class will be allowed.
Page : 47 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Progra This program contains an abstract class C1 with abstract method METH1,
m which is implemented in the same class.The program will not be compiled
Descr. due to this.
The program is then modified and the abstract method is implemented in
class C2, subclass of C1. Now, the program gets successfully compiled.
Dump REPORT YSUBDEL. REPORT YSUBDEL.
Page : 48 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 IMPLEMENTATION .
ENDCLASS.
CLASS C2 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
data : OREF2 TYPE REF TO C2.
CREATE OBJECT oref2.
Page : 49 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 DEFINITION .
PUBLIC SECTION.
METHODS : METH1 FINAL.
ENDCLASS.
CLASS C1 IMPLEMENTATION .
method meth1.
write:/5 'I am method meth1'.
endmethod.
ENDCLASS.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
method : meth1.
write:/5 ' I am meth1,modified in class C2'.
Endmethod.
endclass.
START-OF-SELECTION.
data : OREF2 TYPE REF TO C2.
CREATE OBJECT oref2.
Page : 50 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 DEFINITION .
PUBLIC SECTION.
class-data : num type i.
ENDCLASS.
CLASS C1 IMPLEMENTATION .
ENDCLASS.
CLASS C2 IMPLEMENTATION.
endclass.
START-OF-SELECTION.
C3=>NUM = 10.
WRITE:/5 C2=>NUM.
Output 10
Page : 51 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD constructor.
WRITE:/5 'I am C1'.
skip.
ENDMETHOD.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
ENDCLASS.
START-OF-SELECTION.
DATA: obj type ref to C2.
CREATE OBJECT: obj.
Output I am C1.
Page : 52 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
DATA: myson type ref to son.
CREATE OBJECT: myson.
Page : 53 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Output I am grandfather
I am father
I am son
Page : 54 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
An object is created finally from the class SON, which triggers the
constructor methods in the order: FATHERSON.
Now, an object is created from the class FATHER. But, that did not trigger
constructor of class FATHER, because that had already been triggered by
the program when an object was created from the class SON.
This establishes the theme.
Dump REPORT YSUBOOPS18.
START-OF-SELECTION.
DATA: myson type ref to son.
CREATE OBJECT: myson.
data : myfather type ref to father.
create object : myfather.
Output I am father
I am son
Page : 55 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
4.11 Static type and Dynamic type of a variable
Theme Static type of a reference variable can point to a superclass; whereas its
dynamic type can point to one of its subclasses.
This program will show you various ways to do that.
Progra This program contains class C1 with method M1. Class C2 is a subclass of
m C1 and contains redefined implementation of method M1.
Descrip Reference variables are created in the program as follows:-
. Reference Static Dynamic Dynamic type assigned by
Variable type type
OREF1 C1 C1 CREATE object oref1.
OREF11 C1 C2 CREATE OBJECT oref11 TYPE C2.
OREF111 C1 C2 CREATE OBJECT OREF111.
OREF111 = OREF2.
OREF2 C2 C2 CREATE OBJECT oref2.
Finally, method M1 ic called using all objects.The observations are as
follows:-
class c1 definition.
public section.
methods : m1.
endclass.
class c1 implementation.
method m1 .
write:/5 ' I am m1 of c1'.
endmethod.
endclass.
class c2 implementation.
method m1.
write:/5 'I am m1 of c2'.
endmethod.
endclass.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1,
OREF11 TYPE REF TO C1,
OREF111 TYPE REF TO C1,
OREF2 TYPE REF TO C2 .
CREATE OBJECT : OREF1 ,
OREF11 TYPE C2,
OREF111 ,
OREF2 .
OREF111 = OREF2.
CALL METHOD : OREF1->M1 , “ Output : I am m1 of c1
OREF11->M1 , “ Output : I am m1 of c2
OREF111->M1, “ Output : I am m1 of c2
OREF2->M1 . “ Output : I am m1 of c2
Page : 56 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Output I am m1 of c1
I am m1 of c2
I am m1 of c2
I am m1 of c2
Page : 57 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
4.12 Static type should be more general than dynamic type of a reference
variable
Theme Static type of a reference variable can refer to a superclass, whereas its
dynamic type can refer to a subclass of the superclass. In that case, the
reference variable will identify all the common components of the
superclass and subclass. It will not be able to identify any new components
in the subclass, which are not present in its superclass.
Progra This program contains class C1 and its subclass C2.
m Class C1 contains method M1, which is also redefined in class C2.
Descr. Class C2 contains a new method M2.
A reference variable OREF11 is created with static type of C1 and dynamic
type of C2.
Method M2 is attempted to be called using OREF11.
This produces compilation error, establishing the theme.
class c1 definition.
public section.
methods : m1.
endclass.
class c1 implementation.
method m1 .
write:/5 ' I am m1 of c1'.
endmethod.
endclass.
class c2 implementation.
method m1.
write:/5 'I am m1 of c2'.
endmethod.
method m2.
write:/5 'I am m2'.
endmethod.
endclass.
START-OF-SELECTION.
DATA : OREF11 TYPE REF TO C1.
CREATE OBJECT : OREF11 TYPE C2.
Output Compilation error fails to identify method M2 in the last statement of the
program.
Page : 58 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
4.13 Method of a parent class, used from its subclass, uses attributes of
the parent class only, if the method is not re-defined in subclass.
Theme As long as a method( using private attributes) inherited from a superclass
is not redefined, it still uses the private attributes of the superclass, not
those of the subclass, even if the subclass has private attributes of the
same name.
Progra Class C1 contains a method M1 in the public section and a private variable,
m NUM of value = 5. Method M1 in class C1 displays the value of private
Descrip variable, NUM.
. Class C2 is a subclass of class C1. It does not redefine method M1. But, it
has also a private variable , NUM with value = 6.
An object is created from class C2 and the method M1 is called.
The output shows that the variable NUM( as displayed by method M1) has
been taken from class C1, not C2.
This establishes the theme.
Dump report ysubdel .
CLASS c1 DEFINITION.
PUBLIC SECTION .
METHODS : m1 .
PRIVATE SECTION.
DATA : num TYPE I VALUE 5 .
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD : m1 .
write:/5 num .
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
ENDCLASS.
START-OF-SELECTION.
DATA : oref2 TYPE REF TO c2 .
Output 5
Page : 59 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class c1 definition.
public section.
data : num type i value 5.
endclass.
class c1 implementation.
endclass.
class c2 implementation.
endclass.
start-of-selection .
data : obj1 type ref to c1 ,
obj2 type ref to c2 .
create object : obj1 ,
obj2 .
TRY.
obj2 ?= obj1.
CATCH cx_sy_move_cast_error.
write:/5 'cx_sy_move_cast_error trapped'.
ENDTRY.
Output cx_sy_move_cast_error trapped
Page : 60 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
5 Interface
5.1 Simple use of an interface
Theme This program will show simple use of an interface with its own data and
methods and how it is implemented in a class. It will also show that there
can be methods of same name for an interface and the class implementing
the interface.
Progra This program contains an interface I1 with attribute : NUM an method :
m Desc METH1.
This interface is implemented in a class : C1 which also has its own method
METH1.
An object OREF is created from class C1 and both the methods METH1 ,
one for class and another for interface is called using the object.
Dump
report ysubdel .
interface i1.
data : num type i .
methods : meth1.
endinterface.
class c1 definition.
public section.
methods : meth1. “ class C1’s own method
interfaces : i1.
endclass.
class c1 implementation.
method : meth1.
write:/5 'I am meth1 in c1'.
endmethod.
method i1~meth1.
write:/5 'I am meth1 from i1'.
endmethod.
endclass.
start-of-selection.
data : oref type ref to c1.
create object oref.
write:/5 oref->i1~num.
call method oref->meth1.
call method oref->i1~meth1.
Output 0
I am meth1 in c1
I am meth1 from i1
Page : 61 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
interface i1.
methods : meth1.
endinterface.
class c1 definition.
protected section.
interfaces : i1.
endclass.
Output Compilation error with error message :-
INTERFACES may only be implemented in the public section.
Page : 62 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
5.3 A class with an interface should implement all the methods of that
interface
Theme This program will show that a class containing an interface should
implement all the methods of the interface in its implementation section.
Progra Class C1 implements interface I1, which has got two methods , METH1
m and METH2. But, in the IMPLEMENTATION section of class C1, only METH1
Descrip is implemented.
This program will create a compilation error, establishing the theme.
Dump
report ysubdel .
interface i1.
methods : meth1 ,
meth2 .
endinterface.
class c1 definition.
public section.
interfaces : i1.
endclass.
class c1 implementation.
method i1~meth1.
write:/5 'I am meth1 from i1'.
endmethod.
endclass.
start-of-selection.
data : oref type ref to c1.
create object oref.
call method oref->i1~meth1.
Page : 63 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
5.4 Values for interface attributes are assigned at the time of inclusion in
a class
Theme One cannot specify values for attributes while declaring them in an
interface like the following fashion : DATA : <var> TYPE <type> VALUE
<val>.
Instead of doing that, one has to specify the values for different attributes
of interface at the point where the interface is declared in the public
section of a class.
Progra Interface I1 contains two numeric attributes , NUM1 and NUM2 .
m In version 1 of the program, attempt is made to specify the values while
Descr. defining those attributes in the interface. This version does not get
successfully compiled and thus establishes the theme.
In version 2, values for interface attributes are specified at the time when
I1 is included in the public section of class C1. This version gets
successfully compiled and produces a result.
interface i1 . interface i1 .
data : num1 type i value 5 , data : num1 type i ,
num2 type i value 6 . num2 type i .
endinterface. endinterface.
Page : 64 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
interface i1 .
methods : m1 ,
m2 .
endinterface.
class c1 definition.
public section.
interfaces : I1 final methods m2 .
endclass.
class c1 implementation.
method i1~m1.
write:/5 'I am m1 in c1'.
endmethod.
method i1~m2.
write:/5 'I am m2 in c1'.
endmethod.
endclass.
class c2 implementation.
method : i1~m1.
write:/5 'I am m1 in c2'.
endmethod.
endclass.
start-of-selection.
data : oref1 type ref to c1,
oref2 type ref to c2 .
create object : oref1 , oref2.
call method : oref1->i1~m1 , “ Output : I am m1 in c1
oref2->i1~m1 , “ Output : I am m1 in c2
oref1->i1~m2 , “ Output : I am m2 in c1
oref2->i1~m2 . “ Output : I am m2 in c1
Output I am m1 in c1
I am m1 in c2
I am m2 in c1
I am m2 in c1
Page : 65 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Dump
report ysubdel .
interface i1 .
methods : m1 ,
m2 .
endinterface.
class c1 implementation.
method i1~m1.
write:/5 'I am m1 in c1'.
endmethod.
endclass.
class c2 implementation.
method : i1~m2.
write:/5 'I am m2 in c2'.
endmethod.
endclass.
start-of-selection.
data : oref2 type ref to c2 .
create object : oref2.
call method : oref2->i1~m1 ,
oref2->i1~m2.
Output I am m1 in c1
I am m2 in c2
Page : 66 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
class(implementing that interface). Use of interface reference variable
paves the way for polymorphism via interface.
Progra Interface I1 , included/ implemented in class C1 has the following
m components:-
Descrip
. component nature
C_name Constant with value =ABAP
inum Instance attribute with value = 5
cnum Static attribute of value 6
M1 Instance method
M2 Static method
class c1 definition .
public section.
interfaces : I1 data values inum = 5 cnum = 6 .
endclass.
class c1 implementation.
method i1~m1.
write:/5 'I am m1 in c1'.
endmethod.
method i1~m2.
write:/5 'I am class method m2 in c1'.
endmethod.
endclass.
start-of-selection.
data : iref type ref to i1 ,
oref type ref to c1 .
create object : oref.
write:/5 oref->i1~inum ,
oref->i1~cnum ,
c1=>i1~cnum .
call method : oref->i1~m1 ,
oref->i1~m2 ,
c1=>i1~m2 .
write:/5 sy-uline .
iref = oref .
write:/5 iref->inum ,
iref->cnum ,
i1=>c_name .
call method : iref->m1 ,
iref->m2 .
Output 5 6 6
I am m1 in c1
I am class method m2 in c1
I am class method m2 in c1
Page : 67 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
5 6 ABAP
I am m1 in c1
I am class method m2 in c1
Page : 68 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
interface i2.
methods : m1 , m2.
interfaces i1.
endinterface.
class c1 definition.
public section.
interfaces : i2.
endclass.
class c1 implementation.
method : i1~m1.
write:/5 'I am m1 from i1'.
endmethod.
method : i2~m1.
write:/5 'I am m1 from i2'.
endmethod.
method : i2~m2.
write:/5 'I am m2 from i2'.
endmethod.
endclass.
START-OF-SELECTION.
data : oref type ref to c1.
create object oref.
call method : oref->i1~m1 , “ Output : I am m1 from i1
oref->i2~m1 , “ Output : I am m1 from i2
oref->i2~m2 . “ Output : I am m1 from i2
Output I am m1 from i1
I am m1 from i2
I am m2 from i2
Page : 69 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
–declaring Interfaces in a class
Progra Interface I1 contains method M1.
m Interface I2 contains method : M1 and M2 and interface I1. It aliases
Descr. method M1 of I1 as METH1.
Class C1 contains interface I2 and aliases method M2 of I2 as METH2.
All the methods :- I1~M1 , I2~M1 and I2~M2 are implemented in class
C1.
In the START-OF-SELECTION block, object OREF is created from class C1
and the alias names are used to call the methods.
Dump report ysubdel .
interface i1 .
methods m1.
endinterface.
interface i2.
methods : m1 , m2 .
interfaces i1.
aliases meth1 for i1~m1.
endinterface.
class c1 definition.
public section.
interfaces : i2.
aliases meth2 for i2~m2.
endclass.
class c1 implementation.
method i1~m1.
write:/5 'I am m1 from i1'.
endmethod.
method : i2~m1.
write:/5 'I am m1 from i2'.
endmethod.
method : i2~m2.
write:/5 'I am m2 from i2'.
endmethod.
endclass.
START-OF-SELECTION.
data : oref type ref to c1.
create object oref.
call method : oref->i2~meth1.
call method : oref->meth2 .
Output I am m1 from i1
I am m2 from i2
interface I1.
METHODS : M1 .
ENDINTERFACE.
CLASS C1 DEFINITION.
PUBLIC SECTION.
INTERFACES : I1.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD I1~M1.
WRITE:/5 'I am method m1 in c1'.
ENDMETHOD.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
INTERFACES : I1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
METHOD I1~M1.
WRITE:/5 'I am method m1 in c2'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2 ,
IREF TYPE REF TO I1 .
CREATE OBJECT : OREF1 ,
OREF2 .
IREF = OREF1.
CALL METHOD IREF->M1.
IREF = OREF2.
CALL METHOD IREF->M1.
Output I am method m1 in c1
I am method m1 in c2
6 Friendship
6.1 Friendship between Classes
Theme A class can grant friendship to another class. By granting friendship , it
allows another class to:-
Use its private components.
Instantiate it, irrespective of the CREATE PRIVATE addition.
Progra Class C2 is created using CREATE PRIVATE option. That
Page : 71 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
m means, only the class itself and its friends can instantiate
Descr. this class.
Class C2 has a private method M2 and a private attribute ,
NUM. This means that these components can be accessed
by class C2 itself and its friends.
Now, C2 has granted friendship to class C1.
So, methods of class C1 can access private components of
C2 as well as can instantiate class C2.
This establishes the theme.
Dump REPORT YSUBDEL.
CLASS C2 IMPLEMENTATION.
METHOD M2.
WRITE:/5 'I am method m2 in C2'.
ENDMETHOD.
ENDCLASS .
class c1 definition.
public section .
methods : m1.
endclass.
class c1 implementation.
method m1.
DATA : OREF2 TYPE REF TO C2.
CREATE OBJECT OREF2.
WRITE:/5 OREF2->NUM.
CALL METHOD OREF2->M2.
ENDMETHOD.
endclass.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CREATE OBJECT OREF1.
CALL METHOD OREF1->M1.
Output 5
I am method m2 in C2
Page : 72 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
This establishes the theme.
REPORT YSUBDEL.
CLASS C2 IMPLEMENTATION.
ENDCLASS .
class c1 definition.
public section .
methods : m1.
endclass.
class c1 implementation.
method m1.
DATA : OREF2 TYPE REF TO C2.
CREATE OBJECT OREF2.
WRITE:/5 OREF2->NUM.
ENDMETHOD.
endclass.
START-OF-SELECTION.
DATA : OREF11 TYPE REF TO C11.
CREATE OBJECT OREF11.
CALL METHOD OREF11->M11.
Output 5
Page : 73 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
as its friend.
REPORT YSUBDEL. REPORT YSUBDEL.
Output START-OF-SELECTION.
15 START-OF-SELECTION.
DATA : OREF TYPE REF TO C1. DATA : OREF TYPE REF TO C1.
CREATE OBJECT OREF . CREATE OBJECT OREF .
CALL METHOD OREF ->METHPUB. CALL METHOD OREF ->METHPUB.
7 Events
7.1 Events with Handler Method in the same class
Theme Event is a mechanism by which method of one class can raise method of
another class, without the hazard of instantiating that class .
The steps to be followed are as follows:-
Create an event in a class
Create a triggering method in the same class which will raise the
event.
Create an event handler method for the event in same/other class.
Register the event handler method in the program.
Now, your settings are complete. Create an object from the class
containing the event and call the triggering method to raise the event.
Progra Class C1 contains an event E1, for which the triggering method is T1.
m Event handler method for event E1 is M1, placed in the same class C1.
Descr. Registration is done at runtime for M1.
Object is created from class C1 and the triggering method T1 is called,
Page : 74 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
which raises the event and ultimately calls event handler method M1.
Dump REPORT YSUBOOPS7 .
CLASS c1 DEFINITION.
PUBLIC SECTION.
*(1)Creating event : E1
EVENTS: E1.
*(2) Creating an event handling method. This method can belong to
* same or different class
METHODS: M1 FOR EVENT E1 OF c1.
* Method to raise the event
METHODS : T1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
* Method : M1 will be called when the event is raised
METHOD : M1.
write:/5 ' I am the event handler method'.
ENDMETHOD.
* Method : T1 will raise the event
METHOD : T1.
write:/5 'I am T1, going to raise event E1'.
raise event E1.
ENDMETHOD.
ENDCLASS.
Start-of-selection.
Data: oref type ref to c1.
Create object: oref .
* Registering the event handler method
SET HANDLER oref->M1 FOR oref .
* Calling the event which will raise the event.
call method oref->T1.
CLASS c1 DEFINITION.
PUBLIC SECTION.
* Creating event : E1
EVENTS: E1.
* Triggering method : T1
METHODS : T1.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
* Creating an event handling method.
METHODS: M1 FOR EVENT E1 OF c1.
Page : 75 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
endclass.
CLASS c1 IMPLEMENTATION.
* Method : T1 will raise the event
METHOD : T1.
write:/5 'I am T1, going to raise event E1'.
raise event E1.
ENDMETHOD.
ENDCLASS.
class c2 implementation.
* Method : M1 will be called when the event is raised
METHOD : M1.
write:/5 ' I am the event handler method in c2'.
ENDMETHOD.
endclass.
Start-of-selection.
Data: oref1 type ref to c1,
oref2 type ref to c2.
Create object: oref1 , oref2 .
* Registering the event handler method
SET HANDLER oref2->M1 FOR oref1 .
* Calling the event which will raise the event.
call method oref1->T1.
Output
I am T1, going to raise event E1
I am the event handler method in c2
7.3 More than one event handler method can exist for same event
Theme For an event in a class, there can be more than one event handler methods
in same or different class. However, at runtime only one event handler
method will be triggered at a time, based on the registration.
Progra Class C1 contains an event E1, for which the triggering method is
m T1 and the event handler methods are :-
Descr. M1 in same class C1.
M2 in another class C2.
In the START-OF-SELECTION block, objects are created from class
C1 and C2.
First, registration is made using method M1 of class C1 as event
handler method.
Then, the event E1 is raised, calling method T1. This raises event
handler method M1 of class C1.
After that, the earlier registration is de-activated and new
registration is made for method M2 of class C2 as event handler
method .
Event E1 is raised calling method T1. This raises event handler
method M2 of class C2.
CLASS c1 DEFINITION.
PUBLIC SECTION.
* Creating event : E1
EVENTS: E1.
Page : 76 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
* Creating an event handling method.
METHODS: M1 FOR EVENT E1 OF c1.
* Method to raise the event
METHODS : T1.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
* Creating an event handling method.
METHODS: M2 FOR EVENT E1 OF c1.
endclass.
CLASS c1 IMPLEMENTATION.
* Method : T1 will raise the event
METHOD : T1.
write:/5 'I am T1, going to raise event E1'.
raise event E1.
ENDMETHOD.
* Method : M1 will be called when the event is raised
METHOD : M1.
write:/5 ' I am the event handler method M1 in c1'.
ENDMETHOD.
ENDCLASS.
class c2 implementation.
* Method : M2 will be called when the event is raised
METHOD : M2.
write:/5 ' I am the event handler method M2 in c2'.
ENDMETHOD.
endclass.
Start-of-selection.
Data: oref1 type ref to c1,
oref2 type ref to c2.
Create object: oref1 , oref2 .
* Registering the event handler method
SET HANDLER oref1->M1 FOR oref1 .
* Calling the event which will raise the event.
call method oref1->T1.
* De-Registering the earlier event handler method
SET HANDLER oref1->M1 FOR oref1 ACTIVATION space .
* Registering the new event handler method
SET HANDLER oref2->M2 FOR oref1 .
* Calling the event which will raise the event.
call method oref1->T1.
Page : 77 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 DEFINITION.
PUBLIC SECTION.
* Creating event : E1
CLASS-EVENTS: E1.
* Creating an event handling method.
METHODS: M1 FOR EVENT E1 OF c1.
* Method to raise the event
CLASS-METHODS : T1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
* Method : T1 will raise the event
METHOD : T1.
write:/5 'I am T1, going to raise event E1'.
raise event E1.
ENDMETHOD.
* Method : M1 will be called when the event is raised
METHOD : M1.
write:/5 ' I am the event handler method M1 in c1'.
ENDMETHOD.
ENDCLASS.
Start-of-selection.
Data: oref1 type ref to c1.
Create object: oref1 .
* Registering the event handler method
SET HANDLER oref1->M1 .
* Calling the event which will raise the event.
call method oref1->T1.
Page : 78 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS c1 DEFINITION.
PUBLIC SECTION.
EVENTS : E1 EXPORTING value(NUM1) TYPE I
value(NUM2) TYPE I.
CLASS C1 IMPLEMENTATION.
METHOD : M1.
WRITE:/5 'First input ' , num1 .
write:/5 'Second input ' , num2 .
ENDMETHOD.
METHOD T1.
RAISE EVENT E1 exporting num1 = 2
num2 = 3.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : oref TYPE REF TO c1.
CREATE OBJECT oref.
SET HANDLER oref->M1 for oref.
call method oref->T1.
Page : 79 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
8 Class-Based Exceptions
8.1 Using SAP provided exception class
Theme Errors in a program, which are detected at runtime and can be trapped, can
be dealt with using SAP provided standard exception-classes.
Progra This program makes a runtime error where a division by zero is observed.
m Let us take three different versions of the program and see the outputs
Descr.
Ver.N Program Output
o
1 REPORT YSUBCLASS_EXCEPTION. Short Dump as follows:-
Runtime errors
DATA: i TYPE i VALUE 1. COMPUTE_INT_ZERODIVIDE
START-OF-SELECTION. Exception CX_SY_ZERODIVIDE
i = i / 0. Occurred on 12.04.2004 at 17:02:18
Divide by 0 (type 1).
START-OF-SELECTION.
TRY.
i = i / 0.
CATCH cx_sy_zerodivide.
write:/5 'Divide by zero caught'.
ENDTRY.
The three versions basically represent the same program, but shows how an error
can be trapped using SAP provided standard exception class.
Page : 80 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
8.2 When both superclass and subclass are used to track error
Theme SAP provided standard exception classes can reside in different levels of
hierarchy tree; CX_ROOT being at the top. So, if both superclass and its
subclass are used in a program to detect errors in TRY…ENDTRY block, the
subclass should be used first, then the superclass.
Progra This program creates a division by zero problem. Here, both the superclass
m CX_ROOT and subclass CX_SY_ZERODIVIDE is used to trap errors.
Descr.
In version 1, superclass is used first to trap the error- which creates a
compilation error.
In version 2, subclass is used first – which gets compiled and executed
successfully.
Dump Versio Code Output
n
1 REPORT YSUBCLASS_EXCEPTION. Compilation error :-
Exception in the
DATA: i TYPE i VALUE 1. CATCH clauses are
not sorted in
START-OF-SELECTION.
TRY.
ascending order
i = i / 0.
CATCH cx_root.
write:/5 'Error trapped'.
CATCH cx_sy_zerodivide.
write:/5 'Div. by zero!’.
ENDTRY.
2 REPORT YSUBCLASS_EXCEPTION. Div. by zero!
START-OF-SELECTION.
TRY.
i = i / 0.
CATCH cx_sy_zerodivide.
write:/5 'Div. by zero!'.
CATCH cx_root.
write:/5 'Error trapped'.
ENDTRY.
Page : 81 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
start-of-selection.
try.
perform sub_check_no using 5 .
catch cx_sy_zerodivide.
write:/5 'Hello' .
endtry.
Output Hello
Page : 82 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
try.
raise exception type cx_sy_zerodivide.
CATCH cx_sy_zerodivide.
write:/5 'Exception caught'.
endtry.
Output Exception caught
Page : 83 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
8.5 Objects are created from exception classes when error is trapped
Theme When a class-based exception is trapped using TRY…CATCH…ENDTRY
statement, objects are created from the exception class. One can create
the object using CATCH <exception name> INTO <exception class
reference variable> statement.
CX_ROOT is at the top of the inheritance tree for all SAP provided exception
class and have some pre-defined methods available, which are adopted by
all exception-classes.
Progra The program involves a division by zero error in the guarded section, which
m raises an exception on exception-class : CX_SY_ZERODIVIDE.
Descr. A reference variable , EREF with static type referring to the exception class
CX_SY_ZERODIVIDE is used to create an object while using the CATCH
statement.
Once the object is created, it can be used to manipulate some of the
methods and attributes of the class CX_SY_ZERODIVIDE, which has been
inherited by this class from CX_ROOT.
Dump REPORT YSUBOOPS17 .
data : inum type i value 5 ,
descrip type string ,
progname like sy-repid ,
lineno type i .
try.
inum = inum / 0.
CATCH cx_sy_zerodivide into eref.
* Utilizing methods/attributes using object of the exception classes
call method eref->get_text
receiving result = descrip.
write:/5 'Name of the error trapped : ' , descrip.
Page : 84 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS: m1 raising cx_my_exception .
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
RAISE EXCEPTION TYPE cx_my_exception.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
CREATE OBJECT oref.
oref->m1( ).
CATCH cx_my_exception INTO ex.
write:/5 'My Exception caught'.
ENDTRY.
Page : 85 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
START-OF-SELECTION.
TRY.
TRY.
NUM = NUM / 0.
CATCH cx_sy_ZERODIVIDE .
WRITE:/5 'Division by 0 caught'.
NUM = 'SUBHENDU'.
ENDTRY.
CATCH cx_sy_conversion_no_number.
WRITE:/5 'Cannot be converted to number'.
ENDTRY.
Page : 86 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Under such circumstances, the CLEANUP section of the inner block gets
executed first – then the CATCH section of the outer block works.
Dump REPORT YSUBCLASS_EXCEPTION_3.
START-OF-SELECTION.
TRY.
TRY.
num = 'subhendu'.
cleanup.
write:/5 'In cleanup'.
ENDTRY.
CATCH cx_sy_conversion_no_number.
WRITE:/5 'Cannot be converted to number'.
ENDTRY.
Output
In cleanup
Cannot be converted to number
Page : 87 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Enter the description. The name of the interface will be automatically proposed.
Double click on it.
Page : 88 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Enter the name of the method you want to create. Click the Paramters pushbutton to
create parameters for the method.
A popup window will ask you for the definition name. Enter the name of the BADI
definition which you have created.
Press Enter.
Enter the description for the implementation. Then save and activate it.
Page : 89 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Page : 90 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Page : 91 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
9.2 Multiple Implementation
Page : 92 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Now, you want to create another implementation of the same BADI. Let us examplify
the concept. Say, you want that when the user will enter ‘GARI’ in the selectiuon-
screen, it will stand for ‘CAR’ internally and selection will be done out of MARA table
based on material code : ‘CAR’. O, you define another implementation of the same
BADI from transaction : SE19.
Page : 93 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Now, when you will execute the program and enter ‘GARI’ in the material code field
in the selection-screen, it will get internally translated to ‘CAR’ when the second
implementation will be active.
Page : 94 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
9.3 Searching for BADI in SAP Transaction and Implementing it
There is a business demand in ABC corporation . when the user will post goods
receipt via transaction MIGO, he should enter same date in document date and
posting date field.
Else, an information message will ask the user to do that.
You, as a SAP Technical Consultant, is asked to translate this idea into the
appropriate section of the code.
Your manager has asked you to use BADI instead of any user or field exits to
implement the idea.
So, you get to know now that there are two BADIs which can come to your use. They
are:
MB_MIGO_BADI
MB_MIGO_ITEM_BADI.
Page : 95 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Now, you have to go to transaction SE18 and explore each of the BADIs to find out
the suitable one. In fact, the suitable one will have a method in it for which
import/export parameters should have some reference to document/posting dates.
Now, you will implement this BADI. Go to transaction SE19 and create an
implementation for the BADI. In the code for the method, write the following:-
method IF_EX_MB_MIGO_ITEM_BADI~ITEM_MODIFY .
IF is_gohead-bldat ne is_gohead-budat.
message i398(00) with 'Both posting and document dates should be same'.
ENDIF.
L_INI = IS_GOHEAD-FRBNR+0(2).
TRANSLATE L_INI TO UPPER CASE.
IF L_INI NE 'NP'.
MESSAGE I398(00) WITH 'Bill of Lading should start with NP'.
ENDIF.
endmethod.
Then, save and activate it. Then, perform a transaction via MIGO. Your requirement
will be fulfilled.
Page : 96 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
SAP allows you to enhance menus in its user interfaces using function codes. These
function codes must adhere to the form /namespace/+<...>, just like in SMOD/CMOD
enhancements. They are assigned to a specific enhancement and only appear in their
corresponding menus once an implementation of this enhancement has been
activated.
Application developers reserve specific function codes for customers when defining a
Business Add-In. They use the Menu Painter to include these codes in the appropriate
menu lists. Application developers must also ensure that these menu options are
called in their applications and that the corresponding add-in methods are also
retrieved. Customers can take advantage of menu enhancements by creating a new
implementation, choosing a text for the menu option, and then programming the
method used to determine what action is performed when the menu enhancement is
called.
4. Call the Menu Painter or double-click on your program name or function code
to branch to user interface maintenance in the Menu Painter. Enter your
function code in the appropriate menu list. If you have accessed the Menu
Painter directly during add-in definition, you can call your menu lists by
choosing Goto --> Object lists --> Menu list instead.
(…)
case fcode.
when 'SAP'.
(…)
when '+CUS'
call method …
Page : 97 of 98
Examples on Object Oriented Programming in ABAP Subhendu
Majumdar
Implementing a Menu Enhancement
1. Create an implementation and choose Fcodes. All data adopted from your
Business Add-In's definition is displayed here. You can make entries for the
implementation on the right. You can also double-click on the first input field.
The following dialog box appears:
Here you may enter a text for your function code, the name of an icon and a
text for the icon, and a short informational text.
The actions that you want the system to perform after the pushbutton is
chosen must be programmed in the appropriate method, either manually or
using default source code that has been provided to you.
Menu enhancements only become visible after the implementation has been
activated and the application program that calls the Business Add-In has been
executed.
Page : 98 of 98