Sunteți pe pagina 1din 108

Examples on Object Oriented Programming in ABAP

Page : 1 of 108
Examples on Object Oriented Programming in ABAP
PREFACE
This documentation contains examples on Object Oriented Programming in ABAP !t co"ers
the indi"idual sections in detail and contains examples illustrating the important concepts
Examples are positi"e or nagati"e # the positi"e examples demonstrate implementation of
concepts in program$ the negati"e examples create compilation errors$ %hich sho% the dos
and don&t&s %hile coding for a class'object in ABAP
(are has been ta)en to use simple examples$ %hich spa%ns not more than one page
*iscussion on an example is categorised under four major heads:+
Theme: - This section explains %hat the example is going to demonstrate
Program Description: - This section introduces to the program # briefl, detailing
the components of the programand %hat it is tr,ing to achie"e
Dump:- (ontains code dump
Output: - -ho%s the output of the program %hen executed or sho%s the
compilation errors for negati"e examples
The best %a, to learn an,thing is learning b, examples The entire content has been
designed and documented in such a %a, that the reader can easil, grasp the matter and
implement it in his course of learning
The author %ill remain grateful to the responsible readers if the, can point out mista)es in
the documentation and suggest further impro"ements on this effort
Page : . of 108
Examples on Object Oriented Programming in ABAP
!/*E0
1 (lass 1
11 Accessibilit, of different sections of a class1
1. -ubclass cannot access the pri"ate component of superclass8
12 External users cannot access protected'pri"ate components of a class3
14 5ocal (lass can understand data and t,pes in the global area of the program10
11 (lass can be instantiated %ithin implementation of another class1.
16 *eferred *efinition of a (lass 12
17 Place to put non+declarati"e statements 14
18 8se of 9ield -,mbols in (lass 11
13 8se of -tatic Attributes 16
110 (reation of :lobal class and using it in a local program17
. ;ethods ..
.1 ;ethod %ith one import parameter' onl, one non+optional parameter..
.. !mport parameters passed b, ref can&t be changed inside the method .2
.2 8se of P<E9E<<E* PA<A;ETE< in a method.4
.4 8se of E0PO<T and (=A/:!/: parameters of a method.1
.1 ;ethod using !nternal Table as one of the parameters.6
.6 8se of <ET8</!/: parameters in method.7
.7 *emo on -tatic ;ethod .8
.8 -tatic methods can onl, use static attributes$ instance methods use both.3
.3 ;ethod <aising Exceptions 20
.10 ;ethod can call itself 21
.11 8se of ;E in methods 2.
.1. Pointer Tables 22
.12 *,namic ;ethod (alls 24
.14 8se of parameter table 21
.11 8se of Exception Table 26
2 (onstructors 27
21 !nstance (onstructors get fired at the time of class instantiation27
2. !nstance (onstructors can ha"e import parameters28
22 (onstructors cannot ha"e an, export parameters23
24 !nstance (onstructors can raise exceptions 40
21 8se of static constructor 41
26 -tatic constructor can be triggered at the beginning of a processing
bloc)>form 'e"ent'bloc)'procedure? 4.
27 -tatic'(lass constructors cannot ha"e an, interface42
4 !nheritance 44
41 -ubclass can access public'protected components of superclass44
4. -ubclass can re+implement inherited methods from superclass46
42 Objects cannot be created from an abstract class47
44 Abstract methods cannot be implemented in abstract class48
41 9inal classes cannot ha"e an, subclass 43
46 9inal methods cannot be redefined in the subclasses10
47 -tatic attributes exist onl, once per inheritance tree11
48 (onstructors of superclass flo%s do%n the chain1.
43 -ubclass can ha"e enhanced constructor than its superclass12
410 -tatic constructor of a class is called onl, once per program11
Page : 2 of 108
Examples on Object Oriented Programming in ABAP
411 -tatic t,pe and *,namic t,pe of a "ariable16
41. -tatic t,pe should be more general than d,namic t,pe of a reference "ariable18
412 ;ethod of a parent class$ used from its subclass$ uses attributes of the parent
class onl,$ if the method is not re+defined in subclass13
414 *emo on @idening (ast 60
1 !nterface 61
11 -imple use of an interface 61
1. !nterfaces can onl, be implemented in the public section of a class6.
12 A class %ith an interface should implement all the methods of that interface62
14 Aalues for interface attributes are assigned at the time of inclusion in a class64
11 8se of 9!/A5 methods from !nterface61
16 8se of Abstract methods from !nterface 66
17 8se of !nterface <eference Aariable 67
18 8se of /ested !nterface 63
13 8sing A5!A-E- 70
110 Pol,morphism "ia !nterfaces 71
6 9riendship 7.
61 9riendship bet%een (lasses 7.
6. -ubclasses of friends can also become friends72
62 9riendship is one sided 74
7 E"ents 71
71 E"ents %ith =andler ;ethod in the same class71
7. E"ent %ith e"ent handler method in different class76
72 ;ore than one e"ent handler method can exist for same e"ent77
74 8se of static e"ent 73
71 E"ents %ith export parameters 80
8 (lass+Based Exceptions 81
81 8sing -AP pro"ided exception class 81
8. @hen both superclass and subclass are used to trac) error8.
82 Propagation of (lass+Based exceptions in procedures to the caller82
84 Program can raise exceptions based on -AP standard exception+classes84
81 Objects are created from exception classes %hen error is trapped81
86 *emo on 5ocall, *efined Exception+(lass86
87 /ested T<BCE/*T<B bloc) 87
88 8se of (5EA/8P section 88
3 BA*!s > Business Add+!ns? 83
31 -ingle !mplementation of BA*! 83
3. ;ultiple !mplementation 32
32 -earching for BA*! in -AP Transaction and !mplementing it36
34 ;enu Enhancements 38
341 *emand : 38
34. -teps to be follo%ed 38
34.1 -tep 1 : (reate the main program in -E2838
34.. -tep . :+ (reate BA*! *efinition from -E1833
34.2 -tep 2 :+!mplement the BA*! from transaction -E13102
34.4 -tep 4:+ Place the function codes properl, in the menu painter101
34.1 -tep 1:+ (omplete the program details106
Page : 4 of 108
Examples on Object Oriented Programming in ABAP

1 Class
1.1 Accessibility o !ierent sections o a class
Theme 9rom this program$ ,ou %ill learn:+
1 =o% to define$ implement and instantiate a class
. @hat are the different sections of "isibilit, in a class
2 =o% to define instance attributes and get them accessed b,
external users
The follo%ing program %ill also sho% that :+
*ata declared in public section can be accessed b, the class
itself$ b, its subclasses as %ell as b, other users outside the
class
*ata declared in the protected section can be accessed b, the
class itself$ and also b, its subclasses but not b, external
users outside the class
*ata declared in the pri"ate section can be accessed b, the
class onl,$ but not b, its subclasses and b, external users
outside the class
Brief *escription This program contains a class : parentclass %ith follo%ing attributes in
different sections:+
Common!ata in public section
Protect!ata in protected section
Pri"ate!ata in pri"ate section
The method sho#"al in class : parentclass displa,s "alues of all the
attributes
This !emonstrates that class can access all its attributes.
(lass chil!class is a subclass of class parentclass$ %hich has a method
: sub"al
!t displa,s the "alue for the data : commondata and protectdata
Then$ it changes the "alues for both and displa,s them again
This !emonstrates that subclass can access$change public$
protecte! attributes o superclass.
!n the %TART-OF-%E&ECT'O( e"ent$ object : parent is instantiated
from class : parentclass and object : chil! is instantiated from class :
chil!class
Then $ the method sho#"al of parent>object of parentclass? and
method sub"al of chil!>object of childclass? is called $ %hich displa,s
the "alues of different attributes
Page : 1 of 108
Examples on Object Oriented Programming in ABAP
Then$ the public attribute of object parent is changed and the
changed "alue is displa,ed
This !emonstrates that e)ternal users can change$!isplay public
attributes o a class.
*ump of the program:+
REPORT YSUBDEL LINE-SIZE 120.
CLASS parentclass DEINITION .
PUBLIC SECTION.
DATA ! c"##"n$ata%&0' t(pe c )al*e +Access,-le t" all+.
.ET/ODS ! S/O01AL.
PROTECTED SECTION.
DATA ! pr"tect$ata%20' t(pe c )al*e +Pr"tecte$ $ata+.
pr,)ate sect,"n.
$ata ! pr,)ate$ata%&0' t(pe c )al*e +Pr,)ate $ata+.
ENDCLASS.
CLASS parentclass I.PLE.ENTATION.
.ET/OD ! S/O01AL.
3r,te!45 +All $ata 6r"# parentclass s7"3n!-+.
3r,te!4 s(-*l,ne.
0RITE!45 CO..ONDATA8
45 PROTECTDATA8
45 PRI1ATEDATA.
en$#et7"$.
en$class.
CLASS c7,l$class DEINITION IN/ERITIN9 RO. parentclass.
PUBLIC SECTION .
.ET/ODS ! s*-)al.
ENDCLASS.
CLASS c7,l$class I.PLE.ENTATION.
#et7"$ ! s*-)al.
s:,p 1.
3r,te!45 +Data "6 parent s7"3n 6r"# c7,l$-+.
3r,te!45 s(-*l,ne.
0RITE!45 CO..ONDATA8
45 PROTECTDATA.
C"##"n$ata ; +P*-l,c $ata c7an<e$ ,n s*-class+.
Pr"tect$ata ; +Pr"tecte$ $ata c7an<e$ ,n s*-class+.
3r,te!45 s(-*l,ne.
0RITE!45 CO..ONDATA8
45 PROTECTDATA.
en$#et7"$.
en$class.


Page : 6 of 108
Examples on Object Oriented Programming in ABAP
START-O-SELECTION.
DATA ! parent t(pe re6 t" parentclass 8
c7,l$ t(pe re6 t" c7,l$class .
create "-=ect ! parent 8
c7,l$ .
call #et7"$ ! parent->s7"3)al 8
c7,l$->s*-)al.
s:,p 2.
parent->c"##"n$ata ; ?User c7an<,n< p*-l,c $ata@.
3r,te!45 parent->c"##"n$ata.
Output
All data from parentclass sho%n:+

Accessible to all
Protected data
Pri"ate data

*ata of parent sho%n from child+

Accessible to all
Protected data

Public data changed in subclas
Protected data changed in subclass


8ser changing public data


Page : 7 of 108
Examples on Object Oriented Programming in ABAP
1.* %ubclass cannot access the pri"ate component o superclass
Theme The program demonstrates that subclasses cannot access the pri"ate
components of superclass
Program
description
The program used here is similar to abo"e %ith change in the method :
sub"al of class : childclass This method is no% attempting to access the
attribute : pri"atedata $ %hich is a pri"ate attribute of its superclass :
parentclass
On compilation$ the program %ill gi"e a compilation error
This !emonstrates that pri"ate components o superclass cannot be
accesse! by subclasses
Program *ump Ta)e the first program Onl, change the method : sub"al of class :
chil!class as follo%s:+
method : sub"al
s)ip 1
%rite:'1 DAll data from parent class sho%n b, subclassD
%rite:'1 s,+uline
@<!TE:'1 (O;;O/*ATA$
'1 P<OTE(T*ATA$
'1 pri"atedata
endmethod
Output The program %ill not compile !t %ill sho% an error message:+
Page : 8 of 108
Examples on Object Oriented Programming in ABAP
1.+ E)ternal users cannot access protecte!$pri"ate components o a class
Theme This program %ill demonstrate that external users cannot access the
protected and pri"ate components of a class
Program
*escription
!n this program $ class C1 has three attributes declared in different
sections as follo%s:+
Common!ata in public section
Protect!ata in protected section
Pri"ate!ata in pri"ate section
!n the main program$ an object $ O,-1 is created from class C1 and
an incorrect attempt is made to displa, the protected and pri"ate
attribute of class C1 using its object O,-1
(ompilation of this program produces an error
This !emonstrates : protecte! an! pri"ate components o a class
cannot be accesse! by e)ternal users.
*ump
REPORT YSUBDEL LINE-SIZE 120.
CLASS c1 DEINITION .
PUBLIC SECTION.
DATA ! c"##"n$ata%&0' t(pe c )al*e +Access,-le t" all+.
PROTECTED SECTION.
DATA ! pr"tect$ata%20' t(pe c )al*e +Pr"tecte$ $ata+.
pr,)ate sect,"n.
$ata ! pr,)ate$ata%&0' t(pe c )al*e +Pr,)ate $ata+.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
en$class.
START-O-SELECTION.
DATA ! "-=1 t(pe re6 t" c1.
create "-=ect ! "-=1.
3r,te!45 "-=1->pr"tect$ata 8
"-=1->pr,)ate$ata.

Output On compilation$ an error %ill be generated %hich %ill pro"e that
protected and pri"ate components of a class cannot be accessed b,
external users
Page : 3 of 108
Examples on Object Oriented Programming in ABAP
1.. &ocal Class can un!erstan! !ata an! types in the global area o the program.
Theme This program %ill demonstrate the follo%ing:+
*ifferent attributes of a class can be constructed utiliEing the data
and t,pes declared outside the class$ in the global area of the
program
*ata declared in the global area of the program can be used directl, in
a class
Program
description
The global section of this program contains a t,pe : T/P0TA, and an integer
"ariable 1 (231
These t,pe and data are used %hile defining attributes &0(231>integer? and
'T0TA, >internal table? for class C1 Also$ the global data &0(23 is used
directl, inside the program
This !emonstrates the theme.
*ump
REPORT YSUBDEL1 LINE-SIZE 120.
TYPES ! BE9IN O TYPATAB 8
NA.E%15' TYPE C 8
A9E TYPE I 8
END O TYPATAB .
DATA ! n*#1 t(pe , )al*e 5 .
CLASS c1 DEINITION .
p*-l,c sect,"n.
#et7"$s ! #et71 .
DATA ! lAn*# l,:e n*#1 8
,tAta- t(pe stan$ar$ ta-le "6 t(pAta- 8
3Ata- l,:e l,ne "6 ,tAta-.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
#et7"$ ! #et71 .
$ata ! lAcn*#%2' t(pe c.
lAn*# ; 0.
$" 5 t,#es.
lAn*# ; lAn*# B 1.
lAcn*# ; lAn*#.
c"ncatenate +St*$ent-+
lAcn*#
,nt" 3Ata--na#e.
3Ata--a<e ; n*#1 C lAn*# .
appen$ 3Ata- t" ,tAta-.
clear 3Ata-.
en$$".
l""p at ,tAta- ,nt" 3Ata-.
3r,te!45 3Ata--na#e 8
3Ata--a<e.
en$l""p.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! "-=1 t(pe re6 t" c1.
create "-=ect ! "-=1.
call #et7"$ "-=1->#et71.
Page : 10 of 108
Examples on Object Oriented Programming in ABAP
Output -tudent+1 1
-tudent+. 10
-tudent+2 11
-tudent+4 .0
-tudent+1 .1
Page : 11 of 108
Examples on Object Oriented Programming in ABAP
1.4 Class can be instantiate! #ithin implementation o another class
Theme This program %ill demonstrate that an object can be created from a
class> >%hich %as created %ith no (<EATE P<!AATEFP<OTE(TE* option at
the time of its definition? in the implementation section of another class
Program
*escription
This program contains t%o classes # C&A%%1 and C&A%%*
(lass C&A%%1 contains method : 3ET5OD1 %hich displa,s "alue of some
integer "ariable
(lass C&A%%* contains method : 3ET5OD* !n the method implementation $
an object is created from class : C&A%%1 and then that object is used to call
method 3ET5OD1
This !emonstrates that ob6ect can be create! rom a class7C&A%%18
#ithin implementation section o another class7C&A%%*8.
<EPO<T B-8BOOP-17
class class1 definition
public section
methods : method1
endclass
class class. definition
public section
methods : method.
endclass
class class1 implementation
method :method1
data : iGnum t,pe i "alue .
%rite:'1 iGnum
endmethod
endclass
class class. implementation
method : method.
data : obj1 t,pe ref to class1
create object obj1
call method obj1+Hmethod1
endmethod
endclass
start+of+selection
data : m,Gobj t,pe ref to class.
create object : m,Gobj
call method m,Gobj+Hmethod.
Output .
Page : 1. of 108
Examples on Object Oriented Programming in ABAP
1.9 Deerre! Deinition o a Class
Theme This program %ill demonstrate ho% one can refer to a class %ithout defining
the class before that point But$ the class has to be defined later on
Program
description
!n this program $ class C1 has an interface reference O* declared %ith
reference to class C* But$ before that$ class C* is not defined !t is defined
later %ith a single public attribute $ (23
This !emonstrates the theme.
!n the main section of the program$ object : O,-1 is created from class C1
Then$ an object is created from the reference "ariable O* in class C1 9inall,$
the attribute num of that object is displa,ed
*ump
rep"rt (s*-$el1.
CLASS C2 DEINITION DEERRED.
CLASS C1 DEINITION.
PUBLIC SECTION.
DATA O2 TYPE RE TO C2.
ENDCLASS.
CLASS C2 DEINITION.
p*-l,c sect,"n.
$ata ! n*# t(pe , )al*e 5.
ENDCLASS.
start-"6-select,"n.
$ata ! "-=1 t(pe re6 t" C1.
CREATE OBDECT "-=1.
create "-=ect "-=1->"2.
3r,te!45 "-=1->"2->n*# .
Output 1
Page : 12 of 108
Examples on Object Oriented Programming in ABAP
1.: Place to put non-!eclarati"e statements
Theme 9or a class$ the !;P5E;E/TAT!O/ section can immediatel, follo% the class
*E9!/!T!O/ section !f this is so$ then all the non+declarati"e statements
> "iE$ processing statements outside an, class definition' implementation ?
should be placed under a processing bloc)$ such as -TA<T+O9+-E5E(T!O/
Program
description
This program contains a class (1 %ith a method ;1
!n "ersion 1$ the !;P5E;E/TAT!O/ part of the class follo%s the class
definition section But$ the non+declarati"e statements are not placed under
an, bloc) This creates a compilation error
!n "ersion .$ the non+declarati"e statements are not placed under the bloc)
-TA<T+O9+-E5E(T!O/ !t gets correctl, compiled
This !emonstrates the theme.
;ersion 1: !ncorrect ;ersion * : (orrect
Output Aersion 1 creates compilation error
Aersion . gets correctl, compiled
Page : 14 of 108
<EPO<T B-8B*E5
class c1 definition
public section
methods : m1
endclass

class c1 implementation
method : m1
%rite:'1 D! am method m1D
endmethod
endclass


data : obj1 t,pe ref to c1
create object obj1
call method obj1+Hm1
<EPO<T B-8B*E5
class c1 definition
public section
methods : m1
endclass

class c1 implementation
method : m1
%rite:'1 D! am method m1D
endmethod
endclass

%TART-OF-%E&ECT'O(.
data : obj1 t,pe ref to c1
create object obj1
call method obj1+Hm1
Examples on Object Oriented Programming in ABAP
1.< 2se o Fiel! %ymbols in Class
Theme 9ield -,mbols can be used to contain "alue of an, "ariable in a class
Program
*escription
The program uses a field s,mbol$ I9-H !t handles the "alues of instance
attribute$ inum and static attribute $ onum
*ump <EPO<T B-8BGA--!:/G9-
9!E5*+-B;BO5- : I9-H TBPE A/B
class c1 definition
public section
J !nstance attribute : inum declared belo%
data : inum t,pe i "alue 1
J static attribute onum declared belo%
class+data : onum t,pe i "alue 10
endclass
class c1 implementation
endclass
start+of+selection
data : oref1 t,pe ref to c1
create object oref1
J Assigning instance attribute to field s,mbol IfsH
assign oref1+Hinum to IfsH
%rite:'1 IfsH
J Assigning static attribute to field s,mbol
assign oref1+Honum to IfsH
%rite:'1 IfsH
assign c1KHonum to IfsH
%rite:'1 IfsH
Output 1
10
10
Page : 11 of 108
Examples on Object Oriented Programming in ABAP
1.= 2se o %tatic Attributes
Theme This program %ill demonstrate that : -tatic sttributes of a class are
retained throughout the entire runtime All the objects %ithin a class can
access its static attributes
Program
*escription
The program contains a class C1 %ith static attribute : (23 The
method : 31 increments the static attribute b, 1 and displa,s the "alue
each time it is called
!n the main -TA<T+O9+-E5E(T!O/ portion$ t%o objects : O,-1 and
O,-* are created from class C1.
9irst$ static attribute : (23 is changed and accessed outside the class
using the class component selector $ LKH&
Then$ both objects O,-1 and O,-* are used to call method : 31 %hich
sho%s the ne% "alue of static attribute : (23
That the "alue of the static attribute gets incremented each time %hen
the method 31 of different objects is called sho%s that this "ariable is
able to retain its "alue through the entire runtime
*ump
rep"rt (s*-$el.
CLASS c1 DEINITION .
PUBLIC SECTION.
CLASS-DATA ! NU. TYPE I .
.ET/ODS ! .1.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD #1 .
n*# ; n*# B 1.
3r,te!45 n*# .
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
c1;>n*# ; &.
3r,te!45 c1;>n*# .
DATA ! ORE1 TYPE RE TO C1 8
ORE2 TYPE RE TO C1 .
CREATE OBDECT ! ORE1 8
ORE2 .
CALL .ET/OD ORE1->.1 .
CALL .ET/OD ORE2->.1.
Output 2
4
1
Page : 16 of 108
Examples on Object Oriented Programming in ABAP
1.1> Creation o ?lobal class an! using it in a local program
Theme This example %ill sho% ,ou ho% to create a class globall, and use it in ,our local
program
Program
Descr.
There is a demand to create a global class %hich %ill furnish a list of materials
along %ith their descriptions based on the material t,pe
:lobal class @?ET03ATER'A&% %ill be created ;ethod &'%T03ATER'A&% %ill
belong to this class %hich %ill ta)e material t,pe as an input and %ill furnish a list
of material codes and their descriptions belonging to that material t,pe
%teps 9ollo% the steps outlined belo% to perform the tas)
Page : 17 of 108
Examples on Object Oriented Programming in ABAP
%tep 1. Create the class rom %E*.:-
:o to transaction -E.4
Enter the name of the global class ,ou %ant to create$ %ith LB& or LM& at the beginning
Press (reate pushbutton
A dialog %indo% sho%n abo"e %ill appear (hec) the radiobutton : Class
Press Enter
Another dialog %indo% sho%n abo"e %ill appear Enter the description for the class
-elect from the 'nstantiation listbox %hether ,ou %ant to create the class as
P8B5!('P<OTE(TE*'P<!AATE'AB-T<A(T
(hec) the radiobutton for 2sual A,AP Class
(hec) the chec)box for Final
Press %a"e pushbutton
Enter the pacAage name or sa"e it as 5ocal object
Page : 18 of 108
Examples on Object Oriented Programming in ABAP
%tep * : Create the metho! : &'%T03ATER'A&%
:o to the tab+page : 3etho!s.
Enter the details for the method # mention name$ t,pe of method>instance'static?$
in %hich "isibilit, section the method %ill reside and a short description of the
method
(hec) #off'unchec) the chec)box to ensure that the method %ill be implemented
(lic) the pushbutton for Parameters to na"igate to the screen to enter parameters for the
method
There %ill be one importing parameter : &03TART and one exporting internal table :
3ATER'A&0&'%T (reate entries for them as sho%n abo"e

(lic) the pushbutton : E)ceptions to ma)e entr, for Exceptions to be raised b, the method
Enter the name and description for the exception
Then$ checA and acti"ate the class
Page : 13 of 108
Examples on Object Oriented Programming in ABAP

%tep +: Brite co!e or metho! implementation
(lic) on the pushbutton : (ode> blue colored button? to implement the method
An ABAP Editor %ill open up @rite the logic for code implementation
Then$ chec) and acti"ate the code
/our 6ob o creating a global class is completeCCC
Page : .0 of 108
Examples on Object Oriented Programming in ABAP
%tep . : 2se the global class create! by you in a local program
REPORT YSUBDEL.
TYPES ! BE9IN O t(pA#at 8
#atnr LIEE #ara-#atnr 8
#a:t< LIEE #a:t-#a:t< 8
END O t(pA#at .
DATA ! ,tA#at TYPE STANDARD TABLE O t(pA#at 8
FA#at LIEE LINE O ,tA#at.
PARA.ETERS ! pA#tart LIEE #ara-#tart OBLI9ATORY.
START-O-SELECTION.
C Create "-=ect 6r"# t7e <l"-al class
DATA ! "re6 TYPE RE TO G<etA#ater,als.
CREATE OBDECT "re6.
C Call t7e #et7"$ t" <et l,st "6 #ater,al c"$e an$ na#e
CALL .ET/OD "re6->l,stA#ater,als
EHPORTIN9 lA#tart ; pA#tart
I.PORTIN9 #ater,alAl,st ; ,tA#at
EHCEPTIONS
#ater,alAn"tA6"*n$ ; 2.
,6 s(-s*-rc ne 0.
3r,te!45 +.ater,al n"t 6"*n$+.
else.
C D,spla( t7e l,st
l""p at ,tA#at ,nt" FA#at.
3r,te!45 FA#at-#atnr 8
FA#at-#a:t<.
en$l""p.
en$,6.
Output (ompile and run the program There %ill be a parameter for material t,pe in the
selection screen Enter a "alid "alue and get the list of material codes and
descriptions
Page : .1 of 108
Examples on Object Oriented Programming in ABAP
* 3etho!s
*.1 3etho! #ith one import parameter$ only one non-optional parameter
Theme This program %ill demonstrate different %a,s of calling a method %hich
has onl, one import parameter
This strateg, is also "alid for cases %here a method has more than one
import parameters$ but onl, one of them being non+optional
Program This program has a class $ C1 %ith a method : meth1 This method has
onl, one import parameter>input1? 5oo) at the method implementation
for details The main purpose of this program is to demonstrate the
different %a,s of calling a method %ith single import parameter
*ump
REPORT YSUBDEL .
CLASS C1 DEINITION.
PUBLIC SECTION.
DATA ! NU. TYPE I 1ALUE 5.
.ET/ODS ! .ET/1 I.PORTIN9 INPUT1 TYPE I .
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD ! .ET/1.
n*# ; NU. C INPUT1 .
0RITE!45 NU. .
n*# ; 5.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1.
CREATE OBDECT ! ORE1.
C D,Ierent 3a(s "6 call,n< t7e #et7"$ 3,t7 "ne ,#p"rt para#eter
CALL .ET/OD ORE1->.ET/1 EHPORTIN9 INPUT1 ; 2.
CALL .ET/OD ORE1->.ET/1% INPUT1 ; 5 '.
CALL .ET/OD ORE1->.ET/1% J '.
Output .0
.1
20
Page : .. of 108
Examples on Object Oriented Programming in ABAP
*.* 'mport parameters passe! by re. canDt be change! insi!e the metho! .
Theme Parameters can be passed to a method as import parameters in t%o
fashion:+
B, reference
B, "alue
Parameters passed b, "alue can be changed internall, in a method But$
parameters passed b, reference cannot be changed in the method
Program
description
This program contains a class C1 %ith a method 3ET51 This method
contains t%o input parameters : +
'(P2T1 : passed b, reference
'(P2T* : passed b, "alue
The method 3ET51 attempts to change '(P2T1 On compilation$ an error
is displa,ed This establishes that input parameters passe! by
reerence cannot be change! #ithin the metho!.
*ump
REPORT YSUBDEL .
DATA ! n*# TYPE I.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! .ET/1 I.PORTIN9 INPUT1 TYPE I
)al*e%,np*t2' t(pe ,.
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD ! .ET/1.
Inp*t1 ; 2.
3r,te!45 ,np*t1.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1.
CREATE OBDECT ! ORE1.
n*# ; &.
CALL .ET/OD ORE1->.ET/1 EHPORTIN9 INPUT1 ; 2
,np*t2 ; n*#.
Output On compilation$ an error message is generated
/o%$ instead of changing input1$ change the import parameter input*
>passed b, "alue?%ithin the method The program #ill get successully
compile! an! e)ecute!.
Page : .2 of 108
Examples on Object Oriented Programming in ABAP
*.+ 2se o PREFERRED PARA3ETER in a metho!
Theme !f there are more than one OPT'O(A& import parameters in a method
and no non+optional import parameters %ithout "alues$ one can t,pe in the
clause PREFERRED PARA3ETER after the list of import parameters to
specif, %hich of the optional parameters %ill get more preference
compared to others %hen the method %ill be called using s,ntax :+ (A55
;ET=O* objref+Hmeth>I"alH?
!n other %ords$ it decides %hich of the optional parameters %ill be
assigned the "alue LAA5&
Program
*escription
This program contains a class C1 containing method 3ET51 %hich has
t%o optional parameters $ '(P2T1 and '(P2T* Out of them$ parameter
'(P2T* is declared as preferred parameter The method simpl, displa,s
the "alue of t%o import parameters
/otice the last line of the program and see the output The output #ill
establish that the preerre! parameter '(P2T* gets the "alue
passe! to the metho! #hen it is calle! using the synta):-
CA&& 3ET5OD ob6re-Emeth7F"alE8.
*ump
REPORT YSUBDEL .
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! .ET/1 I.PORTIN9 INPUT1 TYPE I "pt,"nal
,np*t2 TYPE I OPTIONAL
PREERRED PARA.ETER INPUT2.
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD ! .ET/1.
3r,te!45 ,np*t1 8
45 ,np*t2 .
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1.
CREATE OBDECT ! ORE1.
CALL .ET/OD ! ORE1->.ET/1% ,np*t1 ; 5 ,np*t2 ; & '.
s:,p 2.
3r,te!45 +NeFt call+.
call #et7"$ "re61->#et71% 10 ' .
Output 1
2


/ext call
0
10
Page : .4 of 108
Examples on Object Oriented Programming in ABAP
*.. 2se o EGPORT an! C5A(?'(? parameters o a metho!
Theme This program %ill demonstrate the use of E0PO<T!/: and (=A/:!/:
parameters of a method
Program
description
The program contains a method TAG0CA&C belonging to the class CTAG !t
recei"es ?RADE as '3PORT'(? parameter and %A&AR/ as C5A(?'(?
parameter Based on the grade$ the EGPORT'(? parameter 'TAG is
calculated and the C5A(?'(? parameter $ %A&AR/ is modified b,
deducting tax from it
REPORT YSUBDEL .
DATA ! 3AtaF t(pe p $ec,#als 2 8
3Asalar( t(pe p $ec,#als 2 .
CLASS CTAH DEINITION.
PUBLIC SECTION.
.ET/ODS ! TAHACALC I.PORTIN9 <ra$e TYPE C
EHPORTIN9 ,taF TYPE P
C/AN9IN9 salar( TYPE P .
ENDCLASS.
CLASS CTAH I.PLE.ENTATION.
.ET/OD ! TAHACALC.
CASE <ra$e.
0/EN +A01+.
,taF ; salar( C +0.2+.
0/EN +A02+.
,taF ; salar( C +0.1+.
0/EN OT/ERS.
,taF ; salar( C +0.15+.
ENDCASE.
salar( ; salar( - ,taF.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO CTAH.
CREATE OBDECT ! ORE1.
3Asalar( ; &0000.
3AtaF ; 0 .
3r,te!45 +Be6"re #et7"$ call8 salar( an$ taF are+ 8
3Asalar( 8
3AtaF .
CALL .ET/OD ORE1->TAHACALC EHPORTIN9 <ra$e ; +A01+
I.PORTIN9 ,taF ; 3AtaF
C/AN9IN9 salar( ; 3Asalar(.
3r,te!45 +A6ter #et7"$ call8 salar( an$ taF are+ 8
3Asalar( 8
3AtaF .
Output Before method call$ salar, and tax are 20$00000 000
After method call$ salar, and tax are .4$00000 6$00000
Page : .1 of 108
Examples on Object Oriented Programming in ABAP
*.4 3etho! using 'nternal Table as one o the parameters
Theme This program demonstrates ho% an internal table can be used as one of the
interface parameters of a method
Program
*escription
The program contains a method : ?ET3ARA in class : ?ET03ATER'A&% !t
accepts material group$ 3AT?R as import parameter and details out the
details of the materials belonging to that material group into '0TA, $ %hich is
an internal table used as E0PO<T!/: parameter for the method
<EPO<T B-8BOOP-1
t,pes : begin of t,pGtab $
matnr li)e mara+matnr $
meins li)e mara+meins $
end of t,pGtab
data : itab t,pe standard table of t,pGtab $
xGtab 5!NE 5!/E O9 !TAB
(5A-- getGmaterials *E9!/!T!O/
P8B5!( -E(T!O/
;ET=O*- : getmara !;PO<T!/: matgr TBPE (
E0PO<T!/: lGtab TBPE A/B TAB5E
endclass
(5A-- getGmaterials !;P5E;E/TAT!O/
;ET=O* : getmara
-E5E(T matnr meins !/TO TAB5E lGtab
9<O; ;A<A
@=E<E ;ATN5 K matgr
E/*;ET=O*
E/*(5A--
PA<A;ETE<- : pGmat)l li)e mara+mat)l
-TA<T+O9+-E5E(T!O/
*ATA : %Gmat TBPE <E9 TO getGmaterials
(<EATE OBOE(T : %Gmat
(A55 ;ET=O* %Gmat+Hgetmara E0PO<T!/: matgr K pGmat)l
!;PO<T!/: lGtab K itab
5OOP AT !TAB !/TO 0GTAB
@<!TE:'1 0GTAB+;AT/< $ 0GTAB+;E!/-
E/*5OOP
Output One'more than one records %ith material number and basic unit$ depending on
the material group entered in the selection+screen
Page : .6 of 108
Examples on Object Oriented Programming in ABAP
*.9 2se o RET2R('(? parameters in metho!
Theme To get some "alues from a method $ one can use the E0PO<T!/:$ (=A/:!/:
or <ET8</!/: parameters
!f one uses <ET8</!/: parameters$ the follo%ing restrictions appl,:+
>1? /o E0PO<T!/:'(=A/:!/: parameters can be used for the method
>.? Onl, one <ET8</!/: parameter can be used
>2? <ET8</!/: parameters are onl, passed b, "alue
This program demonstrates the use of <ET8</!/: parameters and the
"arious %a,s to call a method %ith <ET8</!/: parameter to get the "alue
into some "ariable
Program
*escription
;ethod 31 in class C1 ha"e t%o input parameters>'(P2T1 and '(P2T*?$
%hich are used to deri"e "alue for <ET8</!/: parameter $ RE%2&T
The program demonstrates "arious s,ntaxes that can be used to call a method
of this )ind
rep"rt (s*-$el1 #essa<e-,$ 00.
$ata ! 3An*# t(pe ,.
class c1 $eKn,t,"n .
p*-l,c sect,"n.
#et7"$s ! #1 ,#p"rt,n< ,np*t1 t(pe ,
,np*t2 t(pe ,
ret*rn,n< )al*e%res*lt' t(pe , .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! #1.
res*lt ; ,np*t1 C 2 B ,np*t2.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "-=1 t(pe re6 t" c1 .
create "-=ect "-=1.
C S(ntaF 1
call #et7"$ "-=1->#1 EHPORTIN9 ,np*t1 ; 5
,np*t2 ; 2
RECEI1IN9 res*lt ; 3An*#.
3r,te!45 3An*# .
C S(ntaF 2
3An*# ; "-=1->#1% ,np*t1 ; 10 ,np*t2 ; 20 '.
3r,te!45 3An*# .
C S(ntaF &
#")e "-=1->#1% ,np*t1 ; 2 ,np*t2 ; & ' t" 3An*# .
3r,te!45 3An*# .
Output 14
40
7
Page : .7 of 108
Examples on Object Oriented Programming in ABAP
*.: Demo on %tatic 3etho!
Theme This program %ill sho% ho% to declare and define a static method and ho% it
can be called using class component selector
!n the follo%ing program$ method : TE-T;ET=O* is defined as static method
and is called later using class component selector$ LKH&
*ump <EPO<T B-8BOOP-13
data : num t,pe i
class testclass definition
public section
class+methods : testmethod
endclass
class testclass implementation
method : testmethod
num K 1
%rite:'1 num
endmethod
endclass
start+of+selection
call method testclassKHtestmethod
Output 1
Page : .8 of 108
Examples on Object Oriented Programming in ABAP
*.< %tatic metho!s can only use static attributes1 instance metho!s use both
Theme -tatic methods of a class can onl, use static attributes of that class !t
cannot use instance attributes But$ instance methods can use both
Program
*escription
The follo%ing program contains a class C1 %hich contains the follo%ing:+
Component Type %tatic$'nstance
stnum *ata static
!nstnum *ata !nstance
-tmeth ;ethod -tatic
!nstmeth ;ethod !nstance
Both the static and instance methods are attempting to displa, "alues of the
static and instance attributes: %T(23 and '(%T(23
On compilation$ an error %ill be generated %hich %ill demonstrate that static
method %T3ET5 cannot %or) %ith instance attribute$ '(%T(23
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION.
PUBLIC SECTION.
CLASS-DATA ! STNU. TYPE I 1ALUE 5.
DATA ! INSTNU. TYPE I 1ALUE J .
CLASS-.ET/ODS ! ST.ET/ .
.ET/ODS ! INST.ET/ .
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD ! ST.ET/ .
0RITE!45 STNU. .
WRITE:/5 INSTNUM .
END.ET/OD.

.ET/OD INST.ET/.
0RITE!45 STNU. .
0RITE!45 INSTNU. .
END.ET/OD.
ENDCLASS.

START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1.
CALL .ET/OD c1;>st#et7 .
CREATE OBDECT ORE1.
CALL .ET/OD "re61->,nst#et7.
Output On compilation$,ou get the follo%ing error:+
<emo"e the line in bold in the program and compile !t %ill get successfull,
compiled and executed
Page : .3 of 108
Examples on Object Oriented Programming in ABAP
*.= 3etho! Raising E)ceptions
Theme ;ethods can raise exceptions li)e function modules %hich can be handled after
calling the method$ depending on "arious s,+subrc "alues This program %ill
demonstrate that
Program
descriptio
n
The program pro"ides the user a selection+screen %here the user enters a
numeric "alue !f the user entr, is I1$ he gets an information message L-hould
be HK1& Else$ fi"e times of the "alue entered is displa,ed b, the program on
execution
The class C1 in this program contains method 31 %hich imports "alue for
(231 $ and returns fi"e times of it through the export parameter$ (23*
=o%e"er$ if the "alue passed to (231 is lesser than 1$ it raises an exception
E1 %ith some error message
The method 31 is called after creating an object from the class 8ser+entered
"alue in the parameter field : P0(O is passed to importing parameter (231
*ump
rep"rt (s*-$el1 #essa<e-,$ 00.
class c1 $eKn,t,"n .
p*-l,c sect,"n.
#et7"$s ! #1 ,#p"rt,n< n*#1 t(pe ,
eFp"rt,n< n*#2 t(pe ,
eFcept,"ns e1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! #1.
,6 n*#1 lt 5 .
#essa<e ,&LM%00' 3,t7 +S7"*l$ -e >;5+ ra,s,n< e1.
else .
n*#2 ; n*#1 C 5 .
en$,6.
en$#et7"$.
en$class.
para#eters ! pAn" t(pe , .
start-"6-select,"n.
$ata ! "-=1 t(pe re6 t" c1 .
create "-=ect "-=1.
call #et7"$ "-=1->#1 eFp"rt,n< n*#1 ; pAn"
,#p"rt,n< n*#2 ; pAn"
eFcept,"ns e1 ; 1.
I s(-s*-rc N> 0.
.ESSA9E ID SY-.S9ID TYPE SY-.S9TY NU.BER SY-.S9NO
0IT/ SY-.S911 SY-.S912 SY-.S91& SY-.S912.
ELSE.
3r,te!45 pAn" .
ENDI.
Output The program pro"ides the user a selection+screen %here the user enters a
numeric "alue !f the user entr, is I1$ he gets an information message L-hould
be HK1& Else$ fi"e times of the "alue entered is displa,ed b, the program on
execution
Page : 20 of 108
Examples on Object Oriented Programming in ABAP
*.1> 3etho! can call itsel
Theme ;ethod of a class can call itself But$ please do not forget to specif, some exit
point to the method Else$ it %ill create an infinite loop
Program
*escr
The program follo%ing contains a method 31 in a class C1 !t increases the
"alue of static "ariable$ %TAT(23 b, 10 and displa,s itThen$ if the "alue of
-TAT/8; is IK100 $ it calls itself again
*ump
rep"rt (s*-$el1 #essa<e-,$ 00.
class c1 $eKn,t,"n .
p*-l,c sect,"n.
class-$ata ! statn*# t(pe , .
#et7"$s ! #1 .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! #1.
statn*# ; statn*# B 10.
,6 statn*# <t 100.
eF,t.
en$,6.
3r,te!45 statn*# .
call #et7"$ #1.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "-=1 t(pe re6 t" c1 .
create "-=ect "-=1.
call #et7"$ "-=1->#1 .
Output 10
.0
C
100
Page : 21 of 108
Examples on Object Oriented Programming in ABAP
*.11 2se o 3E in metho!s
Theme A method can ha"e a "ariable defined %ithin it ha"ing the same name as one
of the attributes of the class to %hich the method belongs to
To clearl, identif, the class le"el attribute$ the selector ;E is used
Program
*escription
(lass TE%TC&A%- contains method TE%T3ET5OD There is a "ariable
'0(23 declared as public attribute in the class as %ell as in the
implementation part of the method
To access the "ariable '0(23 at the class le"el %ithin the method$ the
selector ;E is used Please see the ouputs of this program for better
understanding
*ump
REPORT YSUBOOPS1O .
class testclass $eKn,t,"n.
p*-l,c sect,"n.
$ata ! ,An*# t(pe , )al*e 5.
#et7"$s ! test#et7"$ .
en$class.
class testclass ,#ple#entat,"n.
#et7"$ !test#et7"$.
$ata ! ,An*# t(pe , )al*e 2.
3r,te!45 #e->,An*# 8 P access )ar,a-le "6 t7e class
45 ,An*# . P access )ar,a-le "6 t7e #et7"$
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! ,An*# t(pe ,.
$ata ! #(A"-= t(pe re6 t" testclass.
create "-=ect ! #(A"-=.
call #et7"$ #(A"-=->test#et7"$.
Output 1
.
Page : 2. of 108
Examples on Object Oriented Programming in ABAP
*.1* Pointer Tables
Theme This program %ill demonstrate the use of pointer tables
Program
*escription
The program belo% uses pointer table : 3/O,-0TA,
REPORT YSUBOOPS1L .
class testclass $eKn,t,"n.
p*-l,c sect,"n.
#et7"$s ! test#et7"$ .
class-$ata ! n*# t(pe ,.
en$class.
class testclass ,#ple#entat,"n.
#et7"$ ! test#et7"$.
n*# ; n*# B 5.
3r,te!45 n*#.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! #("-= t(pe re6 t" testclass 8
#("-=Ata- t(pe ta-le "6 re6 t" testclass.
$" 5 t,#es.
create "-=ect #("-= .
appen$ #("-= t" #("-=Ata-.
en$$".
l""p at #("-=Ata- ,nt" #("-=.
call #et7"$ ! #("-=->test#et7"$.
en$l""p.
Output 1
10
11
.0
.1
Page : 22 of 108
Examples on Object Oriented Programming in ABAP
*.1+ Dynamic 3etho! Calls
Theme One can call methods d,namicall, 9ollo%ing restrictions appl,:+
/ame of the method can be d,namic for static'instance method
/ame of the class %hile calling static method can be d,namic
Both the name of the static method and the class containing it can be
d,namic
@hile doing so$ it is better to use uppercase to assign the name of the
class'methods to the "ariables> %hich %ill be used for d,namic assignment?
Program
*escription
The follo%ing program contains class C1 %ith static method>%TAT3? and
d,namic method>'(%T3? The program utiliEes all the s,ntaxes to call these
static'instance methods d,namicall,
*ump
REPORT YSUBOOPS1L .
$ata ! 6%J' t(pe c 8
<%10' t(pe c .
class c1 $eKn,t,"n.
p*-l,c sect,"n.
class-#et7"$s ! stat# .
#et7"$s ! ,nst# .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! stat# .
3r,te!45 +I a# stat,c #et7"$+.
en$#et7"$.
#et7"$ ! ,nst#.
3r,te!45 +I a# ,nstant #et7"$+.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
C Na#e "6 ,nstance #et7"$ can -e $(na#,c
6 ; +INST.+. call #et7"$ "re6->%6'.
C Na#e "6 stat,c #et7"$ can -e $(na#,c
6 ; +STAT.+. call #et7"$ "re6->%6'.
C Na#e "6 t7e class can -e $(na#,c 6"r stat,c #et7"$ call
6 ; +C1+. call #et7"$ %6';>stat#.
C Na#e "6 t7e #et7"$ can -e $(na#,c 6"r stat,c #et7"$ call
6 ; +STAT.+. call #et7"$ c1;>%6'.
C B"t7 can -e $(na#,c 6"r stat,c #et7"$ call
< ; +C1+. call #et7"$ %<';>%6'.
Output ! am instant method
! am static method
! am static method
! am static method
! am static method
Page : 24 of 108
Examples on Object Oriented Programming in ABAP
*.1. 2se o parameter table
Theme 9or d,namic method call$ one can use the concept of PA<A;ETE< TAB5E to
include references of all interface parameters of the method>instead of
mentioning them separatel,?
Prog
*escr
The program contains class C1 %ith a method 31 This method has one
!;PO<T!/: parameter $ HP1D and one E0PO<T!/: parameter LP+&
P+ is calculated %ithin the method
=ere$ parameter table PTA, is used to d,namicall, call the method 31
Please note that$ parameter tables %or) onl, %hen a method is called
d,namicall,>ie to da,$ the name of the method'class is determined at runtime?
*ump
REPORT YSUBOOPS22 .
DATA ! ,Ares*lt t(pe ,8
,An*# t(pe , )al*e 5 .
DATA 6%2' TYPE c 1ALUE +.1+.
CLASS clAa-apA"-=ect$escr DEINITION LOAD .
DEINE ! p"pta-le.
pta-Al,ne-na#e ; Q1.
pta-Al,ne-:,n$ ; CLAABAPAOBDECTDESCR;>Q2.
9ET REERENCE O Q& INTO pta-Al,ne-)al*e.
INSERT pta-Al,ne INTO TABLE pta-.
I s(-s*-rc ne 0.
EHIT.
ENDI.
END-O-DEINITION.
CLASS c1 DEINITION.
PUBLIC SECTION.
.ET/ODS #1 I.PORTIN9 p1 TYPE ,
eFp"rt,n< p& t(pe , .
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD #1.
p& ; p1 B 200.
END.ET/OD.
ENDCLASS.
DATA r TYPE RE TO c1.
DATA! pta- TYPE a-apApar#-,n$Ata-8
pta-Al,ne LIEE LINE O pta-.
START-O-SELECTION.
p"pta-le ! +P1+ EHPORTIN9 ,An*# 8
+P&+ I.PORTIN9 ,Ares*lt .
CREATE OBDECT r TYPE c1.
CALL .ET/OD r->%6' PARA.ETER-TABLE pta-.
3r,te!45 ,Ares*lt .
Output .01
Page : 21 of 108
Examples on Object Oriented Programming in ABAP
*.14 2se o E)ception Table
Theme !nstead of dealing %ith each and e"er, exception and assigning it to different
s,+subrc "alues$ one can use exception table to handle the exceptions %hen a
method is called d,namicall,
The class C1 contains method 31 %hich raises an exception Exception table
ETA, is used to handle the exception
<EPO<T B-8BOOP-.1
(5A-- clGabapGobjectdescr *E9!/!T!O/ 5OA*
(5A-- c1 *E9!/!T!O/
P8B5!( -E(T!O/
;ET=O*- m1 E0(EPT!O/- exc
E/*(5A--
(5A-- c1 !;P5E;E/TAT!O/
;ET=O* m1
<A!-E exc
E/*;ET=O*
E/*(5A--
*ATA r TBPE <E9 TO object
*ATA f>2? TBPE c AA58E D;1D
*ATA: etab TBPE abapGexcpbindGtab$
etabGline 5!NE 5!/E O9 etab
-TA<T+O9+-E5E(T!O/
etabGline+name K DE0(D
etabGline+"alue K 4
!/-E<T etabGline !/TO TAB5E etab
!9 s,+subrc ne 0
E0!T
E/*!9
(<EATE OBOE(T r TBPE c1
(A55 ;ET=O* r+H>f? E0(EPT!O/+TAB5E etab
@<!TE s,+subrc
Output 4
Page : 26 of 108
Examples on Object Oriented Programming in ABAP
+ Constructors
+.1 'nstance Constructors get ire! at the time o class instantiation
Theme This simple program %ill sho% ,ou that instance constructor methods of
a class get triggered %hen an object is created from the class
Program
*escription
This program contains a class C1 %ith a constructor method %hich %rites
out something to indicate that it is triggered !n the -TA<T+O9+
-E5E(T!O/ bloc)$ the class C1 is instantiated$ %hich triggers the
instance constructor method> as is e"ident b, the output as report?
This establishes the theme.
*ump
REPORT YSUBOOPS1.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! CONSTRUCTOR .
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD c"nstr*ct"r.
0RITE!45 +I a# c"nstr*ct"r+.
s:,p 2.
END.ET/OD.
ENDCLASS.
CCCCCCCCCCCCCCC #a,n pr"<ra# CCCCCCCCCCCCCC
START-O-SELECTION.
DATA! "-=1 TYPE RE TO c1.
CREATE OBDECT! "-=1.
Output ! am constructor
Page : 27 of 108
Examples on Object Oriented Programming in ABAP
+.* 'nstance Constructors can ha"e import parameters
Theme !nstance constructors can ha"e import parameters Aalues to them are
passed at the time of (<EATE OBOE(T statement to create object from the
class containing the constructor
Program
*escription
The program contains a class C1 %hich has one instance constructor %ith one
import parameter The constructor gets fired at the time of (<EATE
OBOE(T statement
*ump
REPORT YSUBOOPS2.
CLASS c1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! CONSTRUCTOR ,#p"rt,n< t"$a( t(pe $.
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD c"nstr*ct"r.
0r,te!45 +T"$a( ,s ! + 8 t"$a( $$4##4((((.
END.ET/OD.
ENDCLASS.
CCCCCCCCCCCCCCC #a,n pr"<ra# CCCCCCCCCCCCCC
START-O-SELECTION.
DATA! "-=1 TYPE RE TO c1.
CREATE OBDECT! "-=1 eFp"rt,n< t"$a( ; s(-$at*#.
Output Toda, is 0804.004
Page : 28 of 108
Examples on Object Oriented Programming in ABAP
+.+ Constructors cannot ha"e any e)port parameters
Theme This program %ill demonstrate that constructor methods cannot ha"e an,
export parameters
Program
*escription
This program attempts to create a constructor %ith export parameter$ %hich
is trapped and resisted at the time of compilation
This establishes the theme.
*ump
REPORT YSUBOOPS2.
CLASS c1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! CONSTRUCTOR exporting na#e t(pe c.
ENDCLASS.

Output (ompilation errror is reported
Page : 23 of 108
Examples on Object Oriented Programming in ABAP
+.. 'nstance Constructors can raise e)ceptions
Theme !nstance (onstructor methods can raise exceptions
Program
*escriptions
This program contains a class C1 %hich contains an instance constructor
method !t accepts an import parameter$ (23 !f it is lesser than 7$ an
exception is raised$ %hich is properl, handled b, specif,ing some s,+subrc
"alue at the time of (<EATE OBOE(T statement and later handled properl,
*ump
REPORT YSUBOOPS2.
CLASS c1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! CONSTRUCTOR ,#p"rt,n< n*# t(pe ,
exceptions e1 .
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD c"nstr*ct"r.
,6 n*# lt O.
raise e1.
en$,6.
END.ET/OD.
ENDCLASS.
CCCCCCCCCCCCCCC #a,n pr"<ra# CCCCCCCCCCCCCC
START-O-SELECTION.
DATA! "-=1 TYPE RE TO c1.
CREATE OBDECT! "-=1 eFp"rt,n< n*# ; 5
exceptions e1 ; 2.
,6 s(-s*-rc ; 2.
3r,te!45 +EFcept,"ns ra,se$+.
en$,6.
Output Exceptions <aised
Page : 40 of 108
Examples on Object Oriented Programming in ABAP
+.4 2se o static constructor.
Theme There are t%o programs o"er here %hich %ill sho% ,ou simple example on a static
constructor Bou %ill learn ho% to declare a static constructor -tatic'class
constructors get triggered before an, of the follo%ing e"ents:+
:enerating an instance of a class using (<EATE OBOE(T obj$ %here obj
has the data t,pe <E9 TO class
(alling a static method using P(A55 ;ET=O*Q classKHmeth
<egistering a static e"ent handler method using -ET =A/*5E<
classKHmeth for obj
<egistering an e"ent handler method for a static e"ent of the class class
Addressing a static attribute %ith classKHa
These t%o programs %ill sho% ,ou that a class constructor gets fired before an,
of its static components are accessed$ or an object is created from the class
*ump
Constructor is triggere! #hen a static Constructor is ire! #hen an ob6ect is
attribute is accesse! create! rom the class
Output 9or the first program>on the 5=-?:+
! am class constructor
1
9or the second program>on the <=-?:+
! am class constructor
Page : 41 of 108
REPORT YSUBOOPS2.
CLASS c1 DEINITION .
PUBLIC SECTION.
CLASS-DATA ! NU. TYPE I 1ALUE 5.
CLASS-.ET/ODS !CLASSACONSTRUCTOR.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD CLASSACONSTRUCTOR.
0RITE!45 +I a# class c"nstr*ct"r+.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
0RITE!45 C1=>NUM.
REPORT YSUBOOPS2.
CLASS c1 DEINITION .
PUBLIC SECTION.
CLASS-DATA ! NU. TYPE I 1ALUE 5.
CLASS-.ET/ODS !CLASSACONSTRUCTOR.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD CLASSACONSTRUCTOR.
0RITE!45 +I a# class c"nstr*ct"r+.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE TYPE RE TO C1.
CREATE !"ECT ORE.
Examples on Object Oriented Programming in ABAP
+.9 %tatic constructor can be triggere! at the beginning o a processing
blocA7orm $e"ent$blocA$proce!ure8
Theme !n the -TA<T+O9+-E5E(T!O/ bloc) of this program$ static attribute of a
class containing class constructor %ill be accessed This %ill demonstrate that
the the first thing %hich %ill get executed in the -TA<T+O9+-E5E(T!O/
bloc) is the class constructor method$ irrespecti"e of the point at %hich the
static attribute is accessed
Program
*escription
The program contains a class (1 %ith a static constructor$ %hich prints the
statement:+ R ! am class constructorS
This class also contains a static attribute $ num of "alue K 1
The -TA<T+O9+-E5E(T!O/ bloc) in this program contains the follo%ing
statements:+
A %rite statement %hich %ill print :+R=elloS
A call to access the static attribute>/8;? of class (1
On execution of this program$ %e %ill get the follo%ing output in the list:+
! am class constructor
=ello
1
instead of the expected output:+
=ello
! am class constructor
1
This !emonstrates the theme.
*ump
REPORT YSUBOOPS2.
CLASS c1 DEINITION .
PUBLIC SECTION.
CLASS-DATA ! NU. TYPE I 1ALUE 5.
CLASS-.ET/ODS !CLASSACONSTRUCTOR.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD CLASSACONSTRUCTOR.
0RITE!45 +I a# class c"nstr*ct"r+.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
3r,te!45 +/ell"+.
3r,te!45 c1;>n*#.
Output ! am class constructor
=ello
1
Page : 4. of 108
Examples on Object Oriented Programming in ABAP
+.: %tatic$Class constructors cannot ha"e any interace
Theme This program %ill sho% ,ou that static constructors of a class cannot ha"e
an, interface parameters and exceptions
Program
*escription
!n this program$ the class C1 contains a class constructor %hich is ha"ing an
import parameter $ (23 The program could not be successfull, compiled
due to such attempt
*ump
REPORT YSUBOOPS2.
CLASS c1 DEINITION .
PUBLIC SECTION.
CLASS-.ET/ODS !CLASSACONSTRUCTOR IM#RTIN$ NU. TYPE C.
ENDCLASS.
Output (ompilation of the progran %ill fail %ith an error message:+
IThe metho! C&A%%0CO(%TR2CTOR may not ha"e parameters or
EGCEPT'O(%J.
Page : 42 of 108
Examples on Object Oriented Programming in ABAP
. 'nheritance
..1 %ubclass can access public$protecte! components o superclass
Theme This program %ill demonstrate:+
=o% to create a subclass from a superclass
-ubclass can access public'protected components> methods$ attributes
etc? of superclass
Prog
*escr
This program contains superclass C1 and its subclass C* (lass C1 has the follo%ing
components:+
Component (ature %ection o
E)istence
%igniicance$action
/8; Attribute public Aalue K 6
;ET=1 ;ethod Public *ispla,s "alue of /8;
;ET=. ;ethod Protected *ispla,s:+ R! am meth.S
/8;. Attribute Protected Aalue K 7
-ubclass of C1 is C* %hich contains the follo%ing ne% components:+
Component (ature %ection o
E)istence
%igniicance$action
;1 ;ethod public (alls method meth1
(alls method meth.
*ispla,s "alue of "ariable num.
!n the -TA<T+O9+-E5E(T!O/ bloc)$ an object OREF is created from C* and the method
31 is calledThe output of the program sho%s that method 31 of class C* calls method
3ET51$ then 3ET5* and finall, displa,s "ariable (23*
This !emonstrates that subclass C* has access to the public an! protecte! metho!s
an! attributes o superclass C1 an! truly establishes the theme.
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! .ET/1.
DATA ! NU. TYPE I 1ALUE J.
PROTECTED SECTION.
DATA ! n*#2 t(pe , )al*e O.
.ET/ODS .ET/2.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
.ET/OD ! .ET/1.
0RITE!45 n*#.
en$#et7"$.
.ET/OD ! .ET/2.
0RITE!45 + I a# #et72 +.
END.ET/OD.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
PUBLIC SECTION.
.ET/ODS ! .1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
.ET/OD .1.
CALL .ET/OD ! #et718 #et72.
3r,te!45 n*#2.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE TYPE RE TO C2.
CREATE OBDECT ORE.
Page : 44 of 108
Examples on Object Oriented Programming in ABAP
CALL .ET/OD ! ORE->.1.
Output 6
! am meth.
7
Page : 41 of 108
Examples on Object Oriented Programming in ABAP
..* %ubclass can re-implement inherite! metho!s rom superclass
Theme -ubclass can re+implement the inherited public and protected methods from
superclass
Program
*escr
(lass C1 contains method 3ET51>public? and 3ET5*>protected?$ both of %hich
are modified and re+implemented in its subclass C*Objects are created out of
both classes and the method 3ET51 for both objects are called
Output of the program demonstrates different beha"iour for method 3ET51 of
class C1 and C*
This !emonstrates the theme.
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! .ET/1.
PROTECTED SECTION.
.ET/ODS .ET/2.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
.ET/OD ! .ET/1.
0RITE!45 +I a# #et71 ,n class C1+.
CALL .ET/OD .ET/2.
END.ET/OD.
.ET/OD ! .ET/2.
0RITE!45 + I a# #et72 ,n class C1 +.
END.ET/OD.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
PUBLIC SECTION.
.ET/ODS ! .ET/1 re$eKn,t,"n .
PROTECTED SECTION.
.ET/ODS ! .ET/2 re$eKn,t,"n.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
.ET/OD .ET/1.
0RITE!45 +I a# #et71 ,n class C2+.
call #et7"$ #et72.
en$#et7"$.
.ET/OD ! .ET/2.
0RITE!45 + I a# #et72 ,n class C2 +.
END.ET/OD.
en$class.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1 8
ORE2 TYPE RE TO C2.
CREATE OBDECT ! ORE1 8 ORE2.
CALL .ET/OD ! ORE1->.ET/1 8
ORE2->.ET/1.
Output ! am meth1 in class (1
! am meth. in class (1
! am meth1 in class (.
! am meth. in class (.
Page : 46 of 108
Examples on Object Oriented Programming in ABAP
..+ Ob6ects cannot be create! rom an abstract class.
Theme Objects cannot be created from an abstract class Onl, the subclasses of such
class can be instantiated
Program
*escriptio
n
This program contains an abstract class C1 and its subclass C* Object cannot
be created from class C1$ but possible from class C*
*ump
!nstantiation of abstract class !nstantiation of subclass of an abstract class
Output !nstantiation of abstract class %ill be resisted %ith error message at the time
of compilation
!nstantiation of subclass of an abstract class %ill be allo%ed
Page : 47 of 108
REPORT YSUBDEL.
CLASS C1 DEINITION ABSTRACT.
PUBLIC SECTION.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
.ET/OD ! .ET/1.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9
RO. C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
en$class.
START-O-SELECTION.
$ata ! ORE1 TYPE RE TO C1 8
ORE2 TYPE RE TO C2.
CREATE !"ECT ore%1.

REPORT YSUBDEL.
CLASS C1 DEINITION ABSTRACT.
PUBLIC SECTION.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
.ET/OD ! .ET/1.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9
RO. C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
en$class.
START-O-SELECTION.
$ata ! ORE1 TYPE RE TO C1 8
ORE2 TYPE RE TO C2.
CREATE OBDECT "re62.

Examples on Object Oriented Programming in ABAP
... Abstract metho!s cannot be implemente! in abstract class
Theme Abstract methods cannot be implemented in that class !t has to be implemented
in one of its subclass To implement an abstract method in a subclass$ one need
to redefine this subclass using the REDEF'('T'O( addition
Program
*escr
This program contains an abstract class C1 %ith abstract method 3ET51$ %hich
is implemented in the same classThe program %ill not be compiled due to this
The program is then modified and the abstract method is implemented in class
C*$ subclass of (1 /o%$ the program gets successfull, compiled
*ump
Page : 48 of 108
REPORT YSUBDEL.
CLASS C1 DEINITION ABSTRACT.
PUBLIC SECTION.
.ET/ODS ! .ET/1 ABSTRACT.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
MET&' : MET&1.
WRITE:/5 (I a) )et*o+ : MET&1 (.
EN'MET&'.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
en$class.
START-O-SELECTION.
$ata ! ORE2 TYPE RE TO C2.
CREATE OBDECT "re62.
REPORT YSUBDEL.
CLASS C1 DEINITION ABSTRACT.
PUBLIC SECTION.
.ET/ODS ! .ET/1 ABSTRACT.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9
RO. C1.
p*-l,c sect,"n.
#et7"$s ! #et71 re$eKn,t,"n.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
MET&' : MET&1.
WRITE:/5 (I a) )et*o+: MET&1 (.
EN'MET&'.
en$class.
START-O-SELECTION.
$ata ! ORE2 TYPE RE TO C2.
CREATE OBDECT "re62.
'ncorrect Correct
Examples on Object Oriented Programming in ABAP
..4 Final classes cannot ha"e any subclass
Theme -ubclasses cannot be inherited from a final (lass The, can onl, be instantiated
Program
*escrip
This program contains a final class C1 and a subclass C* This is not allo%ed and is
resisted at the time of compilation
5ence1 the theme is properly establishe!
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION INAL.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
ENDCLASS.
CLASS C2 DEINITION IN&ERITIN$ ,RM C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
en$class.
START-O-SELECTION.
$ata ! ORE2 TYPE RE TO C2.
CREATE OBDECT "re62.
Output (ompilation error is generated:+
The inal class C1 cannot ha"e any subclasses.
Page : 43 of 108
Examples on Object Oriented Programming in ABAP
..9 Final metho!s cannot be re!eine! in the subclasses
Theme 9inal method in a class can onl, be defined in that class !t cannot be redefined in
an, of its subclasses
Program
*escr
This program contains a class C1 %hich has a final method : 3ET51
(lass C* is subclass of C1 and tries to re+define the final method
(ompilation error is genarated $ resisting successful compilation
This !emonstrates the theme.
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION .
PUBLIC SECTION.
.ET/ODS ! .ET/1 INAL.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
#et7"$ #et71.
3r,te!45 +I a# #et7"$ #et71+.
en$#et7"$.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
PUBLIC SECTION.
#et7"$s ! #et71 re+e-nition.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
)et*o+ : )et*1.
.rite:/5 ( I a) )et*1/)o+i-e+ in c0ass C1(.
En+)et*o+.
en$class.
START-O-SELECTION.
$ata ! ORE2 TYPE RE TO C2.
CREATE OBDECT "re62.
Output Program is not successfull, compiled The compilation error message is as
follo%s:+
The inal metho! 3ET51 cannot be re!eine!.
Page : 10 of 108
Examples on Object Oriented Programming in ABAP
..: %tatic attributes e)ist only once per inheritance tree
Theme -tatic attributes onl, exist once in each inheritance tree One can change them
from outside the class using the class component selector %ith an, class name$ or
%ithin an, class in %hich the, are shared The, are "isible in all classes in the
inheritance tree
Program
*escr
(lass C1 contains static attribute $ (23
(lass C* and C+ are subclasses of class C1
!n the -TA<T+O9+-E5E(T!O/ bloc)$ the static attribute $ (23 is changed
using reference of class C+
!t gets changed %ith reference to class C* also
=ence$ static attribute$ /8;$ changed "ia class (2 is also changed %ith respect
to (. also This !emonstrates the theme.
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION .
PUBLIC SECTION.
class-$ata ! n*# t(pe ,.
ENDCLASS.
CLASS C1 I.PLE.ENTATION .
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
en$class.
CLASS C& DEINITION IN/ERITIN9 RO. C1.
ENDCLASS.
START-O-SELECTION.
C&;>NU. ; 10.
0RITE!45 C2;>NU..
Output 10
Page : 11 of 108
Examples on Object Oriented Programming in ABAP
..< Constructors o superclass lo#s !o#n the chain
Theme (onstructor of superclass is inherited b, the subclass also
Program
*escr
(lass C1 contains a constructor method$ but its subclass C* does not explicitl,
ha"e its o%n
@hen object from class C* is created$ the constructor of class C1 is triggered
This establishes the theme.
*ump
REPORT YSUBOOPS1M.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS ! CONSTRUCTOR .
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD c"nstr*ct"r.
0RITE!45 (I a) C1(.
s:,p.
END.ET/OD.
ENDCLASS.
CLASS C2 DEINITION IN/ERITIN9 RO. C1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
ENDCLASS.
START-O-SELECTION.
DATA! "-= t(pe re6 t" C2.
CREATE OBDECT! "-=.
Output ! am (1
Page : 1. of 108
Examples on Object Oriented Programming in ABAP
..= %ubclass can ha"e enhance! constructor than its superclass.
Theme A subclass can modif, the constructor method and add some extra
functionalities !n the instance constructor method of the child class$ the one
for the superclass should be called first using :
CA&& 3ET5OD super-ECO(%TR2CTOR statement and then additional
statements can be added
Pl note that <E*E9!/!T!O/ statement is not reTuired to enhance
constructors for a subclass
Program
*escrip
This program contains three classes:+
?RA(DFAT5ER at the top of the node %ith its o%n constructor method
FAT5ER$ subclass of :<A/*9AT=E< %ith enhanced constructor
%O($ subclass of 9AT=E< %ith its o%n enhanced constructor
An object is created finall, from the class %O($ %hich triggers the constructor
methods in the order: :<A/*9AT=E<9AT=E<-O/
REPORT YSUBOOPS18.
CLASS grandfather DEFINITION.
PUBLIC SECTION.
METHODS CONSTRUCTOR .
ENDCLASS.
CLASS grandfather IMPLEMENTATION.
METHOD !"n#tr$!t"r.
%RITE&' (I a) grandfather(.
#*+,.
ENDMETHOD.
ENDCLASS.
CLASS father DEFINITION INHERITIN- FROM -RANDFATHER.
,$./+! #e!t+"n.
METHODS CONSTRUCTOR.
ENDCLASS.
CLASS father IMPLEMENTATION.
METHOD !"n#tr$!t"r .
!a// )eth"d #$,er01!"n#tr$!t"r.
%RITE&' (I a) father(.
#*+,.
ENDMETHOD.
ENDCLASS.
CLASS #"n DEFINITION INHERITIN- FROM FATHER.
,$./+! #e!t+"n.
METHODS CONSTRUCTOR.
ENDCLASS.
CLASS #"n IMPLEMENTATION.
METHOD !"n#tr$!t"r .
!a// )eth"d #$,er01!"n#tr$!t"r.
%RITE&' (I a) #"n(.
#*+,.
ENDMETHOD.
ENDCLASS.
START0OF0SELECTION.
DATA )2#"n t2,e ref t" #"n.
Page : 12 of 108
Examples on Object Oriented Programming in ABAP
CREATE OB3ECT )2#"n.
Output
I a) grandfather
I a) father
I a) #"n
Page : 14 of 108
Examples on Object Oriented Programming in ABAP
..1> %tatic constructor o a class is calle! only once per program.
Theme The first %hen a subclass in a program is accessed$ its static constructor is
executed But$ before it can be executed$ the static constructors of all of its
superclasses must alread, ha"e been executed A static constructor ma, onl,
be called once per program Therefore$ %hen one first address a subclass$ the
s,stem loo)s for the next+highest superclass %hose static constructor has not
,et been executed !t executes the static constructor of that class$ follo%ed
b, those of all classes bet%een that class and the subclass that is addressed
Program
*escr
This program contains three classes:+
FAT5ER %ith its o%n static constructor method
-O/$ subclass of 9AT=E< %ith its o%n static constructor method
An object is created finall, from the class -O/$ %hich triggers the
constructor methods in the order: 9AT=E<-O/
/o%$ an object is created from the class 9AT=E< But$ that did not trigger
constructor of class 9AT=E<$ because that had alread, been triggered b, the
program %hen an object %as created from the class -O/
This establishes the theme.
*ump
REPORT YSUBOOPS1M.
CLASS 6at7er DEINITION.
p*-l,c sect,"n.
class-.ET/ODS ! classACONSTRUCTOR.
ENDCLASS.
CLASS 6at7er I.PLE.ENTATION.
.ET/OD classAc"nstr*ct"r .
0RITE!45 +I a# 6at7er+.
s:,p.
END.ET/OD.
ENDCLASS.
CLASS s"n DEINITION IN/ERITIN9 RO. AT/ER.
p*-l,c sect,"n.
class-.ET/ODS ! classACONSTRUCTOR.
ENDCLASS.
CLASS s"n I.PLE.ENTATION.
.ET/OD classAc"nstr*ct"r .
0RITE!45 +I a# s"n+.
s:,p.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA! #(s"n t(pe re6 t" s"n.
CREATE OBDECT! #(s"n.
0r,te!45 ?/,@.
+ata : )2%at*er t2pe re% to %at*er.
create o34ect : )2%at*er.
Output ! am father
Page : 11 of 108
Examples on Object Oriented Programming in ABAP
! am son
=i
..11 %tatic type an! Dynamic type o a "ariable
Theme -tatic t,pe of a reference "ariable can point to a superclassU %hereas its d,namic
t,pe can point to one of its subclasses
This program %ill sho% ,ou "arious %a,s to do that
Program
*escrip
This program contains class (1 %ith method ;1 (lass (. is a subclass of (1 and
contains redefined implementation of method ;1
<eference "ariables are created in the program as follo%s:+
Reerence
;ariable
%tatic
type
Dynamic
type
Dynamic type assigne! by
O<E91 (1 (1 (<EATE object oref1
O<E911 (1 (. (<EATE OBOE(T oref11 TBPE (.
O<E9111 (1 (. (<EATE OBOE(T O<E9111
O<E9111 K O<E9.
O<E9. (. (. (<EATE OBOE(T oref.
9inall,$ method ;1 ic called using all objectsThe obser"ations are as follo%s:+

3etho! call Calls metho! Reason
O<E91+H;1 ;1 of class (1 Both -tatic V *,namic t,pe of O<E91 refers to (1
O<E911+H;1 ;1 of class (. -tatic t,pe of O<E911 refers to (1$ d,namic t,pe to (.
O<E9111+H;1 ;1 of class (. -tatic t,pe of O<E9111 refers to (1$ d,namic t,pe to (.
O<E9.+H;1 ;1 of class (. Both -tatic V *,namic t,pe of O<E9. refers to (.
*ump
REPORT YSUBOOPS1M.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
#et7"$s ! #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1 .
3r,te!45 + I a# #1 "6 c1+.
en$#et7"$.
en$class.
class c2 $eKn,t,"n ,n7er,t,n< 6r"# c1.
p*-l,c sect,"n.
#et7"$s ! #1 re$eKn,t,"n.
en$class.
class c2 ,#ple#entat,"n.
#et7"$ #1.
3r,te!45 +I a# #1 "6 c2+.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C18
ORE11 TYPE RE TO C18
ORE111 TYPE RE TO C18
ORE2 TYPE RE TO C2 .
CREATE OBDECT ! ORE1 8
ORE11 TYPE C28
ORE111 8
Page : 16 of 108
Examples on Object Oriented Programming in ABAP
ORE2 .
ORE111 ; ORE2.
CALL .ET/OD ! ORE1->.1 8 R O*tp*t ! I a# #1 "6 c1
ORE11->.1 8 R O*tp*t ! I a# #1 "6 c2
ORE111->.18 R O*tp*t ! I a# #1 "6 c2
ORE2->.1 . R O*tp*t ! I a# #1 "6 c2
Output
I a# #1 "6 c1
I a# #1 "6 c2
I a# #1 "6 c2
I a# #1 "6 c2

Page : 17 of 108
Examples on Object Oriented Programming in ABAP
..1* %tatic type shoul! be more general than !ynamic type o a reerence "ariable
Theme -tatic t,pe of a reference "ariable can refer to a superclass$ %hereas its
d,namic t,pe can refer to a subclass of the superclass !n that case$ the
reference "ariable %ill identif, all the common components of the superclass and
subclass !t %ill not be able to identif, an, ne% components in the subclass$ %hich
are not present in its superclass
Program
*escr
This program contains class (1 and its subclass (.
(lass (1 contains method ;1$ %hich is also redefined in class (.
(lass (. contains a ne% method ;.
A reference "ariable O<E911 is created %ith static t,pe of (1 and d,namic t,pe
of (.
;ethod ;. is attempted to be called using O<E911
This produces compilation error$ establishing the theme
*ump
REPORT YSUBOOPS1M.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
#et7"$s ! #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1 .
3r,te!45 + I a# #1 "6 c1+.
en$#et7"$.
en$class.
class c2 $eKn,t,"n ,n7er,t,n< 6r"# c1.
p*-l,c sect,"n.
#et7"$s ! #1 re$eKn,t,"n.
#et7"$s ! #2 .
en$class.
class c2 ,#ple#entat,"n.
#et7"$ #1.
3r,te!45 +I a# #1 "6 c2+.
en$#et7"$.
#et7"$ #2.
3r,te!45 +I a# #2+.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE11 TYPE RE TO C1.
CREATE OBDECT ! ORE11 TYPE C2.

CALL .ET/OD ! ORE11->.2 .

Output (ompilation error fails to identif, method ;. in the last statement of the
program
Page : 18 of 108
Examples on Object Oriented Programming in ABAP
..1+ 3etho! o a parent class1 use! rom its subclass1 uses attributes o the parent
class only1 i the metho! is not re-!eine! in subclass.
Theme As long as a method> using pri"ate attributes? inherited from a superclass is not
redefined$ it still uses the pri"ate attributes of the superclass$ not those of the
subclass$ e"en if the subclass has pri"ate attributes of the same name
Program
*escrip
(lass C1 contains a method 31 in the public section and a pri"ate "ariable$ (23
of "alue K 1 ;ethod 31 in class C1 displa,s the "alue of pri"ate "ariable1 (23
(lass C* is a subclass of class C1 !t does not redefine method ;1 But$ it has
also a pri"ate "ariable $ (23 %ith "alue K 6
An object is created from class (. and the method 31 is called
The output sho%s that the "ariable (23> as displa,ed b, method ;1? has been
ta)en from class C1$ not C*
This establishes the theme.
*ump
rep"rt (s*-$el .
CLASS c1 DEINITION.
PUBLIC SECTION .
.ET/ODS ! #1 .
#RI5ATE SECTIN.
DATA ! n6) TYPE I 1ALUE 5 .
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD ! #1 .
3r,te!45 n*# .
END.ET/OD.
ENDCLASS.
CLASS c2 DEINITION IN/ERITIN9 RO. c1.
PUBLIC SECTION .
DATA ! n6) TYPE I 1ALUE J.
ENDCLASS.
CLASS c2 I.PLE.ENTATION.
ENDCLASS.
START-O-SELECTION.
DATA ! "re62 TYPE RE TO c2 .
CREATE OBDECT ! "re62 .
CALL .ET/OD "re62->#1 .
Output 1

Page : 13 of 108
Examples on Object Oriented Programming in ABAP
..1. Demo on Bi!ening Cast
Theme This program %ill sho% the use of %idening cast operator
Program
*escription
(lass C1 is superclass of C*. Object of class C* is assigned the object of
class C1 using %idening cast operator This helps to a"oid the compilation
error$ but genarates the error DcxGs,Gmo"eGcastGerror trappedD at runtime$
%hich is handled and reported properl, in the program
*ump <EPO<T B-8B*E5
class c1 definition
public section
data : num t,pe i "alue 1
endclass
class c1 implementation
endclass
class c. definition inheriting from c1
public section
endclass
class c. implementation
endclass
start+of+selection
data : obj1 t,pe ref to c1 $
obj. t,pe ref to c.
create object : obj1 $
obj.
T<B
obj. WK obj1
(AT(= cxGs,Gmo"eGcastGerror
%rite:'1 DcxGs,Gmo"eGcastGerror trappedD
E/*T<B
Output cxGs,Gmo"eGcastGerror trapped
Page : 60 of 108
Examples on Object Oriented Programming in ABAP
4 'nterace
4.1 %imple use o an interace
Theme This program %ill sho% simple use of an interface %ith its o%n data and methods
and ho% it is implemented in a class !t %ill also sho% that there can be methods
of same name for an interface and the class implementing the interface
Program
*esc
This program contains an interface '1 %ith attribute : (23 an method : 3ET51
This interface is implemented in a class : C1 %hich also has its o%n method
;ET=1
An object OREF is created from class C1 and both the methods 3ET51 $ one for
class and another for interface is called using the object
*ump
rep"rt (s*-$el .
,nter6ace ,1.
$ata ! n*# t(pe , .
#et7"$s ! #et71.
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
#et7"$s ! #et71. R class C1@s "3n #et7"$
,nter6aces ! ,1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! #et71.
3r,te!45 +I a# #et71 ,n c1+.
en$#et7"$.
#et7"$ ,1S#et71.
3r,te!45 +I a# #et71 6r"# ,1+.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
3r,te!45 "re6->,1Sn*#.
call #et7"$ "re6->#et71.
call #et7"$ "re6->,1S#et71.
Output 0
! am meth1 in c1
! am meth1 from i1
Page : 61 of 108
Examples on Object Oriented Programming in ABAP
4.* 'nteraces can only be implemente! in the public section o a class
Theme This program %ill sho% ,ou that classes implementing an interface can onl,
contain the features of the interface in its public section
Program
*escription
!n this program$ class C1 is tr,ing to accommodate interface '1 in its
PR';ATE %ECT'O(
This creates a compilation error1 establishing the theme
*ump
rep"rt (s*-$el .
,nter6ace ,1.
#et7"$s ! #et71.
en$,nter6ace.
class c1 $eKn,t,"n.
protecte+ section.
,nter6aces ! ,1.
en$class.
Output (ompilation error %ith error message :+
'(TERFACE% may only be implemente! in the public section.
Page : 6. of 108
Examples on Object Oriented Programming in ABAP
4.+ A class #ith an interace shoul! implement all the metho!s o that interace
Theme This program %ill sho% that a class containing an interface should implement all
the methods of the interface in its implementation section
Program
*escrip
(lass C1 implements interface '1$ %hich has got t%o methods $ 3ET51 and
3ET5* But$ in the !;P5E;E/TAT!O/ section of class C1$ onl, 3ET51 is
implemented
This program %ill create a compilation error$ establishing the theme.
*ump
rep"rt (s*-$el .
,nter6ace ,1.
#et7"$s ! #et71 8
#et72 .
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! ,1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ,1S#et71.
3r,te!45 +I a# #et71 6r"# ,1+.
en$#et7"$.
en$class.

start-"6-select,"n.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
call #et7"$ "re6->,1S#et71.

Output (ompilation error %ith error message :+
'mplementation missing or metho! I'1K3ET5*J
Page : 62 of 108
Examples on Object Oriented Programming in ABAP
4.. ;alues or interace attributes are assigne! at the time o inclusion in a class
Theme One cannot specif, "alues for attributes %hile declaring them in an interface li)e
the follo%ing fashion : *ATA : I"arH TBPE It,peH AA58E I"alH
!nstead of doing that$ one has to specif, the "alues for different attributes of
interface at the point %here the interface is declared in the public section of a
class
Program
*escr
!nterface '1 contains t%o numeric attributes $ (231 and (23*
!n "ersion 1 of the program$ attempt is made to specif, the "alues %hile defining
those attributes in the interface This "ersion does not get successfull, compiled
and thus establishes the theme
!n "ersion .$ "alues for interface attributes are specified at the time %hen '1 is
included in the public section of class C1 This "ersion gets successfull, compiled
and produces a result
Aersion 1 Aersion .
Output Output o "ersion 1:-
(ompilation error %ith error message:+ Bithin an interace1 you cannot use
;A&2E #ith attributes7 e)cept constants8
Output o "ersion *:-
1 6
Page : 64 of 108
rep"rt (s*-$el .
,nter6ace ,1 .
$ata ! n*#1 t2pe i 7a06e 5 8
n*#2 t2pe i 7a06e 8 .
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! ,1 .
#et7"$s #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1.
3r,te!45 ,1Sn*#18
,1Sn*#2.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
call #et7"$ "re6->#1.
rep"rt (s*-$el .
,nter6ace ,1 .
$ata ! n*#1 t(pe , 8
n*#2 t(pe , .
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! ,1 'ATA 5A9UES n6)1
= 5 n6)1 = 8 .
#et7"$s #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1.
3r,te!45 ,1Sn*#18
,1Sn*#2.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
call #et7"$ "re6->#1.
Examples on Object Oriented Programming in ABAP
4.4 2se o F'(A& metho!s rom 'nterace
Theme This program %ill demonstrate ho% to create final method in a class from one
of the methods of an interface
Program
*escription
!nterface '1 contains t%o methods : 31 and 3*
!1 is included and incorporated in class : C1 %ith 3* as a final method Both
the methods are implemented in class C1
(lass C* is a subclass of class (1 !t redefines method : '1K31 and re+
implements it$ but it does not do that for '1K3* as that is declared as final
method
!n the -TA<T+O9+-E5E(T!O/ bloc)$ object OREF1 is created from class C1
and OREF* from class C* and both the methods ;1 and ;. are called using
both the objects
*ump
rep"rt (s*-$el .
,nter6ace ,1 .
#et7"$s ! #1 8
#2 .
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! I1 -na0 )et*o+s #2 .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ,1S#1.
3r,te!45 +I a# #1 ,n c1+.
en$#et7"$.
#et7"$ ,1S#2.
3r,te!45 +I a# #2 ,n c1+.
en$#et7"$.
en$class.
class c2 $eKn,t,"n ,n7er,t,n< 6r"# c1.
p*-l,c sect,"n.
#et7"$s ! ,1S#1 re$eKn,t,"n .
en$class.
class c2 ,#ple#entat,"n.
#et7"$ ! ,1S#1.
3r,te!45 +I a# #1 ,n c2+.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re61 t(pe re6 t" c18
"re62 t(pe re6 t" c2 .
create "-=ect ! "re61 8 "re62.
call #et7"$ ! "re61->,1S#1 8 R O*tp*t ! I a# #1 ,n c1
"re62->,1S#1 8 R O*tp*t ! I a# #1 ,n c2
"re61->,1S#2 8 R O*tp*t ! I a# #2 ,n c1
"re62->,1S#2 . R O*tp*t ! I a# #2 ,n c1
Output ! am m1 in c1
! am m1 in c.
! am m. in c1
! am m. in c1
Page : 61 of 108
Examples on Object Oriented Programming in ABAP
4.9 2se o Abstract metho!s rom 'nterace
Theme This program %ill demonstrate the %a, b, %hich a method from an interface can
be used as an abstract method for a class
Abstract methods can onl, be implemented b, the subclasses
A class containing an abstract method should be abstract itself
Program
*esc
This program contains an interface '1 %ith t%o methods $ 31 and 3*
(lass C1 includes and implements methods of interface '1$ declaring
method 3* as an abstract method =ence$ class C1 %as also declared as
an abstract class
(lass C1 implements method '1K31 $ but not '1K3* as this is an
abstract method
(lass C* is a subclass of C1 $ %hich defines '1K3*
!n the -TA<T+O9+-E5E(T!O/ bloc)$object OREF* is created from
class C*> class (1 cannot be instantiated$ as this is an abstract class? and
both the methods : '1K31 and '1K3* are called
*ump
rep"rt (s*-$el .
,nter6ace ,1 .
#et7"$s ! #1 8
#2 .
en$,nter6ace.
class c1 $eKn,t,"n a-stract.
p*-l,c sect,"n.
,nter6aces ! I1 a-stract #et7"$s #2 .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ,1S#1.
3r,te!45 +I a# #1 ,n c1+.
en$#et7"$.
en$class.
class c2 $eKn,t,"n ,n7er,t,n< 6r"# c1.
p*-l,c sect,"n.
#et7"$s ! ,1S#2 re$eKn,t,"n.
en$class.
class c2 ,#ple#entat,"n.
#et7"$ ! ,1S#2.
3r,te!45 +I a# #2 ,n c2+.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! "re62 t(pe re6 t" c2 .
create "-=ect ! "re62.
call #et7"$ ! "re62->,1S#1 8
"re62->,1S#2.
Output ! am m1 in c1
! am m. in c.
Page : 66 of 108
Examples on Object Oriented Programming in ABAP
4.: 2se o 'nterace Reerence ;ariable
Theme This program %ill sho% the use of interface reference "ariable and ho% it can be
used to access the components of an interface in a class>implementing that
interface? 8se of interface reference "ariable pa"es the %a, for pol,morphism
"ia interface
Program
*escrip
!nterface '1 $ included' implemented in class C1 has the follo%ing components:+

component nature
(Gname (onstant %ith "alue KABAP
inum !nstance attribute %ith "alue K 1
cnum -tatic attribute of "alue 6
;1 !nstance method
;. -tatic method

(lass C1 implements all the methods in its !;P5E;E/TAT!O/ section
!n the -TA<T+O9+-E5E(T!O/ bloc)$ all the different attributes and methods
are called using class reference "ariable O<E9 first
Then$ interface reference "ariable !<E9 is used to do similar jobs$ after the
assignment !<E9 K O<E9
*ump
rep"rt (s*-$el .
,nter6ace ,1 .
c"nstants ! cAna#e%2' t(pe c )al*e +ABAP+.
$ata ! ,n*# t(pe , .
class-$ata ! cn*# t(pe , .
#et7"$s ! #1 .
class-#et7"$s ! #2.
en$,nter6ace.
class c1 $eKn,t,"n .
p*-l,c sect,"n.
,nter6aces ! I1 $ata )al*es ,n*# ; 5 cn*# ; J .
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ,1S#1.
3r,te!45 +I a# #1 ,n c1+.
en$#et7"$.
#et7"$ ,1S#2.
3r,te!45 +I a# class #et7"$ #2 ,n c1+.
en$#et7"$.
en$class.
start-"6-select,"n.
$ata ! ,re6 t(pe re6 t" ,1 8
"re6 t(pe re6 t" c1 .
create "-=ect ! "re6.
3r,te!45 "re6->,1S,n*# 8
"re6->,1Scn*# 8
c1;>,1Scn*# .
call #et7"$ ! "re6->,1S#1 8
"re6->,1S#2 8
c1;>,1S#2 .
3r,te!45 s(-*l,ne .
,re6 ; "re6 .
3r,te!45 ,re6->,n*# 8
,re6->cn*# 8
,1;>cAna#e .
call #et7"$ ! ,re6->#1 8
Page : 67 of 108
Examples on Object Oriented Programming in ABAP
,re6->#2 .
Output 1 6 6
! am m1 in c1
! am class method m. in c1
! am class method m. in c1

1 6 ABAP
! am m1 in c1
! am class method m. in c1

Page : 68 of 108
Examples on Object Oriented Programming in ABAP
4.< 2se o (este! 'nterace
Theme This program %ill demonstrate ho% an interface can be included in another
interface C And the final interface containing all the interfaces can be used
inside a class
Program
*escr
!nterface '1 contains method 31
!nterface '* includes interface '1 =o%e"er$ it also contains t%o
methods 31 and 3* of its o%n
(lass C1 contains interface '* and implements methods : '1K31 1 '*K31
and '*K3*.
Object is created from class C1 and all the methods are called
rep"rt (s*-$el .
,nter6ace ,1 .
#et7"$s #1.
en$,nter6ace.
,nter6ace ,2.
#et7"$s ! #1 8 #2.
,nter6aces ,1.
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! ,2.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ! ,1S#1.
3r,te!45 +I a# #1 6r"# ,1+.
en$#et7"$.
#et7"$ ! ,2S#1.
3r,te!45 +I a# #1 6r"# ,2+.
en$#et7"$.
#et7"$ ! ,2S#2.
3r,te!45 +I a# #2 6r"# ,2+.
en$#et7"$.
en$class.
START-O-SELECTION.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
call #et7"$ ! "re6->,1S#1 8 R O*tp*t ! I a# #1 6r"# ,1
"re6->,2S#1 8 R O*tp*t ! I a# #1 6r"# ,2
"re6->,2S#2 . R O*tp*t ! I a# #1 6r"# ,2
Output ! am m1 from i1
! am m1 from i.
! am m. from i.
Page : 63 of 108
Examples on Object Oriented Programming in ABAP
4.= 2sing A&'A%E%
Theme The full name of a component %hich an !nterface adds to a class or another
interface is intfXcomp Alias names can be substituted for this name %hen
#defining compound interfaces$ or
#declaring !nterfaces in a class
Program
*escr
!nterface '1 contains method 31
!nterface '* contains method : 31 and 3* and interface '1 !t aliases method
31 of '1 as 3ET51
(lass C1 contains interface '* and aliases method 3* of '* as 3ET5*
All the methods :+ '1K31 1 '*K31 and '*K3* are implemented in class C1
!n the -TA<T+O9+-E5E(T!O/ bloc)$ object OREF is created from class C1 and
the alias names are used to call the methods
*ump
rep"rt (s*-$el .
,nter6ace ,1 .
#et7"$s #1.
en$,nter6ace.
,nter6ace ,2.
#et7"$s ! #1 8 #2 .
,nter6aces ,1.
a0iases #et71 6"r ,1S#1.
en$,nter6ace.
class c1 $eKn,t,"n.
p*-l,c sect,"n.
,nter6aces ! ,2.
a0iases #et72 6"r ,2S#2.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ ,1S#1.
3r,te!45 +I a# #1 6r"# ,1+.
en$#et7"$.
#et7"$ ! ,2S#1.
3r,te!45 +I a# #1 6r"# ,2+.
en$#et7"$.
#et7"$ ! ,2S#2.
3r,te!45 +I a# #2 6r"# ,2+.
en$#et7"$.
en$class.
START-O-SELECTION.
$ata ! "re6 t(pe re6 t" c1.
create "-=ect "re6.
call #et7"$ ! "re6->,2S#et71.
call #et7"$ ! "re6->#et72 .
Output ! am m1 from i1
! am m. from i.
Page : 70 of 108
Examples on Object Oriented Programming in ABAP
4.1> Polymorphism "ia 'nteraces
Theme This program %ill demonstrate ho% a method of an interface can be interpreted
differentl, in different classes At runtime$ one can use different class
reference "ariables to call same method of the interface to ha"e different
things done
8se of interface reference "ariable in this context further simplifies the %or)
Program
*escrip
Both the classes (1 and (. $ totall, different from each other$
contains interface !1
!nterface !1 contains method ;1 $ %hich is implemented differentl, in
(1 and (.
!n the -TA<T+O9+-E5E(T!O/ bloc)$ objects are created from class
(1 and (. An !nterface reference "ariable !<E9 is also defined
B, assigning the class reference "ariables> of (1 and (. $ one at a time?
to interface reference "ariable !<E9 and then calling method ;1 >using
!<E9? clearl, demonstrates ho% pol,morphism can be achie"ed using
the concept of interface
*ump
REPORT YSUBDEL.
,nter6ace I1.
.ET/ODS ! .1 .
ENDINTERACE.
CLASS C1 DEINITION.
PUBLIC SECTION.
INTERACES ! I1.
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD I1S.1.
0RITE!45 +I a# #et7"$ #1 ,n c1+.
END.ET/OD.
ENDCLASS.
CLASS C2 DEINITION.
PUBLIC SECTION.
INTERACES ! I1.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
.ET/OD I1S.1.
0RITE!45 +I a# #et7"$ #1 ,n c2+.
END.ET/OD.
ENDCLASS.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1 8
ORE2 TYPE RE TO C2 8
IRE TYPE RE TO I1 .
CREATE OBDECT ! ORE1 8
ORE2 .
IRE ; ORE1.
CALL .ET/OD IRE->.1.
IRE ; ORE2.
CALL .ET/OD IRE->.1.
Output ! am method m1 in c1
! am method m1 in c.
Page : 71 of 108
Examples on Object Oriented Programming in ABAP
9 Frien!ship
9.1 Frien!ship bet#een Classes
Theme A class can grant friendship to another class B, granting friendship $ it allo%s
another class to:+
8se its pri"ate components
!nstantiate it$ irrespecti"e of the (<EATE P<!AATE addition
Program
*escr
(lass C* is created using (<EATE P<!AATE option That means$
onl, the class itself and its friends can instantiate this class
(lass C* has a pri"ate method 3* and a pri"ate attribute $ (23
This means that these components can be accessed b, class (.
itself and its friends
/o%$ C* has granted friendship to class (1
-o$ methods of class C1 can access pri"ate components of C* as
%ell as can instantiate class C*
This establishes the theme.
*ump
REPORT YSUBDEL.
CLASS C1 DEINITION DEERRED.
CLASS C2 DEINITION CREATE #RI5ATE ,RIEN'S C1 .
PROTECTED SECTION.
DATA ! NU. TYPE I 1ALUE 5.
.ET/ODS ! .2.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
.ET/OD .2.
0RITE!45 +I a# #et7"$ #2 ,n C2+.
END.ET/OD.
ENDCLASS .
class c1 $eKn,t,"n.
p*-l,c sect,"n .
#et7"$s ! #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1.
'ATA : RE,1 T:#E RE, T C1.
CREATE !"ECT RE,1.
WRITE:/5 RE,1;>NUM.
CA99 MET&' RE,1;>M1.
END.ET/OD.
en$class.
START-O-SELECTION.
DATA ! ORE1 TYPE RE TO C1.
CREATE OBDECT ORE1.
CALL .ET/OD ORE1->.1.
Output 1
! am method m. in (.
Page : 7. of 108
Examples on Object Oriented Programming in ABAP
9.* %ubclasses o rien!s can also become rien!s.
Theme -ubclasses of the friend class are also friends of the class granting
friendship> to their super classes?
Program
*escrip
(lass C* has granted friendship to class C1=ence$ (1 is friend of class
(.
(lass C11 is a subclass of class C1
-o$ class C11 is also a friend of class C*(lass C11 can thus access the
protected components of class C*
This establishes the theme
REPORT YSUBDEL.
CLASS C1 DEINITION DEERRED.
CLASS C2 DEINITION ,RIEN'S C1 .
PROTECTED SECTION.
DATA ! NU. TYPE I 1ALUE 5.
ENDCLASS.
CLASS C2 I.PLE.ENTATION.
ENDCLASS .
class c1 $eKn,t,"n.
p*-l,c sect,"n .
#et7"$s ! #1.
en$class.
class c1 ,#ple#entat,"n.
#et7"$ #1.
DATA ! ORE2 TYPE RE TO C2.
CREATE OBDECT ORE2.
0RITE!45 ORE2->NU..
END.ET/OD.
en$class.
class c11 $eKn,t,"n ,n7er,t,n< 6r"# c1.
p*-l,c sect,"n.
#et7"$s ! #11.
en$class.
class c11 ,#ple#entat,"n.
#et7"$ #11.
'ATA : RE,1 T:#E RE, T C1.
CREATE !"ECT RE,1.
WRITE:/5 RE,1;>NUM.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE11 TYPE RE TO C11.
CREATE OBDECT ORE11.
CALL .ET/OD ORE11->.11.
Output 1
Page : 72 of 108
Examples on Object Oriented Programming in ABAP
9.+ Frien!ship is one si!e!
Theme !n principle$ granting of friendship is one+sided: A class granting a friendship is
not automaticall, a friend of its friends !f the class granting the friendship
%ants to access the pri"ate components of a friend$ then the latter has to
explicitl, grant friendship to the former
Program
*escr
(lass C* grants friendship to class C1 =ence$ class C1 can access protected
attribute>num.? of C*
But$ class C* cannot access protected attribute>num1? of class C1 This is
because friendship is one+sided
To allo% C* access protected attribute of C1$ class (1 must also declare (. as its
friend
Page : 74 of 108
REPORT YSUBDEL.
CLASS C1 DEINITION DEERRED.
CLASS C2 DEINITION ,RIEN'S C1 .
PROTECTED SECTION.
DATA ! NU.2 TYPE I 1ALUE 15.
.ET/ODS ! .2.
ENDCLASS.
class c1 $eKn,t,"n .
p*-l,c sect,"n.
#et7"$s ! #et7p*-.
pr,)ate sect,"n .
$ata ! n*#1 t(pe , )al*e 10 .
#et7"$s ! #1.
en$class.

CLASS C2 I.PLE.ENTATION.
.ET/OD .2.
$ata ! "re61 t(pe re6 t" c1.
create "-=ect "re61.
.rite:/5 ore%1;>n6)1.
END.ET/OD.
ENDCLASS .
class c1 ,#ple#entat,"n.
#et7"$ #1.
DATA ! ORE2 TYPE RE TO C2.
CREATE OBDECT ORE2.
0RITE!45 ORE2->NU.2.
END.ET/OD.

#et7"$ #et7p*-.
call #et7"$ #1.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE TYPE RE TO C1.
CREATE OBDECT ORE .
CALL .ET/OD ORE ->.ET/PUB.
REPORT YSUBDEL.
CLASS C1 DEINITION DEERRED.
CLASS C2 DEINITION ,RIEN'S C1 .
PROTECTED SECTION.
DATA ! NU.2 TYPE I 1ALUE 15.
.ET/ODS ! .2.
ENDCLASS.
class c1 $eKn,t,"n %rien+s c1.
p*-l,c sect,"n.
#et7"$s ! #et7p*-.
pr,)ate sect,"n .
$ata ! n*#1 t(pe , )al*e 10 .
#et7"$s ! #1.
en$class.

CLASS C2 I.PLE.ENTATION.
.ET/OD .2.
$ata ! "re61 t(pe re6 t" c1.
create "-=ect "re61.
.rite:/5 ore%1;>n6)1.
END.ET/OD.
ENDCLASS .
class c1 ,#ple#entat,"n.
#et7"$ #1.
DATA ! ORE2 TYPE RE TO C2.
CREATE OBDECT ORE2.
0RITE!45 ORE2->NU.2.
END.ET/OD.

#et7"$ #et7p*-.
call #et7"$ #1.
en$#et7"$.
en$class.
START-O-SELECTION.
DATA ! ORE TYPE RE TO C1.
CREATE OBDECT ORE .
CALL .ET/OD ORE ->.ET/PUB.
Examples on Object Oriented Programming in ABAP
'ncorrect Correct
Output 11
: E"ents
:.1 E"ents #ith 5an!ler 3etho! in the same class
Theme E"ent is a mechanism b, %hich method of one class can raise method of another
class$ %ithout the haEard of instantiating that class
The steps to be follo%ed are as follo%s:+
(reate an e"ent in a class
(reate a triggering method in the same class %hich %ill raise the e"ent
(reate an e"ent handler method for the e"ent in same'other class
<egister the e"ent handler method in the program
/o%$ ,our settings are complete (reate an object from the class containing the
e"ent and call the triggering method to raise the e"ent
Program
*escr
(lass C1 contains an e"ent E1$ for %hich the triggering method is T1
E"ent handler method for e"ent E1 is 31$ placed in the same class C1
<egistration is done at runtime for ;1
Object is created from class C1 and the triggering method T1 is called$ %hich
raises the e"ent and ultimatel, calls e"ent handler method 31
*ump
REPORT YSUBOOPSO .
CLASS c1 DEINITION.
PUBLIC SECTION.
C%1'Creat,n< e)ent ! E1
E1ENTS! E1.
C%2' Creat,n< an e)ent 7an$l,n< #et7"$. T7,s #et7"$ can -el"n< t"
C sa#e "r $,Ierent class
.ET/ODS! .1 OR E1ENT E1 O c1.
C .et7"$ t" ra,se t7e e)ent
.ET/ODS ! T1.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
C .et7"$ ! .1 3,ll -e calle$ 37en t7e e)ent ,s ra,se$
.ET/OD ! .1.
3r,te!45 + I a# t7e e)ent 7an$ler #et7"$+.
END.ET/OD.
C .et7"$ ! T1 3,ll ra,se t7e e)ent
.ET/OD ! T1.
3r,te!45 +I a# T18 <",n< t" ra,se e)ent E1+.
ra,se e)ent E1.
END.ET/OD.
ENDCLASS.
Start-"6-select,"n.
Data! "re6 t(pe re6 t" c1.
Create "-=ect! "re6 .
C Re<,ster,n< t7e e)ent 7an$ler #et7"$
SET /ANDLER "re6->.1 OR "re6 .
Page : 71 of 108
Examples on Object Oriented Programming in ABAP
C Call,n< t7e e)ent 37,c7 3,ll ra,se t7e e)ent.
call #et7"$ "re6->T1.
Output ! am T1$ going to raise e"ent E1
! am the e"ent handler method
:.* E"ent #ith e"ent han!ler metho! in !ierent class
Theme -imilar to abo"e =ere$ e"ent handler method is in different class
(lass C1 contains an e"ent E1$ for %hich the triggering method is T1
E"ent handler method for e"ent E1 is 31$ placed in the another class$C*
<egistration is done at runtime for 31
Object is created from class C1 and the triggering method T1 is called$ %hich
raises the e"ent and ultimatel, calls e"ent handler method 31 in class C*
*ump
REPORT YSUBOOPSO .
CLASS c1 DEINITION.
PUBLIC SECTION.
C Creat,n< e)ent ! E1
E1ENTS! E1.
C Tr,<<er,n< #et7"$ ! T1
.ET/ODS ! T1.
ENDCLASS.
CLASS C2 DEINITION.
PUBLIC SECTION.
C Creat,n< an e)ent 7an$l,n< #et7"$.
.ET/ODS! .1 OR E1ENT E1 O c1.
en$class.
CLASS c1 I.PLE.ENTATION.
C .et7"$ ! T1 3,ll ra,se t7e e)ent
.ET/OD ! T1.
3r,te!45 +I a# T18 <",n< t" ra,se e)ent E1+.
ra,se e)ent E1.
END.ET/OD.
ENDCLASS.
class c2 ,#ple#entat,"n.
C .et7"$ ! .1 3,ll -e calle$ 37en t7e e)ent ,s ra,se$
.ET/OD ! .1.
3r,te!45 + I a# t7e e)ent 7an$ler #et7"$ ,n c2+.
END.ET/OD.
en$class.
Start-"6-select,"n.
Data! "re61 t(pe re6 t" c18
"re62 t(pe re6 t" c2.
Create "-=ect! "re61 8 "re62 .
C Re<,ster,n< t7e e)ent 7an$ler #et7"$
SET /ANDLER "re62->.1 OR "re61 .
C Call,n< t7e e)ent 37,c7 3,ll ra,se t7e e)ent.
call #et7"$ "re61->T1.
Output
! am T1$ going to raise e"ent E1
! am the e"ent handler method in c.
Page : 76 of 108
Examples on Object Oriented Programming in ABAP

:.+ 3ore than one e"ent han!ler metho! can e)ist or same e"ent
Theme 9or an e"ent in a class$ there can be more than one e"ent handler methods in
same or different class =o%e"er$ at runtime onl, one e"ent handler method %ill
be triggered at a time$ based on the registration
Program
*escr
(lass C1 contains an e"ent E1$ for %hich the triggering method is T1 and
the e"ent handler methods are :+
31 in same class C1
3* in another class C*
!n the -TA<T+O9+-E5E(T!O/ bloc)$ objects are created from class C1
and C*
9irst$ registration is made using method 31 of class C1 as e"ent handler
method
Then$ the e"ent E1 is raised$ calling method T1 This raises e"ent handler
method 31 of class C1
After that$ the earlier registration is de+acti"ated and ne% registration
is made for method 3* of class C* as e"ent handler method
E"ent E1 is raised calling method T1 This raises e"ent handler method
3* of class C*
*ump
REPORT YSUBOOPSO .
CLASS c1 DEINITION.
PUBLIC SECTION.
C Creat,n< e)ent ! E1
E1ENTS! E1.
C Creat,n< an e)ent 7an$l,n< #et7"$.
.ET/ODS! .1 OR E1ENT E1 O c1.
C .et7"$ t" ra,se t7e e)ent
.ET/ODS ! T1.
ENDCLASS.
CLASS C2 DEINITION.
PUBLIC SECTION.
C Creat,n< an e)ent 7an$l,n< #et7"$.
.ET/ODS! .2 OR E1ENT E1 O c1.
en$class.
CLASS c1 I.PLE.ENTATION.
C .et7"$ ! T1 3,ll ra,se t7e e)ent
.ET/OD ! T1.
3r,te!45 +I a# T18 <",n< t" ra,se e)ent E1+.
ra,se e)ent E1.
END.ET/OD.
C .et7"$ ! .1 3,ll -e calle$ 37en t7e e)ent ,s ra,se$
.ET/OD ! .1.
3r,te!45 + I a# t7e e)ent 7an$ler #et7"$ .1 ,n c1+.
END.ET/OD.
ENDCLASS.
class c2 ,#ple#entat,"n.
C .et7"$ ! .2 3,ll -e calle$ 37en t7e e)ent ,s ra,se$
Page : 77 of 108
Examples on Object Oriented Programming in ABAP
.ET/OD ! .2.
3r,te!45 + I a# t7e e)ent 7an$ler #et7"$ .2 ,n c2+.
END.ET/OD.
en$class.
Start-"6-select,"n.
Data! "re61 t(pe re6 t" c18
"re62 t(pe re6 t" c2.
Create "-=ect! "re61 8 "re62 .
C Re<,ster,n< t7e e)ent 7an$ler #et7"$
SET /ANDLER "re61->.1 OR "re61 .
C Call,n< t7e e)ent 37,c7 3,ll ra,se t7e e)ent.
call #et7"$ "re61->T1.
C De-Re<,ster,n< t7e earl,er e)ent 7an$ler #et7"$
SET /ANDLER "re61->.1 OR "re61 ACTI1ATION space .
C Re<,ster,n< t7e ne3 e)ent 7an$ler #et7"$
SET /ANDLER "re62->.2 OR "re61 .
C Call,n< t7e e)ent 37,c7 3,ll ra,se t7e e)ent.
call #et7"$ "re61->T1.
Output ! am T1$ going to raise e"ent E1
! am the e"ent handler method ;1 in c1
! am T1$ going to raise e"ent E1
! am the e"ent handler method ;. in c.
Page : 78 of 108
Examples on Object Oriented Programming in ABAP
:.. 2se o static e"ent
Theme -tatic methods can onl, raise static e"ents The 9O<Caddition is not reTuired to
register for static e"ents
Program (lass C1 contains a static e"ent E1 and static triggering method T1 The e"ent
handler method 31 is in class C1 itself
At the time of registering e"ent 31 as e"ent handler method$ the FORLaddition
is omitted
*ump
REPORT YSUBOOPSO .
CLASS c1 DEINITION.
PUBLIC SECTION.
C Creat,n< e)ent ! E1
CLASS-E1ENTS! E1.
C Creat,n< an e)ent 7an$l,n< #et7"$.
.ET/ODS! .1 OR E1ENT E1 O c1.
C .et7"$ t" ra,se t7e e)ent
CLASS-.ET/ODS ! T1.
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
C .et7"$ ! T1 3,ll ra,se t7e e)ent
.ET/OD ! T1.
3r,te!45 +I a# T18 <",n< t" ra,se e)ent E1+.
ra,se e)ent E1.
END.ET/OD.
C .et7"$ ! .1 3,ll -e calle$ 37en t7e e)ent ,s ra,se$
.ET/OD ! .1.
3r,te!45 + I a# t7e e)ent 7an$ler #et7"$ .1 ,n c1+.
END.ET/OD.
ENDCLASS.
Start-"6-select,"n.
Data! "re61 t(pe re6 t" c1.
Create "-=ect! "re61 .
C Re<,ster,n< t7e e)ent 7an$ler #et7"$
SET /ANDLER "re61->.1 .
C Call,n< t7e e)ent 37,c7 3,ll ra,se t7e e)ent.
call #et7"$ "re61->T1.
Output ! am T1$ going to raise e"ent E1
! am the e"ent handler method ;1 in c1
Page : 73 of 108
Examples on Object Oriented Programming in ABAP
:.4 E"ents #ith e)port parameters
Theme E"ents can ha"e export parameters$ %hich it passes to its e"ent handler method
The triggering method must pass "alues for all the exporting parameters of the
e"ent %hile raising the e"ent using <A!-E EAE/T statement The interface of an
e"ent handler method consists of a list of IMPORTING parameters$ %hose names
are identical %ith those in the E0PO<T!/: list and %hich are automaticall,
created from the interface of the e"ent Each handler method can ho%e"er
specif, %hich e"ent parameters it %ants to handle and %hich it does not
Program
*escr
(lass C1 contains e"ent E1 %hich exports t%o parameters $ (231 and (23* to
its e"ent handler method $ 31 in class C1 ;ethod T1 is the triggering method
for the e"ent$ %hich passes "alues to the E0PO<T!/: parameters of the e"ent
at the time of RA'%E E;E(T statement
*ump
REPORT YSUBDEL1.
CLASS c1 DEINITION.
PUBLIC SECTION.
E1ENTS ! E1 E<#RTIN$ )al*e%NU.1' TYPE I
)al*e%NU.2' TYPE I.

.ET/ODS ! .1 OR E1ENT E1 O C1
IM#RTIN$ NU.1
NU.2
.
.ET/ODS ! T1.
ENDCLASS.
CLASS C1 I.PLE.ENTATION.
.ET/OD ! .1.
0RITE!45 +,rst ,np*t + 8 n*#1 .
3r,te!45 +Sec"n$ ,np*t + 8 n*#2 .
END.ET/OD.

.ET/OD T1.
RAISE E5ENT E1 exporting n*#1 ; 2
n*#2 ; &.
END.ET/OD.
ENDCLASS.

START-O-SELECTION.
DATA ! "re6 TYPE RE TO c1.
CREATE OBDECT "re6.
SET /ANDLER "re6->.1 6"r "re6.
call #et7"$ "re6->T1.
Output 9irst input .
-econd input 2
Page : 80 of 108
Examples on Object Oriented Programming in ABAP
< Class-,ase! E)ceptions
<.1 2sing %AP pro"i!e! e)ception class
Theme Errors in a program$ %hich are detected at runtime and can be trapped$ can be
dealt %ith using -AP pro"ided standard exception+classes
Program
*escr
This program ma)es a runtime error %here a di"ision b, Eero is obser"ed 5et us
ta)e three different "ersions of the program and see the outputs
;er.(o Program Output
1
REPORT YSUBCLASS4E5CEPTION.
DATA + TYPE + 6ALUE 1.
START0OF0SELECTION.
i = i / 0.
Sh"rt D$), a# f"//"7#0
R$nt+)e err"r#
COMPUTE_INT_ZERODIVIDE
E8!e,t+"n CX_SY_ZERODIVIDE
O!!$rred "n 19.:;.9::; at 1<:918
D+=+de .2 : >t2,e 1?.

.
REPORT YSUBCLASS4E5CEPTION.
DATA + TYPE + 6ALUE 1.
START0OF0SELECTION.
!at!h #2#te)0e8!e,t+"n#
COMPUTE4INT4@ERODI6IDE A 9.
i = i / 0.
end!at!h.
+f #20#$.r! A 9.
7r+te&' (D+=+#+"n .2 Ber"CCCChe!*(.
end+f.
(D+=+#+"n .2 Ber"CCCChe!*
2
REPORT YSUBCLASS4E5CEPTION.
DATA + TYPE + 6ALUE 1.
START0OF0SELECTION.
TRY.
i = i / 0.
CATCH !84#24Ber"d+=+de.
7r+te&' (D+=+de .2 Ber" !a$ght(.
ENDTRY.
*i"ide b, Eero caught
The three "ersions basicall, represent the same program$ but sho%s ho% an error can be
trapped using -AP pro"ided standard exception class
Page : 81 of 108
Examples on Object Oriented Programming in ABAP
<.* Bhen both superclass an! subclass are use! to tracA error
Theme -AP pro"ided standard exception classes can reside in different le"els of
hierarch, treeU CG0ROOT being at the top -o$ if both superclass and its
subclass are used in a program to detect errors in T<BCE/*T<B bloc)$ the
subclass should be used first$ then the superclass
Program
*escr
This program creates a di"ision b, Eero problem =ere$ both the superclass
(0G<OOT and subclass (0G-BGME<O*!A!*E is used to trap errors
!n "ersion 1$ superclass is used first to trap the error+ %hich creates a
compilation error
!n "ersion .$ subclass is used first # %hich gets compiled and executed
successfull,
*ump Aersion (ode Output
1
REPORT YSUBCLASSAEHCEPTION.
DATA! , TYPE , 1ALUE 1.
START-O-SELECTION.
TRY.
, ; , 4 0.
CATC/ cFAr""t.
3r,te!45 +Err"r trappe$+.
CATC/ cFAs(AGer"$,),$e.
3r,te!45 +D,). -( Ger"T@.
ENDTRY.
(ompilation error :+
Exception in the (AT(=
clauses are not sorted
in ascending order
.
REPORT YSUBCLASSAEHCEPTION.
DATA! , TYPE , 1ALUE 1.
START-O-SELECTION.
TRY.
, ; , 4 0.
CATC/ cFAs(AGer"$,),$e.
3r,te!45 +D,). -( Ger"T+.
CATC/ cFAr""t.
3r,te!45 +Err"r trappe$+.
ENDTRY.
*i" b, EeroY
Page : 8. of 108
Examples on Object Oriented Programming in ABAP
<.+ Propagation o Class-,ase! e)ceptions in proce!ures to the caller
Theme (lass+based exceptions in procedures can be propagated to the caller in the
definition of the interface using the <A!-!/: addition$ if the exception is not to
be handled in the procedure

Program
*escr
!n the follo%ing program$ the piece of code %hich ma, contain error is inside a
subroutine $ -8BG(=E(NG/O But$ the error is not handled in the procedure
itself -o$ a <A!-!/: statement >%ith probable class+based exception that
might be raised b, the procedure? is specified at the definition of the procedure
itself$ and is later trapped using (AT(= statement
REPORT YSUBOOPS1O .
start-"6-select,"n.
tr(.
per6"r# s*-Ac7ec:An" *s,n< 5 .
catc7 cx=s2=>ero+i7i+e.
3r,te!45 +/ell"+ .
en$tr(.


OR. s*-Ac7ec:An" USIN9 PAPANO
RAISIN9 C<=S:=?ER'I5I'E.
pApAn" ; pApAn" 4 0 .
ENDOR.. P s*-Ac7ec:An"
Output =ello
Page : 82 of 108
Examples on Object Oriented Programming in ABAP
<.. Program can raise e)ceptions base! on %AP stan!ar! e)ception-classes
Theme
The runtime en"ironment onl, causes exceptions that are based on pre+defined classes$
%hile in ABAP programs one can use raise pre+defined as %ell as user+specific exception
classes
Program
*escr
This program %ill sho% ho% exceptions based on -AP pro"ided exception classes
can be manuall, raised =ere$ exception based on -AP exception+class
(0G-BGME<O*!A!*E is raised manuall,
*ump
REPORT YSUBOOPS1O .
$ata ! ,n*# t(pe , .
tr(.
raise exception t2pe cx=s2=>ero+i7i+e.
CATC/ cFAs(AGer"$,),$e.
3r,te!45 +EFcept,"n ca*<7t+.
en$tr(.
Output Exception caught
Page : 84 of 108
Examples on Object Oriented Programming in ABAP
<.4 Ob6ects are create! rom e)ception classes #hen error is trappe!
Theme @hen a class+based exception is trapped using T<BC(AT(=CE/*T<B statement$
objects are created from the exception class One can create the object using
(AT(= Iexception nameH !/TO Iexception class reference "ariableH statement
(0G<OOT is at the top of the inheritance tree for all -AP pro"ided exception
class and ha"e some pre+defined methods a"ailable$ %hich are adopted b, all
exception+classes
Program
*escr
The program in"ol"es a di"ision b, Eero error in the guarded section$ %hich raises
an exception on exception+class : (0G-BGME<O*!A!*E
A reference "ariable $ E<E9 %ith static t,pe referring to the exception class
(0G-BGME<O*!A!*E is used to create an object %hile using the (AT(=
statement
Once the object is created$ it can be used to manipulate some of the methods
and attributes of the class (0G-BGME<O*!A!*E$ %hich has been inherited b,
this class from (0G<OOT
*ump
REPORT YSUBOOPS1O .
$ata ! ,n*# t(pe , )al*e 5 8
$escr,p t(pe str,n< 8
pr"<na#e l,:e s(-rep,$ 8
l,nen" t(pe , .
$ata ! ere6 t(pe re6 t" cFAs(AGer"$,),$e.
tr(.
,n*# ; ,n*# 4 0.
CATC/ cFAs(AGer"$,),$e ,nt" ere6.
C Ut,l,G,n< #et7"$s4attr,-*tes *s,n< "-=ect "6 t7e eFcept,"n classes
call #et7"$ ere6-><etAteFt
rece,),n< res*lt ; $escr,p.
3r,te!45 +Na#e "6 t7e err"r trappe$ ! + 8 $escr,p.
call #et7"$ ere6-><etAs"*rceAp"s,t,"n
,#p"rt,n< pr"<ra#Ana#e ; pr"<na#e
s"*rceAl,ne ; l,nen" .
3r,te!45 +Err"r $etecte$ ,n pr"<ra#+ 8
pr"<na#e%15' 8
+l,ne n*#-er+ 8
l,nen".
3r,te!45 ere6->:ernelAerr,$.
en$tr(.
Output /ame of the error trapped : *i"ision b, Eero
Error detected in program B-8B*E5 line number 10
(O;P8TEG!/TGME<O*!A!*E
Page : 81 of 108
Examples on Object Oriented Programming in ABAP
<.9 Demo on &ocally Deine! E)ception-Class
Theme One can create his o%n exception+class locall, in a program and raise exceptions
related to his o%n class This program %ill sho% ho% to do that
Program
*escrip
!n this program$ an exception class CG03/0EGCEPT'O( is defined locall,$
inheriting from standard exception class (0G-TAT!(G(=E(N
;ethod 31 of class C1 raises it in the -TA<T+O9+-E5E(T!O/ bloc)$ in the
guarded section> bet%een T<B and (=E(N?$ %hich is trapped and dealt %ith
*ump
REPORT YSUBCLASSAEHCEPTIONA&.
CLASS cFA#(AeFcept,"n DEINITION
IN&ERITIN$ ,RM C<=STATIC=C&EC@.
ENDCLASS.
CLASS cFA#(AeFcept,"n I.PLE.ENTATION.
ENDCLASS.
CLASS C1 DEINITION.
PUBLIC SECTION.
.ET/ODS! #1 raising cx=)2=exception .
ENDCLASS.
CLASS c1 I.PLE.ENTATION.
.ET/OD #1.
RAISE E<CE#TIN T:#E cx=)2=exception.
END.ET/OD.
ENDCLASS.
DATA! eF TYPE RE TO cFA#(AeFcept,"n8
"re6 TYPE RE TO c1.
START-O-SELECTION.
TRY.
CREATE OBDECT "re6.
"re6->#1% '.
CATC/ cFA#(AeFcept,"n INTO eF.
3r,te!45 +.( EFcept,"n ca*<7t+.
ENDTRY.
Output ;, exception caught
Page : 86 of 108
Examples on Object Oriented Programming in ABAP
<.: (este! TR/LE(DTR/ blocA
Theme Tr,CEndtr, bloc)s can be nested The inner Tr,Cendtr, bloc) can be in the
guraded section of outer Tr,CEndtr, bloc)
Program
*escr
The program sho%n belo% uses one nested tr,Cendtr, statement
9or the inner bloc)$ the error for di"ision b, Eero is properl, caught from the
s,stem pro"ided exception+class : (0G-BGME<O*!A!*E
But$ there is a character to integer con"ersion statement specified in the inner
bloc)$ %hich creates an error of exception class (0G-BG(O/AE<-!O/G
/OG/8;BE< trapped b, the outer Tr,CEndtr, bloc)
REPORT YSUBCLASSAEHCEPTIONA&.
START-O-SELECTION.
DATA ! n*# t(pe , )al*e 5 .
TRY.
TRY.
NU. ; NU. 4 0.
CATC/ cFAs(AZERODI1IDE .
0RITE!45 +D,),s,"n -( 0 ca*<7t+.
NU. ; +SUB/ENDU+.
ENDTRY.
CATC/ cFAs(Ac"n)ers,"nAn"An*#-er.
0RITE!45 +Cann"t -e c"n)erte$ t" n*#-er+.
ENDTRY.
Output *i"ision b, 0 caught
(annot be con"erted to number
Page : 87 of 108
Examples on Object Oriented Programming in ABAP
<.< 2se o C&EA(2P section
Theme (lean up bloc) is executed %hene"er an exception occurs %ithin the T<B bloc)
and is not handled b, a (AT(= %ithin the same T<B bloc)$ but is handled b, an
surrounding T<B bloc)
Program
*escr
This program uses a nested T<BCE/*T<B statement !n the inner bloc)$ there is
an attempt to character+to+integer con"ersion$ %hich raises an error of
exception+class : (0G-BG(O/AE<-!O/G/OG/8;BE< But$ the inner T<B bloc)
does not trap the error # rather$ it is trapped b, the outer T<BCE/*T<B bloc)
8nder such circumstances$ the (5EA/8P section of the inner bloc) gets
executed first # then the (AT(= section of the outer bloc) %or)s
*ump
REPORT YSUBCLASSAEHCEPTIONA&.
START-O-SELECTION.
DATA ! n*# t(pe , )al*e 5 .
TRY.
TRY.
n*# ; +s*-7en$*+.
clean*p.
3r,te!45 +In clean*p+.
ENDTRY.
CATC/ cFAs(Ac"n)ers,"nAn"An*#-er.
0RITE!45 +Cann"t -e c"n)erte$ t" n*#-er+.
ENDTRY.
Output
!n cleanup
(annot be con"erted to number
Page : 88 of 108
Examples on Object Oriented Programming in ABAP
= ,AD's 7 ,usiness A!!-'ns8
=.1 %ingle 'mplementation o ,AD'
:o to transaction -E18 and create a BA*! *efinition
Enter the description The name of the interface %ill be automaticall, proposed *ouble
clic) on it
Page : 83 of 108
Examples on Object Oriented Programming in ABAP
Enter the name of the method ,ou %ant to create (lic) the Paramters pushbutton to create
parameters for the method
-a"e and acti"ate it
:o to transaction -E13 and create a BA*! implementation
A popup %indo% %ill as) ,ou for the definition name Enter the name of the BA*! definition
%hich ,ou ha"e created
Press Enter
Enter the description for the implementation Then sa"e and acti"ate it
Page : 30 of 108
Examples on Object Oriented Programming in ABAP
*ouble clic) on the method name
@rite the code -a"e and acti"ate it
Then$ create a code using the BA*!
Page : 31 of 108
Examples on Object Oriented Programming in ABAP
Page : 3. of 108
Examples on Object Oriented Programming in ABAP
=.* 3ultiple 'mplementation
(reate a BA*!: B-8BG:ETG;ATE<!A5-G1 in a manner similar to that created abo"e But$
chec) the chec)box for ;ultiple 8se
Then$ create one implementations for the same BA*!:+
>(ode is similar to the pre"ious one?
Then$ create a program utiliEing the BA*!:+
Page : 32 of 108
Examples on Object Oriented Programming in ABAP
/o%$ ,ou %ant to create another implementation of the same BA*! 5et us examplif, the
concept -a,$ ,ou %ant that %hen the user %ill enter L:A<!& in the selectiuon+screen$ it %ill
stand for L(A<& internall, and selection %ill be done out of ;A<A table based on material
code : L(A<& O$ ,ou define another implementation of the same BA*! from transaction :
-E13
Page : 34 of 108
Examples on Object Oriented Programming in ABAP
/o%$ %hen ,ou %ill execute the program and enter L:A<!& in the material code field in the
selection+screen$ it %ill get internall, translated to L(A<& %hen the second implementation
%ill be acti"e
Page : 31 of 108
Examples on Object Oriented Programming in ABAP
=.+ %earching or ,AD' in %AP Transaction an! 'mplementing it
There is a business demand in AB( corporation %hen the user %ill post goods receipt "ia
transaction ;!:O$ he should enter same date in document date and posting date field
Else$ an information message %ill as) the user to do that
The Bill of 5ading number should start %ith L/P&
Bou$ as a -AP Technical (onsultant$ is as)ed to translate this idea into the appropriate
section of the code
Bour manager has as)ed ,ou to use BA*! instead of an, user or field exits to implement the
idea
:o to the program behing ;!:O and search for the phrase :
(5GE0!T=A/*5E<KH:ETG!/-TA/(E in the main program This %ill sho% ,ou the BA*!s
%hich can be implemented b, ,ou to incorporate the business demand
-o$ ,ou get to )no% no% that there are t%o BA*!s %hich can come to ,our use The, are:
;BG;!:OGBA*!
Page : 36 of 108
Examples on Object Oriented Programming in ABAP
;BG;!:OG!TE;GBA*!
/o%$ ,ou ha"e to go to transaction -E18 and explore each of the BA*!s to find out the
suitable one !n fact$ the suitable one %ill ha"e a method in it for %hich import'export
parameters should ha"e some reference to document'posting dates
On in"estigation$ ,ou %ill find that in BA*! : ;BG;!:OG!TE;GBA*!$ there is a method :
!TE;G;O*!9B %hich uses : isGgohead as import parameter This has the structure
:O=EA* %hich contains the fields for document and posting date as columns
/o%$ ,ou %ill implement this BA*! :o to transaction -E13 and create an implementation for
the BA*! !n the code for the method$ %rite the follo%ing:+
method !9GE0G;BG;!:OG!TE;GBA*!X!TE;G;O*!9B
*ATA : 5G!/!>.? TBPE (
!9 isGgohead+bldat ne isGgohead+budat
message i238>00? %ith DBoth posting and document dates should be sameD
E/*!9
5G!/! K !-G:O=EA*+9<B/<Z0>.?
T<A/-5ATE 5G!/! TO 8PPE< (A-E
!9 5G!/! /E D/PD
;E--A:E !238>00? @!T= DBill of 5ading should start %ith /PD
E/*!9
endmethod
Then$ sa"e and acti"ate it Then$ perform a transaction "ia ;!:O Bour reTuirement %ill be
fulfilled
Page : 37 of 108
Examples on Object Oriented Programming in ABAP
=.. 3enu Enhancements
=...1 Deman! :
8ser %ill choose a material in the selection screen All the records for that material in
different plants and storage locs %ill be sho%n in a report 8ser %ill select a line from the
list and choose option from the menupath to migrate to transaction ;B02 to see the details
of that document number
BA*! %ith function codes %ill be used to pro"ide the menu option The underl,ing method
%ill accept the document number as an input and the method %ill call transaction ;B02 for
that document number
=...* %teps to be ollo#e!
=...*.1 %tep 1 : Create the main program in %E+<
(reate a report program from -E28 (reate a pf+ststus for including menu painter
*ouble clic) on the :8! status to create and acti"ate it
(lic) Bes to create the :8! status
Page : 38 of 108
Examples on Object Oriented Programming in ABAP
Enter a description for the :8! status and press Enter
The screen sho%n abo"e %ill appear -a"e$ acti"ate and come out
Bou %ill be na"igated to -E28 editor for the program -a"e and acti"ate it (ome out
=...*.* %tep * :- Create ,AD' Deinition rom %E1<
:o to transaction -E18 Enter the name of the BA*! to be defined and press (reate
pushbutton
Page : 33 of 108
Examples on Object Oriented Programming in ABAP
Enter the description (hec)+off the chec)box for ;ultiple use BA*! The tabstrips for
function code>9codes? %ill appear (lic) on that to go to appropriate tab+page
Enter the name of the program Enter the name of the function code$starting %ith LZ& %hich
%ill be used later in program for BA*! Enter description Then$ double clic) on the
program name
(lic) /es
Page : 100 of 108
Examples on Object Oriented Programming in ABAP
-a"e the BA*! definition
enter t%o lines for t%o function codes:+ one for exiting out of the program$another for the
details on document numberPress Enter
The screen sho%n abo"e %ill appear *ouble clic) on the line for ZM*O(
Enter the details for function code ZM*O(
Then $ double clic) on the line for function code : E0!T
Page : 101 of 108
Examples on Object Oriented Programming in ABAP
(omplete entr, for that too
Then$ sa"e and acti"ate the menu painter(ome out
Bou %ill na"igate to the transaction -E18 (lic) on the tab page : !nterface
An interface name is proposed b, the s,stem *ouble+clic) on that
Enter the name of the method %hich %ill be acti"ated %hen the function code related to
BA*! %ill be acti"ated Then$ clic) on the pushbutton : Parameters
Page : 10. of 108
Examples on Object Oriented Programming in ABAP
Enter the details for import'export parameters of the method
Then$ chec) and acti"ate the BA*! definition
=...*.+ %tep + :-'mplement the ,AD' rom transaction %E1=
:o to transaction -E13 and create an implementation for the BA*! ,ou ha"e defined
Enter the name of the BA*! definition
Enter the description for the BA*! implementation
Page : 102 of 108
Examples on Object Oriented Programming in ABAP
Then$ in the table control$ fill in the details for function code ZM*O(
Then$ go to tab page for: !nterfaces
Then$ chec) and acti"ate the BA*! implementation
*ouble clic) on the method name to %rite code for it
@rite the code (hec) and acti"ate it
(ome outOnce again$ chec) and acti"ate the implementation and come out of -E13
Page : 104 of 108
Examples on Object Oriented Programming in ABAP
=...*.. %tep .:- Place the unction co!es properly in the menu painter
Edit the report program *ouble clic) on the :8! status
;aintain the t%o function codes at appropriate place (hec)$ acti"ate and come out to -E28
editor
Page : 101 of 108
Examples on Object Oriented Programming in ABAP
=...*.4 %tep 4:- Complete the program !etails
CQ---------------------------------------------------------------------C
CQ Rep"rt YSUBBADIC0001 C
CQ C
CQ---------------------------------------------------------------------C
REPORT YSUBBADIC0001 .
C Create a 9UI stat*s 6"r t7e pr"<ra#
SET P-STATUS +YSUBBADIC0001+.
C LOAD T/E DEINITION O T/E 9LOBAL CLASS
CLASS CLAEHIT/ANDLER DEINITION LOAD.
C Declare an ,nter6ace re6erence )ar,a-le 6"r t7e ,nter6ace *se$ ,n t7e
C BADI
$ata ! lA-a$, t(pe re6 t" YIAEHASUBBADIC0001AD.
$ata ! 3A#-lnr l,:e #se<-#-lnr.
C Declare t(pes
TYPES ! BE9IN O TYPADOC 8
.BLNR LIEE .SE9-.BLNR 8 PD"c*#ent n*#-er
.DA/R LIEE .SE9-.DA/R 8 P,scal (ear
-3art LIEE .SE9-B0ART 8 P.")e#ent t(pe
0ERES LIEE .SE9-0ERES 8 PPlant
L9ORT LIEE .SE9-L9ORT 8 PSt"ra<e l"cat,"n
S/EZ9 LIEE .SE9-S/EZ9 8 PDr4Cr In$,cat"r
C/AR9 LIEE .SE9-C/AR9 8 PBatc7
.EN9E LIEE .SE9-.EN9E 8 PUt(
END O TYPADOC.
DATA ! IADOC TYPE STANDARD TABLE O TYPADOC 0IT/ /EADER LINE.
PARA.ETERS ! PA.ATNR LIEE .SE9-.ATNR OBLI9ATORY.
START-O-SELECTION.
SELECT #-lnr PD"c*#ent n"
#=a7r P,scal (r.
-3art P.")e#ent t(pe
3er:s PPlant
l<"rt PSt"ra<e L"c
s7:G< PDr4Cr ,n$,cat"r
c7ar< PBatc7
#en<e PU*ant,t(
6r"# #se<
INTO TABLE ,A$"c
37ere #atnr ; pA#atnr.
END-O-SELECTION.
I NOT ,A$"cVW IS INITIAL.
l""p at ,A$"c.
3r,te!45 ,A$"c-#-lnr r,<7t-=*st,Ke$8
20 ,A$"c-#=a7r r,<7t-=*st,Ke$8
2L ,A$"c--3art r,<7t-=*st,Ke$8
&L ,A$"c-3er:s r,<7t-=*st,Ke$8
2J ,A$"c-l<"rt r,<7t-=*st,Ke$8
5& ,A$"c-s7:G< r,<7t-=*st,Ke$8
5M ,A$"c-c7ar< r,<7t-=*st,Ke$8
JJ ,A$"c-#en<e le6t-=*st,Ke$.
en$l""p.
en$,6.
TOP-O-PA9E .
0RITE!45 +D"cN"+ 8
20 +,s.Yr+ 8
Page : 106 of 108
Examples on Object Oriented Programming in ABAP
2M +.)t.T(p+ 8
&M +Plant+ 8
25 +StL"c+ 8
52 +Dr4Cr+ 8
5M +Batc7+ 8
J5 +Ut(+ .
3r,te!45 s(-*l,ne%O0'.
AT USER-CO..AND.
CASE SY-UCO...
0/EN +EHIT+.
LEA1E PRO9RA..
0/EN +BZDOC+.
C Re<,ster 6"r t7e BADI
CALL .ET/OD CLAEHIT/ANDLER;>9ETAINSTANCE
EHPORTIN9 eF,tAna#e ; +YSUBBADIC0001AD+
n*llA,nstanceAaccepte$ ; +H+
C/AN9IN9 ,nstance ; lA-a$,.
I NOT lA-a$, IS INITIAL.
C Rea$ t7e $"c*#ent n" 6"r t7e c*rrent l,ne an$ pass ,t t" t7e #et7"$
READ CURRENT LINE IELD 1ALUE IADOC-.BLNR INTO 0A.BLNR.
CALL .ET/OD LABADI->S/O0ADETAILS EHPORTIN9 DOCNR ; 0A.BLNR.
en$,6.
en$case.
=...+ 3o!e o E)ecution
Execute the program %ith a material code
The output sho%n abo"e %ill appear -elect a document number and press the pushbutton in
the application toolbar
Page : 107 of 108
Examples on Object Oriented Programming in ABAP
!t %ill ta)e ,ou to transaction ;B02 for that document number> as per the method behing
the function code implemented b, BA*!? Press Enter to see the details
Page : 108 of 108

S-ar putea să vă placă și