Object Oriented ABAP Tutorials with Examples - 3, welcome to SAP ABAP Interview Questions.
Here we sapabapiq.com presenting third post in the series of OOABAP Tutorials with examples. Before going to read this article please refer previous posts in this series of Object Oriented ABAP programming tutorials with examples.
Read
Object Oriented ABAP Turorial with Examples - 1
Read
OOABAP Programming Tutorial with Examples - 2
Object Oriented ABAP Tutorial with Examples - 3
Place to put non-declarative statements |
|
Theme
|
For a class, the IMPLEMENTATION section can immediately
follow the class DEFINITION section.
If this is so, then all the
non-declarative statements ( viz., processing statements outside any class
definition/ implementation ) should be placed under a processing block, such
as START-OF-SELECTION.
|
Program description
|
This program contains a class C1
with a method M1.
In version 1, the IMPLEMENTATION
part of the class follows the class definition section.
But, the
non-declarative statements are not placed under any block. This creates a
compilation error.
In version 2, the non-declarative
statements are not placed under the block START-OF-SELECTION. It gets
correctly compiled.
This
demonstrates the theme.
|
REPORT YSUBDEL .
class c1 definition.
public section.
methods : m1 .
endclass.
class c1 implementation.
method : m1 .
write:/5 'I am method m1'.
endmethod.
endclass.
START-OF-SELECTION.
data : obj1 type ref to c1 .
create object obj1.
call method obj1->m1.
----------------------------------------
REPORT YSUBDEL .
class c1 definition.
public section.
methods : m1 .
endclass.
class c1 implementation.
method : m1 .
write:/5 'I am method m1'.
endmethod.
endclass.
data : obj1 type ref to c1 .
create object obj1.
call method obj1->m1.
Version 1: Incorrect
Version 2 : Correct
|
|
Output of the program
|
Version 1 creates compilation error.
Version 2 gets correctly compiled.
|
Use of Field Symbols in Class |
|
Theme
|
Field Symbols can be used to contain value of any variable
in a class.
|
Program Description
|
The program uses a field symbol, <FS>. It handles
the values of instance attribute, inum and static attribute , onum.
|
Dump of the Program
|
REPORT YSUB_ASSIGN_FS.
FIELD-SYMBOLS : <FS> TYPE
ANY .
class c1 definition .
public section .
* Instance attribute : inum
declared below
data : inum type i value 5 .
* static attribute onum declared
below
class-data : onum type i value 10 .
endclass.
class c1 implementation.
endclass.
start-of-selection.
data : oref1 type ref to c1 .
create object oref1.
* Assigning instance attribute to
field symbol <fs>
assign oref1->inum to <fs> .
write:/5 <fs> .
* Assigning static attribute to
field symbol
assign oref1->onum to <fs> .
write:/5 <fs> .
assign c1=>onum to <fs> .
write:/5 <fs> .
|
Output
|
5
10
10
|
Use of Static Attributes |
|
Theme
|
This program will demonstrate that : Static sttributes of a class are
retained throughout the entire runtime. All the objects within a class can
access its static attributes.
|
Program Description
|
The program contains a class C1 with static
attribute : NUM . The method : M1 increments the static
attribute by 1 and displays the value each time it is called.
In the main START-OF-SELECTION portion, two objects : OBJ1 and OBJ2
are created from class C1.
First, static attribute : NUM is changed and
accessed outside the class using the class component selector , ‘=>’.
Then, both objects OBJ1 and OBJ2 are used to
call method : M1 which shows the new value of static attribute : NUM
.
That the value of the static attribute gets incremented
each time when the method M1 of different objects is called shows that
this variable is able to retain its value through the entire runtime.
|
Dump of the program
|
report
ysubdel.
CLASS
c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I .
METHODS : M1.
ENDCLASS.
CLASS
c1 IMPLEMENTATION.
METHOD m1 .
num = num + 1.
write:/5 num .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
c1=>num = 3.
write:/5 c1=>num .
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C1 .
CREATE OBJECT : OREF1 ,
OREF2 .
CALL METHOD OREF1->M1 .
CALL METHOD OREF2->M1.
|
Output
|
3
4
5
|
We'll update next post in coming days on OOABAP Tutorials with Examples, keep visiting www.sapabapiq.com.
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.