Showing posts with label ABAP OOPS. Show all posts
Showing posts with label ABAP OOPS. Show all posts

Wednesday, 26 December 2012

Constructor in Classes

Usage of Constructors in Classes

There are 2 types of constructors in an ABAP CLASS.
  1.  CONSTRUCTOR: The constructor is a method which is used to initialize the class members or assign value to them. It is called once for each instance of an class or more precisely it is called for each object of a class.
  2. CLASS CONSTRUCTOR: The class constructor is called once for each class irrespective of the number of objects created or instantiated for that class.

Let us look into this concept with the help of an example:

EXAMPLE:

REPORT z_constructor.

*--Class Definition
CLASS l_test DEFINITION.
     PUBLIC SECTION.
         METHODS:  constructor.
         CLASS-METHODS: class_constructor.
ENDCLASS.

*--Class Implementation.
CLASS l_test IMPLEMENTATION.
     METHOD constructor.
        WRITE : / ' inside constructor '.
     ENDMETHOD.
     METHOD class_constructor.
        WRITE : / 'inside class constructor'.
     ENDMETHOD.
ENDCLASS.

*--Report.
*--Declare the reference variables.
DATA  : ob1 TYPE REF TO  l_test,
              ob2 TYPE REF TO  l_test.

START-OF-SELECTION.
CREATE OBJECT ob1.
CREATE OBJECT ob2.

------------------------------------------------------------------------------------------------------------
OUTPUT:

inside class constructor                          <-- from class constructor

inside constructor                                  <-- from constructor for object ob1

inside constructor                                  <-- from constructor for object ob2
------------------------------------------------------------------------------------------------------------

Thursday, 13 December 2012

Declaring Local Class in ABAP Report

In this tutorial we will learn about how to declare local class and use it in a report program.

KEY POINTS:
  • The first step is to give a class definition.
  • Next implement the class and its methods.
  • Declare a reference to the class.
  • Create an object of the class and use it to call the methods of the class.
EXAMPLE:
REPORT  zlocal_class.
*--Class Definition
CLASS l_test DEFINITION.
   PUBLIC SECTION.
*--data declaration
     DATA: info TYPE string.
*--methods
     METHODS:
     constructor     IMPORTING p_balance TYPE dmbtr,
     set_balance    IMPORTNG  p_set TYPE dmbtr,
                           EXPORTING p_amount TYPE dmbtr,
     display.
  PRIVATE SECTION.
    DATA : balance TYPE dmbtr.
ENDCLASS.      " end of class definition
*--Class Implementation
 CLASS l_test IMPLEMENTATION.
    METHOD constructor.
      WRITE: / 'inside constructor'.
       balance  = p_balance.
      WRITE: /  'value'  ,  balance.
    ENDMETHOD.
    METHOD set_balance.
       balance = p_set.
    ENDMETHOD.
   METHOD display.
      WRITE : / ' set the balance'.
      WRITE :  balance.
   ENDMETHOD.
ENDCLASS.

*--Report 
*-- Declare the reference.
 DATA:  test TYPE REF TO  l_test.

*declare a parameter on selection screen.
PARAMETERS: p_val TYPE dmbtr.

START-OF-SELECTION.
*--create object
CREATE OBJECT test 
    EXPORTING 
     p_balance =  40.                          <-- This will be passed to the constructor.

*-- call method set_balance.
CALL METHOD test->set_balance  <-- call the method to set balance.
    EXPORTING
      p_set = p_val.
*-- call method display.
CALL METHOD test->display.        <-- call method display.

  ----------------------------------------------------------------------------------------------------------

INPUT :
 P_VAL  = 500.

OUTPUT:
inside constructor
value 40

set the balance 500
-----------------------------------------------------------------------------------------------------------
Notes :  
  • The method constructor is called when the object is created using CREATE OBJECT statement.
  • The method set_balance s used to set the value or assign the value of parameter to the class attribute balance.
  • The attribute 'balance' is private and hence can only be accessed through class methods.
  • The method display will print the value of balance attribute.