OOABAP Tutorials with Examples-2, ABAP Object Oriented Programming Tutorials with Examples 2.Welcome to SAP ABAP Interview Questions.
ABAP Object Oriented Programming Tutorials
Here we sapabapiq.com presenting second post in the series of SAP ABAP Object Oriented Programming with Examples. Before going to read this post please read first post of this series, so that you can understand very clearly without any ambiguity.
Read Here for :
ABAP Object Oriented Programming Tutorials with Examples 1
After reading first post, continue with this second post here:
ABAP Object Oriented Programming Tutorials with Examples 2
Local
Class can understand data and types in the global area of the program
|
|
Theme of OOABAP program
|
This program will demonstrate the following:-
* Different
attributes of a class can be constructed utilizing the data and types
declared outside the class, in the global area of the program.
* Data
declared in the global area of the program can be used directly in a class.
|
OOABAP Program description
|
The global section of this program contains a type : TYP_TAB
and an integer variable , NUM1.
These type and data are used while defining attributes L_NUM1(integer) and IT_TAB (internal table) for
class C1 . Also, the global data L_NUM is used directly inside
the program.
This demonstrates the theme.
|
OOABAP Program Code
|
REPORT YSUBDEL1 LINE-SIZE
120.
TYPES : BEGIN OF TYP_TAB
,
NAME(15) TYPE C ,
AGE TYPE I ,
END OF TYP_TAB .
DATA : num1 type i value
5 .
CLASS c1 DEFINITION .
public section.
methods : meth1 .
DATA : l_num like num1
,
it_tab type
standard table of typ_tab ,
w_tab like line of it_tab.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
method : meth1 .
data : l_cnum(2) type
c.
l_num = 0.
do 5 times.
l_num = l_num + 1.
l_cnum = l_num.
concatenate
'Student-'
l_cnum
into w_tab-name.
w_tab-age = num1 *
l_num .
append w_tab to
it_tab.
clear w_tab.
enddo.
loop at it_tab into
w_tab.
write:/5 w_tab-name ,
w_tab-age.
endloop.
endmethod.
endclass.
START-OF-SELECTION.
DATA : obj1 type ref to
c1.
create
object : obj1.
call method
obj1->meth1.
|
Output of the Program
|
Student-1 5
Student-2 10
Student-3 15
Student-4 20
Student-5 25
|
Class
can be instantiated within implementation of another class
|
|
Theme of the OOABAP Program
|
This
program will demonstrate that an object can be created from a class( (which
was created with no CREATE PRIVATE|PROTECTED option at the time of its
definition) in the implementation section of another class.
|
OOABAP Program
Description
|
This program
contains two classes – CLASS1 and CLASS2 .
Class CLASS1
contains method : METHOD1 which displays value of some integer
variable.
Class
CLASS2 contains method : METHOD2 . In the method implementation
, an object is created from class : CLASS1 and then that object is
used to call method METHOD1.
This demonstrates that object can be created from a
class(CLASS1) within implementation section of another class(CLASS2).
|
|
REPORT
YSUBOOPS17
.
class class1 definition.
public section.
methods : method1 .
endclass.
class class2 definition.
public section.
methods : method2 .
endclass.
class class1 implementation.
method :method1.
data : i_num type i value 2.
write:/5 i_num.
endmethod.
endclass.
class class2 implementation.
method : method2.
data : obj1 type ref to class1.
create object obj1.
call method obj1->method1.
endmethod.
endclass.
start-of-selection.
data : my_obj type ref to class2.
create object : my_obj.
call method my_obj->method2.
|
Output of the program
|
2
|
Deferred
Definition of a Class
|
|
Theme of the program
|
This program will demonstrate how one can refer to a class
without defining the class before that point. But, the class has to be
defined later on.
|
OOABAP
Program description
|
In this program , class C1 has an interface
reference O2 declared with reference to class C2. But, before
that, class C2 is not defined. It is defined later with a single
public attribute , NUM .
This demonstrates the theme.
In the main section of the program, object : OBJ1
is created from class C1.
Then, an object is created from
the reference variable O2 in class C1. Finally, the attribute
num of that object is displayed.
|
Source code of the OOABAP Program
|
report
ysubdel1.
CLASS
C2 DEFINITION DEFERRED.
CLASS
C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2.
ENDCLASS.
CLASS
C2 DEFINITION.
public
section.
data : num type i value 5.
ENDCLASS.
start-of-selection.
data : obj1 type ref to C1.
CREATE OBJECT obj1.
create object obj1->o2.
write:/5
obj1->o2->num .
|
Output of the program
|
5
|
Read continuation of SAP ABAP Object Oriented Programming series third post in next article.
"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.