Monday, February 7, 2011

Binding in ABAP Object Oriented Programming

Some basic terminologies

1.1 ABAP Objects

ABAP objects is the term given to object oriented programming done in ABAP. This programming model unites data and functions. OO ABAP is built on existing ABAP language. ABAP objects are run in same environment as the normal ABAP programs. OO ABAP is part of ABAP since R/3 release 4.0 .

1.2 Class

Class is a prototype that defines data and the behaviour common to all the objects of certain kind. Here methods provide the behaviour. We can say classes describe objects.

Classes can be declared either globally or locally. Global classes can be declared using transaction SE24. Local classes are declared in an ABAP program (reports etc).

1.3 Objects

It signifies the real world. Technically we can say objects are instances of a class. We can create any number of objects from a class template. All the objects created have unique identity and each contain different set of attributes. Objects we create in a program exist only till the program exists.

1.4 Encapsulation

Through encapsulation we restrict the visibility of attributes and methods in the object.

There are three levels of visibility in OO ABAP.

  • Public
  • Protected
  • Private

1.5 Polymorphism

The name of method is same but they behave differently in different classes. It means implementation of method (i.e. body of the method) is different in different classes. It can be achieved in two different ways in OO ABAP.

  • Interfaces
  • Overriding methods or redefining methods in each class after inheritance

1.6 Inheritance

In OO ABAP we use an existing class to derive a new class (child class). The new class contains the attributes of the parent class (derives according to the visibility of attributes and methods) in addition to new attributes and methods added to it. The child class derives all the attributes and methods declared in parent class as public visibility. The child class cannot inherit private members. The protected members in parent class are derived in child class but their visibility changes to private.

1.7 Interfaces

Interfaces are similarly defined as classes. They also contain attributes and methods. But interfaces do not have implementation part. Their methods are implemented in the class that implements the interface.

So we provide different implementation to the methods defined in the interface in different class that implements that interface. This way polymorphism is achieved.

1.8 Binding in Object Oriented languages

It is the concept which determines the object’s method to be invoked based on the signature or parameters provided. It is important in case of invoking a method, which is redefined in subsequent sub classes. As the level of hierarchy increases and the same method is redefined in subclasses we need to know which method is called. When this decision is made at run time it is called as Dynamic binding. When this decision is made at compile time it is known as Static binding. In Java this concept is implemented using concept of method overriding and in C++ there is concept of virtual functions. They help in achieving dynamic binding. Similarly in OO ABAP we can redefine methods and use concept of binding to invoke methods as per required.

Binding in OO ABAP

Suppose we need to redefine or override a method of a class in all sub classes inheriting from it. We can take a pointer or reference variable to the base or parent class. This parent class reference variable can take or refer to objects of the sub classes. Now to decide that which method will be called upon while we are using this parent class reference variable, we need to know about the concept of binding.

  • We define a reference variable as

Data : obj_a type ref to .

Here obj_a is a reference variable of type class

  • Creating the object

create object : obj_a.

Now obj_a refers to an object of class

  • Clear obj_a.

Now the reference variable obj_a no more refers to the object of .

In this case garbage collector will come and remove the object from memory.

2.1 Example

Let us create a report and some local classes and look into concept of binding

Create a report zpmm_class_dynamic.


*&----------------------------------------------------------------*
*& Report ZPMM_CLASS_DYNAMIC *
*& *
*&----------------------------------------------------------------*
REPORT  ZPMM_CLASS_DYNAMIC                      .
*----------------------------------------------------------------*
* CLASS a DEFINITION
*----------------------------------------------------------------*
CLASS a DEFINITION.
PUBLIC SECTION.
methods : rise,
fall.
ENDCLASS.                    "a DEFINITION
*----------------------------------------------------------------*
* CLASS a IMPLEMENTATION
*----------------------------------------------------------------*
CLASS a IMPLEMENTATION.
  METHOD rise.
   write : / 'Super class a --------- rise()'.
  ENDMETHOD.                     "rise
  METHOD fall.
write : / 'Super class a --------- fall()'.
  ENDMETHOD.                    "fall
ENDCLASS. "a IMPLEMENTATION
*----------------------------------------------------------------*
* CLASS b DEFINITION
*----------------------------------------------------------------*
CLASS b DEFINITION inheriting from a.
PUBLIC SECTION.
methods : rise redefinition,
xyz.
ENDCLASS.                    "b DEFINITION
*----------------------------------------------------------------*
* CLASS b IMPLEMENTATION
*----------------------------------------------------------------*
CLASS b IMPLEMENTATION.
  METHOD rise.
   write : / 'Child class b redefined --------- rise()'.
  ENDMETHOD.                    "rise
  METHOD xyz.
     write : / 'Child class b new method --------- xyz()'.
  ENDMETHOD.                    "xyz
