Friday, December 6, 2013

OOABAP Tutorials with Examples-2

ooabap-tutorials-with-real-time-examples
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).













OOABAP Example 
Program Code

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.

Categories

ABAP (1) ABAP Interview Questions (112) ABAP Open SQL Statements (1) ABAP Syntax Rules (6) ABAP WORKBENCH (2) ABAP-Interview-Questions (52) ALE IDOC (6) ALE IDOC Interview Questions (6) ale-idoc (6) ALE-IDOC-Interview-Questions (19) ALV Interview Questions (5) ALV-Interview-Questions (22) BADI (2) BAPI (1) BAPI Interview Questions (1) BAPI-Interview-Questions (14) BDC (6) BDC Interview Questions (6) BDC-Interview-Questions (9) big data (2) big data interview questions (1) Classical Reports Interview Question (3) Classical-Reports-Interview-Questions (22) Conditional Statements (1) Cross Applications (3) Cross-Applications (14) Data Dictionary (22) Data Type Questins (1) Data types (1) Data-Dictionary (48) Data-Type-Questins (6) Dialog programming (5) Dialog Programming Interview Questions (4) Dialog-Programming (30) DOMAIN Interview Questions (1) Domain-Interview-Questions (8) Function Module (2) hadoop (2) hadoop interview questions (2) hdfs (1) IDoc Tutorials (6) Interactive Report Interview Questions (4) Interactive-Reports-Interview-Questions (22) Internal Tables (1) interview questions (1) Lock Object Interview Questions (1) Lock-Objects-Interview-Questions (10) Logical Database (1) Modularization Interview Questions (4) Modularization-Interview-Questions (25) Module Pool Programming (5) Module-Pool-Programming (39) modules in sap (1) Object Oriented ABAP (19) Object Oriented ABAP Interview Questions (15) object-oriented-abap (2) Object-Oriented-ABAP-Interview-Questions (34) OOABAP (9) Reports (14) Reports Interview Questions (9) Reports-Interview-Questions (19) RFC (1) RFC Interview Questions (1) RFC-Interview-Questions (14) RICEF (1) RICEF Objects (1) SAP (4) SAP ABAP (4) SAP ABAP Interview Questions (42) SAP ABAP Introduction (46) SAP ABAP Message Types (2) SAP BADI Interview Questions (2) SAP Basics (71) SAP Books (2) SAP Certification (1) SAP CONSULTANTS (5) SAP CRM (1) SAP ENHANCEMENTS (3) SAP EXITS (2) SAP EXITS ( SAP ENHANCEMENTS ) Interview Questions (1) SAP Free Books (1) SAP HR (2) SAP Lock Object (1) sap modules (2) SAP Open SQL Statements (1) SAP R/3 Architecture (4) SAP Search help (1) SAP Smartforms (1) SAP Smartforms Interview Questions (2) SAP Tables (5) SAP Tcodes (10) SAP Views (1) SAP Webdynpro ABAP (12) SAP Work Processors (2) SAP Workflow (3) SAP-BADI-Interview-Questions (11) SAP-Enhancements (39) SAP-Exits (39) SAP-Exits-Enhancements-Interview Questions (3) SAP-HANA (1) SAP-HANA-Interview-Questions (1) SAP-Smartforms-Interview-Questions (2) SAP-Workflow (3) Scripts (3) Scripts Interview Questions (2) Scripts-Interview-Questions (32) Search Help Interview Questions (1) Search-Help-Interview-Questions (9) Smartforms (1) Table Maintenance Generator (1) Table-Maintenance-Generator (4) Tables in SAP (2) Tables Interview Questions (3) Tables-Interview-Questions (3) Type Group Interview Questions (1) Type-Group-Interview-Questions (7) Variable Declaration (1) Views Interview Questions (1) Views-Interview-Questions (5) Webdynpro (12) what is big data (1)

Protected Blog

 
This blog is not affiliated to SAP AG |SAP is trademark of SAP AG |The information collected from various sources use information with your own risk.