Object Oriented ABAP (OOABAP) Tutorials-5 Working with Methods. Welcome to sap abap interview questions.
In this post of SAP OOABAP Tutorials with Examples - you will learn about Methods in OOABAP and how to work with methods and what is the functionality of methods in Object Oriented ABAP. This is the fifth post in the series of OOABAP tutorials with examples.
ABAP OO Tutorials with Examples
Before going to read this article, sapabapiq.com suggest you to read previous articles on OOABAP tutorials to better understand.
Methods and its functionality in OOABAP
1.1 Method with one import parameter/ only one non-optional parameter | 
 |
Theme 
 | 
  
This program will demonstrate different ways of calling a
  method which has   only one import
  parameter. 
This strategy is also valid for cases where a method has
  more than one import parameters,  but
  only one of them being non-optional. 
 | 
 
Program 
 | 
  
This program has a class , C1 with a method : meth1.
  This method has only one import parameter(input1). Look at the method
  implementation for details.  
The main purpose of this program is to
  demonstrate the different ways of calling a method with single import
  parameter. 
 | 
 
Code Dump 
 | 
  
REPORT
  YSUBDEL . 
 CLASS C1 DEFINITION. 
  PUBLIC SECTION. 
   DATA : NUM TYPE I VALUE 5. 
   METHODS : METH1 IMPORTING INPUT1 TYPE
  I   . 
 ENDCLASS. 
 CLASS C1 IMPLEMENTATION. 
  METHOD : METH1. 
   num = NUM * INPUT1 . 
   WRITE:/5 NUM . 
   num = 5. 
  ENDMETHOD. 
 ENDCLASS. 
 START-OF-SELECTION. 
  DATA : OREF1 TYPE REF TO C1. 
  CREATE OBJECT : OREF1. 
*
  Different ways of calling the method with one import parameter 
   CALL METHOD OREF1->METH1 EXPORTING
  INPUT1 = 4. 
   CALL METHOD OREF1->METH1( INPUT1 = 5 ). 
   CALL METHOD OREF1->METH1( 6 ). 
 | 
 
Output 
 | 
  
20 
25 
30 
 | 
 
1.2 Import parameters passed by ref. can’t be changed inside the method . | 
 |
Theme 
 | 
  
Parameters can be passed to a method as import parameters
  in two fashion:- 
v  By
  reference 
v  By
  value. 
Parameters passed by value can be
  changed internally in a method. But, parameters passed by reference cannot be
  changed in the method. 
 | 
 
Program description 
 | 
  
This program contains a class C1 with a method METH1.
  This method contains two input parameters : 
  - 
INPUT1  :
  passed by reference 
INPUT2 : passed by value. 
 The method METH1
  attempts to change INPUT1. On compilation, an error is
  displayed.  This
  establishes that input parameters passed by reference cannot be changed
  within the method. 
 | 
 
Dump 
 | 
  
REPORT
  YSUBDEL . 
 DATA : num TYPE I. 
 CLASS C1 DEFINITION. 
  PUBLIC SECTION. 
   METHODS : METH1 IMPORTING INPUT1 TYPE I 
                             value(input2)
  type i. 
 ENDCLASS. 
 CLASS C1 IMPLEMENTATION. 
  METHOD : METH1. 
   Input1 = 4. 
   write:/5 input1. 
  ENDMETHOD. 
 ENDCLASS. 
 START-OF-SELECTION. 
  DATA : OREF1 TYPE REF TO C1. 
  CREATE OBJECT : OREF1. 
   num = 3. 
   CALL METHOD OREF1->METH1 EXPORTING
  INPUT1 = 4 
                                      input2 = num. 
 | 
 
Output 
 | 
  
On compilation, an error message is generated.  
Now, instead of changing input1, change the import
  parameter input2 (passed by value)within the method.  
The program will get successfully compiled and executed. 
 | 
 
1.7 Demo on Static Method | 
 |
Theme 
 | 
  
This program will show how to declare and define a static
  method and how it can be called using class component selector. 
 | 
 
In the following program, method : TESTMETHOD is defined
  as static method and is called later using class component selector, ‘=>’. 
 | 
 |
Dump 
 | 
  
REPORT  YSUBOOPS19                              . 
data : num type i. 
class testclass definition. 
 public section. 
  class-methods :
  testmethod. 
endclass. 
class testclass implementation. 
 method : testmethod. 
  num = 5. 
  write:/5 num. 
endmethod. 
endclass. 
start-of-selection. 
 call method
  testclass=>testmethod. 
 | 
 
Output 
 | 
  
5 
 | 
 
1.8 Static methods can only use static attributes, instance methods use both | 
 ||||||||||||||||
Theme 
 | 
  
Static methods of a class can only use static attributes
  of that class. It cannot use instance attributes. But, instance methods can
  use both. 
 | 
 |||||||||||||||
Program Description 
 | 
  
The following program contains a class C1 which
  contains the following:- 
 
Both the static and instance methods are attempting to
  display values of the static and instance attributes: STNUM and INSTNUM
  . 
On compilation, an error will be generated which will
  demonstrate that static method STMETH cannot work with instance
  attribute, INSTNUM. 
 | 
 |||||||||||||||
Dump 
 | 
  
REPORT
  YSUBDEL. 
CLASS
  C1 DEFINITION. 
PUBLIC
  SECTION. 
 CLASS-DATA : STNUM TYPE I VALUE 5. 
 DATA : INSTNUM TYPE I VALUE 6 . 
 CLASS-METHODS : STMETH . 
 METHODS : INSTMETH . 
ENDCLASS. 
CLASS
  C1 IMPLEMENTATION. 
  METHOD : STMETH . 
   WRITE:/5 STNUM . 
   WRITE:/5 INSTNUM . 
  ENDMETHOD. 
  METHOD INSTMETH. 
   WRITE:/5 STNUM . 
   WRITE:/5 INSTNUM . 
  ENDMETHOD. 
 ENDCLASS. 
 START-OF-SELECTION. 
  DATA : OREF1 TYPE REF TO C1. 
  CALL METHOD c1=>stmeth . 
  CREATE OBJECT OREF1. 
  CALL METHOD oref1->instmeth. 
 | 
 |||||||||||||||
Output 
 | 
  
On compilation,you get the error:- 
Remove the line in bold in the program and compile. It
  will get successfully compiled and executed. 
 | 
 |||||||||||||||
1.11 Use of ME in methods | 
 |
Theme 
 | 
  
A method can have a variable defined within it having the
  same name as one of the attributes of the class to which the method belongs
  to. 
To clearly identify the class level attribute, the
  selector ME is used. 
 | 
 
Program Description 
 | 
  
Class TESTCLASS contains method TESTMETHOD.
  There is a variable I_NUM declared as public attribute in the class as
  well as in the implementation part of the method. 
To access the variable I_NUM at the class level
  within the method, the selector ME is used. Please see the ouputs of this
  program for better understanding. 
 | 
 
Dump 
 | 
  
REPORT  YSUBOOPS17 
class
  testclass definition. 
 public section. 
  data : i_num type i value 5. 
  methods : testmethod . 
endclass. 
class
  testclass implementation. 
 method :testmethod. 
  data : i_num type i value 2. 
   write:/5 me->i_num ,   " access variable of
  the class 
         /5     i_num .  
  " access variable of the method 
 endmethod. 
 endclass. 
 start-of-selection. 
 data : i_num type i. 
  data : my_obj type ref to testclass. 
  create object : my_obj. 
  call method my_obj->testmethod. 
 | 
 
Output 
 | 
  
5 
2 
 | 
 
OOABAP Interview Questions
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"

 Posted in:  
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.