ENDCLASS.                    "b IMPLEMENTATION
********End of Class Definition and implementations***************
***Global declaration
***Creating reference variables for the classes defined above
data :
*Reference variable of type class a
obj_a type ref to a,
*Reference variable of type class b
obj_b1 type ref to b,
*Reference variable of type class b
obj_b2 type ref to b.
***********************************
******************************************************************
* START-OF-SELECTION
*******************************************************************
START-OF-SELECTION.
create object : obj_a,
obj_b1,
obj_b2.
******************************************************************
* END-OF-SELECTION
*******************************************************************
END-OF-SELECTION.
 call method : obj_a->fall,
obj_a->rise,
obj_b1->fall.

Now output of above code is :

Super class a-----------fall()

Super class a-----------rise()

Super class a-----------fall()

We will just discuss how we got this output and what will happen when we assign subclass objects to reference variables of parent class.

2.2 Binding

We have reference variables

obj_a , obj_b1 ,obj_b2

Further we created object obj_a (refers to object of class a) and obj_b1(refers to object of class b) using create object statement.

When we assign

obj_a = obj_b1.

Then both obj_a and obj_b now refer to same object of class b.

But obj_a is reference variable of type parent class of class b.

Now when

obj_a = obj_b .

Reference variable is of type Base Class

Object passed is of type Sub Class.

When we will use the reference variable obj_a to invoke method rise() which is overridden in sub class b, the sub class b method rise() (redefined method) is invoked.

So if we change the code below START-OF-SELECTION event and END-OF-SELECTION event in section 2.1 to check the above theory.

*****************************************************************
* START-OF-SELECTION
******************************************************************
START-OF-SELECTION.
create object : obj_a,
obj_b1,
obj_b2.
obj_a = obj_b1.
*****************************************************************
* END-OF-SELECTION
******************************************************************
END-OF-SELECTION.
 call method : obj_a->fall,
obj_a->rise,
obj_b1->fall.
Now output of above code is :
Super class a-----------fall()
Child class b redefined-----------rise()
Super class a-----------fall()

2.3 Binding Check Table

I have prepared a table to check the method invoked in case of inheritance. This table is used to check the method invoked when the method is redefined in sub classes.

Reference Variable

Object

Method Invoked

Base Class

Base Class

Base Class

Base Class

Sub Class

Sub Class

Sub Class

Sub Class

Sub Class

Note: We can not take a reference variable of Sub Class to refer a Base class object.

obj_b1 = obj_a. is not possible

We can now verify the output of code given in section section 2.1.

2.4 Important in Binding

Till now we have seen which method is called when we use reference variable of base class and pass the object of sub class. But there is some restrictions on calling methods.

When obj_a = obj_b.

When reference variable is of base class i.e obj_a

And object referred by obj_a is of type subclass i.e. obj_b.

In this case base class reference variable can only call the methods which are defined there in the base class.

We can not invoke the new method defined in the class b xyz() using base class obj_a reference variable.

obj_a = obj_b.

call method : obj_a->xyz. Is not possible. It will throw an error.

In this case we can only call

call method : obj_a->fall,

obj_a->rise.

When we call obj_a->fall , it will call the method of base class since it is not redefined in sub class b.

When we call obj_a->rise, it will call the method of sub class since it is redefined in sub class b. For this we can use the table of section 2.3.

2.5 Output of section 2.1

We can now verify the output of code given in section 2.1 from the table described in section 2.3

We will just see the START-OF-SELECTION and END-OF-SELECTION events below from section 2.1

******************************************************************
* START-OF-SELECTION
*******************************************************************
START-OF-SELECTION.
create object : obj_a,
obj_b1,
obj_b2.
******************************************************************
* END-OF-SELECTION
*******************************************************************
END-OF-SELECTION.
 call method : obj_a->fall,
obj_a->rise,
obj_b1->fall.
Now output of above code is :
Super class a-----------fall()
Super class a-----------rise()
Super class a-----------fall()

Here obj_a refers to base class object so it only calls base class methods rise() and fall().

Since method fall() is not redefined in class b and is just inherited from class a , so when we callobj_b1->fall, the base class method is invoked.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites