ABAP OOPS
ABAP OOPS
SR TRAININGS
HYDERABAD
ABAP – OOPS Classes
What is Class:
A class is a user defined data type with attributes, methods, types, events etc … for a particular
business entity or business scenario.
• Global class → whenever we created a class in SE24 that class we will call it as global class
i.e., we can call this class in any other class or program or FM etc ….
• Local class → whenever we created a class in SE38 that class we will call it as local class i.e.,
we can only use that class only in SE38 program, we can’t be able to use outside of that
SE38.
• Class definition → here we will provide the definition of method, global parameters (Types,
Data and constant declarations)
o Syntax:
o Class <Name of the class> DEFINITION.
ENDCLASS.
• Class implementation → here we will implements the methods which we are created in the
definition by using the global data.
o Syntax:
▪ CLASS <name of class> IMPLEMENTATION.
ENDCLASS.
What is an Object:
• Objects are nothing but the instances of the classes.
• Each object has a unique identity i.e., it will have its own memory and attributes
• To create an object, we will use a key word called as “CREATE OBJECT <Object name>”.
• Once the object is created, we must instance the object to the class, so that I will get the
unique memory.
o The syntax:
DATA: GO_<Des> type ref to <class name >
Create object go_<des>.
1|P ag e
• Interfaces
o Its also a one of type of class where we will create only the definition and the
implementation, we will do it in class where we are using interfaces.
• Attributes
o Here we will declare the data declaration and constant declarations
• Methods
o It will contains the block of abap code, some functionality for the class
o These are similar to like your function module
o In the methods we can access all the attributes of the class
o We can create our own parameter for the method similar to like your function
module
▪ Importing
▪ Exporting
▪ Changing parameters
▪ Returning parameters
o We will be defining the method in the class definition and exact code of the method
we will write it in class implementation.
o If we wish to call the method in the program then we need use statement called
“CALL METHOD”
• Events
o Events are the action that are performed by the user
o When ever we create an event, we need to link this event to a method for writing
the business logic
2|P ag e
Levels of attributes/methods:
INSTANCE component:
• The components which exist separately for each instance (for each object creation) of the
class are called instance component.
• That means for each object, the instance component will be available separately.
• The are referred by using a symbol ->
STATIC Component:
• These components that exist globally or are fixed for all the instances(objects) are called the
static components.
• The means all the objects will be referred to same memory/fixed memory
• These are referred by using a symbol =>
• If I want to create the static variable I have to a key word called as class
o Data Declarations
▪ Class-data
o Method
▪ Class-methods
CONSTANT:
• These are some fixed values which we will using the class
VISIBILITY:
• Public
o Data declared in this public session can be accessed by the class and in outside of
your classes (either it can be a child class or SE38 program or Function modules)
• Protected
o Data declared in the protected session can be access in own class or in the child
classes but we can’t able to use them outside of the class
• Private
o Data declared in this private session can be accessed only in that class we can’t able
to call in the child or outside the class.
3|P ag e
Constructors
Types of constructors:
Instance constructor:
Example program:
REPORT zr_promg1_classes_local.
METHOD get_so_number.
4|P ag e
ENDCLASS.
START-OF-SELECTION.
data(gr_local) = NEW lcl_local( ).
data(gr_local1) = NEW lcl_local( ).
gr_local->get_so_number(
IMPORTING
ev_vbeln = data(gv_vbeln)
).
WRITE:/ gv_vbeln.
gr_local1->get_so_number(
IMPORTING
ev_vbeln = gv_vbeln
).
WRITE:/ gv_vbeln.
Static constructor:
public section.
class-methods CLASS_CONSTRUCTOR .
methods CONSTRUCTOR .
methods GET_SO_NUMBER
exporting
!EV_VBELN type VBELN_VA .
protected section.
private section.
5|P ag e
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Static Public Method ZCL_FEB_187_CLASS1=>CLASS_CONSTRUCTOR
* +------------------------------------------------------------------------
-------------------------+
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method CLASS_CONSTRUCTOR.
iv_doc_type = 'ZOR'.
endmethod.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->CONSTRUCTOR
* +------------------------------------------------------------------------
-------------------------+
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method CONSTRUCTOR.
iv_vbeln = '123456'.
endmethod.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SO_NUMBER
* +------------------------------------------------------------------------
-------------------------+
* | [<---] EV_VBELN TYPE VBELN_VA
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SO_NUMBER.
ev_vbeln = iv_vbeln.
endmethod.
ENDCLASS.
Program:
6|P ag e
START-OF-SELECTION.
WRITE: gv_vblen.
WRITE: gv_vblen.
Interview questions:
Interface:
o It’s a special type of class, where we will have the methods without implementation, layman
terms we will call it has a dummy class.
o What we need to create an interface, if I want the method name should be same across all
the classes but the implementation should be different then we will go head create an
interface.
7|P ag e
In all my class the method names are same, but the implementation of the method is different
with one class to another class, this we can achieve a concept called as POLYMORPISHM.
Interview Questions:
1. How can you achieve polymorphism → we can achieve with the help of interface.
Example class:
interface ZIF_FEB_187_CLASS
public .
methods GET_SALES
importing
!IV_VBLEN type VBELN_VA
exporting
!ES_VBAK type VBAK .
endinterface.
8|P ag e
public section.
interfaces ZIF_FEB_187_CLASS .
protected section.
private section.
ENDCLASS.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS2->ZIF_FEB_187_CLASS~GET_SALES
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBLEN TYPE VBELN_VA
* | [<---] ES_VBAK TYPE VBAK
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method ZIF_FEB_187_CLASS~GET_SALES.
Polymorphism: It’s a concept in which the same method name will be behave differently in different
classes. i.e., each method of that class will have its own implementation.
Inheritance:
9|P ag e
Interview questions:
Note: In SAP single inheritance is possible where the multi-inheritance is not possible.
Method overloading: the name of the method will the same, but the parameters will be different in
different class ➔ this is not possible in SAP
Method over riding: to add the extra functionalities in the child class for the parent class method.
Example of inheritance:
class ZCL_FEB_187_CLASS1 definition
public
inheriting from ZCL_FEB_187_CLASS2
create public .
public section.
interfaces ZIF_FEB_187_CLASS .
10 | P a g e
methods GET_SALES_ORG_DATA
redefinition .
protected section.
private section.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SALES_ORG_DATA
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBELN TYPE VBELN_VA
* | [<---] EV_VKORG TYPE VKORG
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SALES_ORG_DATA.
*CALL METHOD SUPER->GET_SALES_ORG_DATA
* EXPORTING
* IV_VBELN =
** IMPORTING
** ev_vkorg =
* .
SELECT SINGLE vkorg from vbak INTO ev_vkorg WHERE vbeln = iv_vbeln.
endmethod.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SO_NUMBER
* +------------------------------------------------------------------------
-------------------------+
* | [<---] EV_VBELN TYPE VBELN_VA
* +------------------------------------------------------------------------
--------------</SIGNATURE>
11 | P a g e
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->ZIF_FEB_187_CLASS~GET_SALES
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBLEN TYPE VBELN_VA
* | [<---] ES_VBAK TYPE VBAK
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method ZIF_FEB_187_CLASS~GET_SALES.
es_vbak-vbeln = '12345'.
es_vbak-auart = 'ZOR'.
es_vbak-erdat = sy-datum.
endmethod.
ENDCLASS.
Abstract class:
o it’s a class which contains the methods with implementation as well as without implementations Or a
class which will contains at least one abstract method , this abstract method will never get implement
in the abstract class, to implement this method we need call this Abstract class in the child class this
way we can achieve inheritance.
public section.
methods GET_SALES_ORG_DATA
importing
!IV_VBELN type VBELN_VA
exporting
!EV_VKORG type VKORG .
methods GET_SALEAREA_DATA
abstract
exporting
!EX_VKORG type VKORG
!EX_VTWEG type VTWEG
!EX_SPART type SPART .
protected section.
12 | P a g e
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS2->GET_SALES_ORG_DATA
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBELN TYPE VBELN_VA
* | [<---] EV_VKORG TYPE VKORG
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SALES_ORG_DATA.
ev_vkorg = 1000.
endmethod.
ENDCLASS.
public section.
interfaces ZIF_FEB_187_CLASS .
methods GET_SO_NUMBER
exporting
!EV_VBELN type VBELN_VA .
methods GET_SALES_ORG_DATA
redefinition .
methods GET_SALEAREA_DATA
redefinition .
protected section.
private section.
13 | P a g e
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SALEAREA_DATA
* +------------------------------------------------------------------------
-------------------------+
* | [<---] EX_VKORG TYPE VKORG
* | [<---] EX_VTWEG TYPE VTWEG
* | [<---] EX_SPART TYPE SPART
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SALEAREA_DATA.
ex_vkorg = 1000.
ex_vtweg = 10.
ex_spart = 10.
endmethod.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SALES_ORG_DATA
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBELN TYPE VBELN_VA
* | [<---] EV_VKORG TYPE VKORG
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SALES_ORG_DATA.
*CALL METHOD SUPER->GET_SALES_ORG_DATA
* EXPORTING
* IV_VBELN =
** IMPORTING
** ev_vkorg =
* .
SELECT SINGLE vkorg from vbak INTO ev_vkorg WHERE vbeln = iv_vbeln.
endmethod.
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->GET_SO_NUMBER
14 | P a g e
* <SIGNATURE>--------------------------------------------------------------
-------------------------+
* | Instance Public Method ZCL_FEB_187_CLASS1->ZIF_FEB_187_CLASS~GET_SALES
* +------------------------------------------------------------------------
-------------------------+
* | [--->] IV_VBLEN TYPE VBELN_VA
* | [<---] ES_VBAK TYPE VBAK
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method ZIF_FEB_187_CLASS~GET_SALES.
es_vbak-vbeln = '12345'.
es_vbak-auart = 'ZOR'.
es_vbak-erdat = sy-datum.
endmethod.
ENDCLASS.
Final Class:
o A class which cannot used in inheriting in other classes is called as final class.
Friend Class:
o By Default, the child class cannot access the private variable of the super class
o The child class can access only public the protected variables.
o Friend is a concept which is used to access the private of the super class
o So access the private variable of the super class, the super class has to be treated the child as a friend,
then the child class can able to access the private variables of the super class.
o To do this go to the super class → under the friends table → provide the child class and press enter
and click save and activate
o Once we have done this the child class can able access the private variable of the super class.
Singleton class:
o if I need only one single instance to created at one point of time, then we will be using this singleton
class.
15 | P a g e
o the achieve the singleton class the class must be created a private, in this create a a public
constructor, this can be helpful to restrict the further creation of objects
1. A class is said to be a singleton class if it can have only one single instance.
3. Declare a public attribute with referencing to the same class, where we are declaring.
4. create a public static method with returing parameter, having the reference to the same class.
5. create an implementation and an object inside the implementation of the public static method.
6. to create the instance for the singleton class, call this public static method in any program.
Example Program:
REPORT zroops_singleton_class.
PUBLIC SECTION.
DATA: gv_name TYPE char20.
CLASS-DATA: lo_singleton TYPE REF TO lcl_singleton READ-ONLY.
CLASS-METHODS: class_constructor.
METHODS: set_name.
ENDCLASS.
METHOD: set_name.
gv_name = 'VERSIONIT'.
ENDMETHOD.
ENDCLASS.
lr_sing->set_name( ).
16 | P a g e
o Before we go ahead with topic let the basic difference the wide casting and narrow casting, basically
these two are related to in heritance concept.
o The sub class which inherits the super class would have more components than the super class this is
called a wide
o The Parent class or super class which have the smaller number of components compared to child class
this we will call as Narrow.
Narrow Casting:
o An instance of the sub class is assigned to the reference of the super class its called as narrow casting.
When we do this assignment only the inherited components can be assessed using the reference of
the super class
Wide casting:
o An instance of super class is assigned to the instance of the sub class its called as the wide casting,
When we do this assignment all the super class components and the child class components can be
used in this instance.
17 | P a g e
METHOD fasting.
WRITE:/ 'No need to run, becuase lion is on fasting today'.
ENDMETHOD.
ENDCLASS.
18 | P a g e
lo_anamial->hungry( ).
lo_lion->fasting( ).
Narrowing Cast:
19 | P a g e
I will still remember the above inheritance tree while referring casting and refer Widening cast as
Down casting ( though is is referred as up casting from release 7.0).
Coming to the topic, I use Widening cast in this blog (instead of up/down casting ) .
To understand the Widening cast, I will just explain with the simple program.
METHOD parent_method.
WRITE / ‘Called -> parent method in Parent Class!’.
ENDMETHOD. “parent_method
ENDCLASS. “lcl_parent IMPLEMENTATION
20 | P a g e
START-OF-SELECTION.
lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).
Now I am interested in child(sub) class method in my parent(super) class. Which mean when I call
the ‘parent_method’ of parent class in the above report then it should call the ‘parent_method’ of
child class( Redefined method call instead of original method call). So I do a Narrowing cast.
21 | P a g e
* Narrowing cast
lr_parent = lr_child.
Now when I execute the above report, I get the below output, which is as expected.
Now I am interested in parent(super) class method in my child(sub) class. Which mean when I call
the ‘parent_method’ of child class in the above report then it should call the ‘parent_method’ of super
class( Super class method call instead of redefined sub class method call). So I do a Widening cast.
TRY .
* Widening Cast
lr_child ?= lr_parent.
22 | P a g e
After seeing the above output, we remember that ” It is always not possible to do Widening cast as
the sub class will have more functionality compared to super class” .
So, I removed the method definition ‘child_method’ in the child class and tried Widening cast. I got
the same output!
I created an empty class definitions and tried widening cast to see the widening cast success
message 😛 .
23 | P a g e
START-OF-SELECTION.
TRY .
* Widening Cast
lr_child ?= lr_parent.
WRITE / 'Widening Cast Success!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.
Now we will see the output which we are all waiting for!
The Widening cast will always fail unless the assigning instance has the same type of instance
to which we are assigning.
So people used to tell do Narrowing cast before Widening Cast!, to have the assigning instance same
reference.
I will take the main report and do a narrowing cast before widening cast.
REPORT zkk_widening_cast_demo.
24 | P a g e
METHOD parent_method.
WRITE / 'Called -> parent method in Parent Class!'.
ENDMETHOD. "parent_method
ENDCLASS. "lcl_parent IMPLEMENTATION
METHOD parent_method.
WRITE / 'Called -> parent redifinition method in Child Class!'.
ENDMETHOD. "parent_method
METHOD child_method.
WRITE / 'Called -> child method in Child Class!'.
ENDMETHOD. "child_method
START-OF-SELECTION.
25 | P a g e
Now lr_parent has same type of lr_child as we did Narrowing cast. We will see the output:
Wow! widening cast is successful this time. But we can see that this output is same as the output of
Narrowing cast shown above (which is not what we required).
Because, we have copied the lr_child instance to lr_parent ( lr_parent = lr_child ” Narrow cast) before
widening cast. Now lr_parent has same type of lr_child and we assign lr_child ?= lr_parent which is
noting but copying child class instance to child class instance!
REPORT zkk_widening_cast_demo.
INTERFACE lif_test.
METHODS interface_method.
26 | P a g e
START-OF-SELECTION.
Output:
As using, lr_mine = lr_intf ” will give syntax error because lr_intf is not the same type of lr_mine.
27 | P a g e
REPORT zkk_widening_cast_demo.
INTERFACE lif_test.
METHODS interface_method.
ENDINTERFACE. "lif_test
METHOD lif_test~interface_method.
WRITE / 'Interface method call in Your class'.
ENDMETHOD. "lif_test~interface_method
ENDCLASS. "lcl_class2 IMPLEMENTATION
START-OF-SELECTION.
TRY .
* Widening Cast
lr_mine ?= lr_intf.
28 | P a g e
In this case the widening cast will be failed since lr_intf is not of same type as lr_mine.
We use widening cast mainly while dealing with interfaces and in case of dynamic programming.
So in this context, “Do Narrowing cast before Widening cast” makes sense without confusion!
29 | P a g e