1.ABAP Object Oriented Programming
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
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).
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.
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
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
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.
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.
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.
2.1 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 <class name>.
Here obj_a is a reference variable of type class <class name>
- Creating the object
create object : obj_a.
Now obj_a refers to an object of class <class name>
- Clear obj_a.
Now the reference variable obj_a no more refers to the object of <class name>.
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.
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.
Super class a-----------fall()
Super class a-----------rise()
Super class a-----------fall()
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.
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.
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.
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.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.