OOPS ABAP Notes – 1

Hello Dear Learners, if you want to learn OOPS ABAP step by step concepts | OOPS ABAP Local class Global Class Notes | OOPS ABAP Tutorial for Beginners | OOPS ABAP in SAP ABAP Notes | OO ABAP Notes | Features of OOPS ABAP then you will learn here.

In OOABAP, everything we can write in form of Class and Methods and call through object. Class is the blueprint or template of an object. Object is the real one.

Example – If you want to build a form house then we take a plan from engineer to build the form house. It’s nothing but class. Based on the plan constructed house is an object.

Note: – Based on one class we can create any number of objects.

There are two types of classes.

  1. Local Class
  2. Global class

Differences between Local & Global classes.

A class contains two sections.

  1. Class Definition
  2. Class Implementation
    A class definition contains components. Components means Attributes, Constants, Methods, Events, Interfaces.

Attributes: – Attributes are used to declare the variables, work areas, internal tables which are needed to implement the logics.
Constants: – Constants will contain fixed value which will not change entire class.
Methods: – Method is the collection of statements which perform the particular activity. Methods are coding blocks of a class, which can provide some business functionality
Events: – Event is an action which is performed at run time. Events are used to provide the dynamic features at run time. Events are used to handle the methods of some other class.
Interface: – Interface is the collection of methods which are defined & not implemented.

Attributes, Methods, Events will contain in two forms.

  • Instance
  • Static

There are three types of visibility sections.

  • Public Section
  • Protected Section
  • Private Section

Public Section: – We can access the public components within the class as well as outside the class.
Protected section: – We can access the protected components within the class as well as derived or child class.
Private section: – We can access the private components within the class only.

Note: – In ABAP we haven’t default visibility section.

REPORT ZLOCAL_CLASS1. 
class lcl_test1 definition. 
PUBLIC SECTION. 
data a type i. class-data b type i. 
CONSTANTS c type i value 30. 
ENDCLASS.

write:/ lcl_test1=>b. 
write:/ lcl_test1=>c.

Component which is starting with ‘Data’ is static (attribute or method or event) and which is starting with ‘Class-data’ is Static.
Instance or Static components we can’t use directly outside of the class. By using class name, we can use Static components outside of the class and by using object we can use instance or static components outside of the class.

Instance variable and Static variable how work in background.

CLASS lcl_test1 DEFINITION. 
PUBLIC SECTION. DATA a TYPE i. 
CLASS-DATA b TYPE i. 
ENDCLASS. 

DATA obj1 TYPE REF TO lcl_test1. 
DATA obj2 TYPE REF TO lcl_test1. 
CREATE OBJECT obj1. 
CREATE OBJECT obj2. 
WRITE:/ obj1->a, obj1->b. 
ULINE. 
obj1->a = 40. 
obj1->b = 70. 
WRITE:/ ' Assign values to a, b from OBJ1'.
WRITE:/ obj1->a, obj1->b. 
ULINE. WRITE:/ 'Just calling a, b from OBJ2'. 
WRITE:/ obj2->a, obj2->b. 
ULINE. 
WRITE:/ 'Assign values to a, b from OBJ2'. 
obj2->a = 98. 
obj2->b = 99. 
WRITE:/ obj2->a, obj2->b. 
ULINE. 
WRITE:/ 'Display a, b from OBJ1'. 
WRITE:/ obj1->a, obj1->b. 
ULINE.

Initially the value for a, b will be 0 (default value for integer is 0). So in the 1st case 0, 0 is coming in the output. Then I’m initializing the values to a = 40 and b = 70. Same values are coming in the 2nd case.
First two cases I’m calling from OBJ1. I’ve created one more Object named as OBJ2. From OBJ2 calling a, b attributes.

The values will be 0, 70 is coming.
Instance variable will create separate memory for new objects. For example, I’ve created 40 objects for a class. At that time, 40 memory locations will create for Instance variable.
Static variable will create single memory in life time of class. For example, I’ve created 40 objects for a class. Only one memory location will create for Static variable.

From OBJ1 I’m assigning something value to Static variable. The same value should pick by all other objects. Instance variable value will be specific for object. In 4th case, I’m assigning values a = 89, b = 90 from OBJ2 and calling those variables. Values are displaying as expected.
In 5th case, I’m calling a, b from OBJ1. From OB1, I’ve updated instance variable updated as 10 in 2nd case. System will pick that value. For static variable ‘b’, last time updated in case 4. System will pick that value.

Methods calling which are from Private or Protected Sections

CLASS lcl_test1 DEFINITION. 
PUBLIC SECTION. 
DATA a TYPE i. DATA b TYPE i.
METHODS get_data IMPORTING i_a TYPE i OPTIONAL i_b TYPE i. 
PROTECTED SECTION. 
METHODS display_data. 
ENDCLASS. 
CLASS lcl_test1 IMPLEMENTATION. 
METHOD get_data. a = i_a. b = i_b. 
ENDMETHOD. 
METHOD display_data. 
WRITE:/ a, b. 
ENDMETHOD.
ENDCLASS. 
DATA obj1 TYPE REF TO lcl_test1. 
START-OF-SELECTION. 
CREATE OBJECT obj1. 
PARAMETERS: P_a TYPE i, p_b TYPE i. 
CALL METHOD obj1->get_data EXPORTING i_a = p_a i_b = p_b. 
CALL METHOD obj1->display_data.

Program is not activating. Because we can’t call the Private or Protected methods outside of the program. Don’t call Private or Protected methods externally. We can call these methods internally (Method calling with in another method).

METHOD get_data. 
a = i_a. b = i_b. 
call method me->display_data. 
ENDMETHOD.

In method GET_DATA implementation I’m calling one more method (Private or Protected method). System will not show any error now.
Here I’ve used ‘ME->DISPLAY_DATA’. ‘ME’ means current object. If we are calling this method from OBJ1 then in place of ME, OBJ1 will come internally.

I’ve mentioned in method declaration as below. METHODS get_data IMPORTING i_a TYPE i OPTIONAL i_b TYPE i.

Here Importing I’m mentioning. If we want to import something value from outside then we need to go for Importing parameters. i_a TYPE i OPTIONAL means, i_a is optional value. While calling this method from outside program, if we pass something value to i_a, it will pick that value otherwise it will pick the default value as 0.
For i_b, I’m not maintaining the key work as ‘OPTIONAL’ means, we need to pass something value to this variable while calling this method from outside program. Otherwise system will through error message.

PROTECTED SECTION. METHODS display_data. Protected Section or Private Section methods we can’t use outside of the class. If we keep the method in Public Section, then only we can call the method outside of the class.

Creating Global class

Execute SE24 transaction. Provide the class name, click on create button. Two radio buttons will come as class and interface. Select ‘class’ radio button click on enter button. Provide the description of the class. We can see Instant Generation as ‘PUBLIC’. Click on Enter button. Provide package, click on save button.
Now we can see the screen with a few tabs like Properties, Interfaces, Friends, Attributes, Methods, Events, Types, Aliases. By default Methods tab will open. We can declare Methods here.

We can see Method, Level, Visibility, Description here. Method means, method name we need to
mention, Level means what type of method it is like Instance or Static method. Visibility we can
select as Public, Protect, Private and we can maintain Description of method under Description.
We can see Parameters, exceptions, source code buttons also here. If we want provide any input
parameters or output parameters or changing parameters or returning parameters then we can
provide by clicking on Parameters button. By using ‘Exceptions’ button, we can add exception to
the method. By using ‘Sourcecode’ button, we can implement the method.
Give something input and output parameters as below. Click on ‘Parameters’ button.

The naming convention I’m giving like I_KUNNR means Importing Customer number and ES_KNA1
means, Exporting structure (work area) from KNA1 table, ET_SALES means Exporting Table
(Internal table). For I_KUNNR I’ve taken Associated type as ‘KUNNR’ and for ES_KNA1 I’ve taken
associated type as ‘KNA1’ total table structure will come as reference. If we want to declare
internal table, then we need to declare table type in SE11 transaction (Global table type) or Locally
(in side the Class-Types tab). Here I’ve mentioned Associated type as ‘TT_SALES’ for ET_SALES. I’ve
declared one table type locally. Click on ‘Types’ tab.

I’ve declared Type structure first based on that one, I’ve declared Table Type. Provide the Type
Structure name, visibility as ‘Public’, click on ‘ ➔ ‘ button as shown in above image. If we have any
global structure we can that name otherwise click on arrow button.
public section.

types:
BEGIN OF ts_sales,
vbeln type vbak-vbeln,
vbtyp type vbak-vbtyp,
kunnr type vbak-kunnr,
end of ts_sales .
types:
TT_SALES type table of ts_sales .

Write the code as above. Click on Save button, activate, click on back button.
Now we can see TS_SALES and TT_SALES. By using this TT_SALES table type, I’ve declare the
exporting parameters in Method ‘M1’.
Click on ‘Methods’ tab, select the method, click on ‘Source code’ button.

We can see Importing, exporting parameters here. If we are not able see these parameters, click
on ‘Signature’ button. Then we can see these parameters.
Implement the logic as below.
Now if we want to call this method, then need create one local program there we can call the
method.

PARAMETERS p_kunnr TYPE kunnr.
DATA obj TYPE REF TO ztest_t002.
START-OF-SELECTION.
CREATE OBJECT obj.
CALL METHOD obj->m1
EXPORTING
i_kunnr = p_kunnr
" Customer Number
IMPORTING
es_kna1 = DATA(ls_kna1)
" General Data in Customer Master
et_sales = DATA(lt_sales).
LOOP AT lt_sales INTO DATA(ls_sales).
WRITE:/ ls_sales-vbeln, ls_sales-vbtyp, ls_sales-kunnr.
ENDLOOP.

Parameters I am declaring, Object declaring, creating the object.
Now calling the method. Provide like ‘CALL METHOD OBJ->’ and click on CTRL + Space button. It
will show list of the methods available under that class. Select the method. Click on CTRL + Space button. Click on Shift + Enter button. It will display the Input and Output parameters if the method
contains.
Provide Importing, Exporting parameters. Save, check, activate the program, click on execute
button.

Note:- Importing parameters value we can not change in the method or in debugging.
If we want to change the importing parameter in method implementation, instead of taking
‘Importing’, need to take ‘Changing’ option. Changing will work like Import as well as Export also
(both functionalities).

In the above image, we can see the example of Changing parameter.

Inheritance in Class in SAP ABAP::

Inheritance is used to give parent and child relationship in between of classes.
Create a Class with 3 methods (Public, Protect, Private) as below. While creating the method, do
not select the ‘Final’ checkbox.

Methods I can create like this.

Note: Final Class will not support Inheritance concept..

These methods implemented. Creating one new class (child / sub class).

Click on ‘Create Inheritance’ button as shown in the above image.

Now system will ask the Super class name. Provide super class name, click on ‘OK’ button, provide
package, save the class.

Automatically Methods will come from Super class. Here we can see M1 and M2 methods only.
M3 is not coming here because M3 method is available in Private Section. Private Section Methods
can’t use outside of the class (even though in child class).
I want to add one new method in child class.

Now I’ll create object / Instance for child class and call the methods.

I’m calling methods. So written the syntax like ‘CALL METHOD OBJ->’ and I have given CTRL +
Space button. It is showing list of methods under super and sub classes. Method ‘M2’ is not
showing here. Because method ‘M2’ is in Protected Section. Protected Section Methods can be
used with in the class or subclass only. Outside of class or outside of subclass we can’t use it. That
is why it is not showing the ‘M2’ method.

data obj type ref to ZCL_INHRTC2.
start-OF-SELECTION.
create object obj.
call method obj->m1.
call method obj->m4.

Here system will not throw any error. Because Child class object / instance can access all Super
class public methods.
Method ‘M1’ is created in Super class. Now I want to define same method in Child class also.

method is in editable mode and there is not background colour. ‘M4’ method is available in the
current class.
I’m defining ‘M1’ method in sub class also.
Select the method, click on ‘Redefine Method’ button as shown in the above image.

Now method ‘M1’ text colour changed to black colour but background colour didn’t change.
Means, Method ‘M1’ is redefined in this sub class.
If we call method M1 from program then it will call from sub class only. If we want to call Super
class ‘M1’ method from sub class object, need to write the logic like this in child class method
implementation.

METHOD m1.
CALL METHOD super->m1.
WRITE:/ 'Method M1 from ZCL_INHRTC2' .
ENDMETHOD.

By using ‘CALL METHOD SUPER->M1’, System will call the super class method.

Convert local class to Global class

Execute SE24 transaction. Click on ‘Object Type’ menu button → click on ‘Import’ → click on
‘Local classes in Program’ → Provide the program name (Local contain program name). Click on
enter button. Provide Global class name, select that row, click on Import button (F5). Provide the
package. We can see one success message. Open the Global class activate it.

Constructor in SAP ABAP::

Constructor is also one method only. But it is special method. The speciality is this Constructor
method will call automatically after creating object, not required to call it externally.
Method name we need to take as ‘CONSTRUCTOR’ only. System can automatically understand like
this is special method.

I’ve taken 3 methods here.
Constructor is a special method and it is Instance Method. CLASS_CONSTRUCTOR is also a special
method and it is Static Method. ‘M1’ is normal method.
Let us create one object for this class in a program.

data obj1 type ref to ZTEST_CON.
start-OF-SELECTION.
create object obj1.

I’m just creating the object for class and execute the program.

Here Static Constructor is calling first and Instance constructor is calling next.
I’m not calling any method, just created object. At that time only these two special methods are
calling.
I’ll do one thing here.
I don’t create any object & I’ll take one static attribute in class and assign something value from
program.

I’ve mentioned ‘KUNNR’ as static attribute. Now I’ll assign something value to this attribute from
program like this.

REPORT ZTEST_CON_123.
ztest_con=>kunnr = '123'.

I’m just assigning static attribute value and click on execute button.

Automatically Static Constructor is calling internally. So Static constructor can trigger in two ways.
If we are assigning something value to static attribute it will trigger and while creating the object
static constructor will trigger. Only one time it will trigger.
But coming Instance constructor, It will trigger at the time of creating object only.
Constructor is triggering automatically then how can we pass input and output parameters?

Instance Constructor can accept Input parameters only. It will not accept any other parameters.
Static Constructor should not allow any parameters.

I’ve given the input parameters to Instance Constructor.

method CONSTRUCTOR.
cust_number = i_kunnr.
cust_name = i_name.
write:/ 'Instase Constructor from ZTEST_CON'.
write:/ cust_number, cust_name.
endmethod.

Logic I’ve written inside the Instance Constructor. Now I’ll pass the values to constructor from
program.

data obj1 type ref to ZTEST_CON.
PARAMETERS: p_kunnr type kunnr,
p_name type name1.
start-OF-SELECTION.
create object obj1 EXPORTING i_kunnr = p_kunnr
i_name = p_name.

If I execute the program, both special methods will trigger. Output will come like this.

First it is triggering Static Constructor and next Instance constructor is triggering.
As of now I’m removing the input parameters to the Instance constructor and I’m creating more
than one object for the class.

REPORT ZTEST_CON_123.
data obj1 type ref to ZTEST_CON.
data obj2 type ref to ZTEST_CON.
data obj3 type ref to ZTEST_CON.
start-OF-SELECTION.
create object obj1.
create object obj2.
create object obj3.

Output will come like this.

Only one-time Static constructor is triggering and three times Instance constructor is triggering.
Instance Constructor is specific for Object. How many number objects created, that many of times
instance constructor will trigger. But Static constructor will trigger only once in life time of the
class.
I’ll take one child class for this class and create object for child class.

Automatically constructor, Method M1 is coming from super class.
Method M2 I’ve taken in the child class & implemented as below.

Now I’ll create a program.

REPORT ZTEST_28731.
data obj1 type ref to ZTEST_CON_INH1.
start-OF-SELECTION.
create object obj1.

Automatically super class Static and Instance constructors are calling because child class object can
access the super class methods.

Let me add one Instance Constructor in the child class.

It is showing something error. If we are maintaining the Instance Constructor in Super class as well
as child class, then we need to call super class constructor externally in child class Instance
constructor implementation.

Now it is not showing any error.
Execute the program once again.

First Super class Static Constructor is triggering, then Sub class Instance Constructor is triggering,
then super class Instance constructor is triggering.
I’ll add Static Constructor in child class.

Implemented static constructor as below.
method CLASS_CONSTRUCTOR.
write:/ ‘Static Constructor from class ZTEST_CON_INH1’.
endmethod.

Execute the program once again.

Execute sequence:

  1. Super class Static Constructor
  2. Child class Static Constructor
  3. Child class Instance Constructor
  4. Super class Instance Constructor

Differences between normal method and special method (constructor).

Normal MethodConstructor
It can declare any of section (Public, Protected,Private)It can declare in only public section
It should call explicitlyAutomatically it will call. Explicit call not
required
it can any number of times by using the same
object
Instance Constructor will call in life time of
object. Static Constructor will call in life time
of class.
It can contain any type of parameters (Import,
export, changing, returning)
Instance Constructor will allow only import parameters, Static Constructors will not allow
any parameters
Can return any number of valuesNever written any value(exporting values)

Friend Class in SAP ABAP:

I’ve created class like this

3 methods I’ve mentioned with Public, Protected, Private sections. In Friends Tab, I’m giving one
class name like this.

Open the friend class – ZCL_TEST22_FR.

Implemented the method M4 as below.

method M4.
data obj type ref to zcl_test22.
create object obj.
call method obj->m1.
call method obj->m2.
call method obj->m3.
endmethod.

It will not through any error. The class – ZCL_TEST22 is containing Public, Protected and Private
methods. Protected and Private methods we can not use outside of the class. But by using friend
class, we can use any type of components in friend class.

Exceptions in Class in SAP ABAP:

Create a class like this

Take importing, exporting parameters for method M1.

Implement the method like this

METHOD m1.
i_c = i_a / i_b.
ENDMETHOD.

Create a program to pass the values.

REPORT ZTEST_28731.
data obj type ref to ZCL_TEST23.
parameters: p_a type string,
p_b type string.
start-OF-SELECTION.
create object obj.
call method obj->m1
EXPORTING
i_a = p_a
i_b = p_b
IMPORTING
i_c = data(lv_c)
.
write:/ lv_c.

Execute the program and provide input parameters.

We can the output as ‘10’.
If we provide input like this

Execute the program.

System will through dump like this. We can divide any value with ‘0’. If we divide with ‘0’, it will go
to dump. We can check all dumps in ST22 transaction.

This is called Exception. We need to handle the exceptions in method implementation level or
method calling time. We can see the exception class in the dump. Copy that exception class name.

Go to method declaration part, click on exception button, provide the exception class name.

Go to method implementation.

METHOD m1.
DATA obj_ex TYPE REF TO cx_sy_zerodivide.
TRY.
i_c = i_a / i_b.
CATCH cx_sy_zerodivide INTO obj_ex.
CALL METHOD obj_ex->get_longtext
RECEIVING
result = DATA(lv_result).
WRITE:/ lv_result.
ENDTRY.
ENDMETHOD.

I’m just using try and end try blocks.

If any exception raised, then it will handle by ‘Catch’ block. In catch block, I’m calling one method
from CX_SY_ZERODIVIDE class. It will give the actual message.
Execute program by giving p_b as ‘0’. Output will come like this

We have another methods from CX_SY_ZERODIVIDE class.

  1. GET_LONGTEXT
  2. GET_TEXT
  3. GET_SOURCE_POSITION

Each method will give some message .
If we want to handle the exception at time of calling the method, we can do like this.
In method implementation part, remove try and catch blocks as below.

Exception class name will be there in method declaration but Try and Catch blocks removed from
method implementation.

Handle the exceptions at the time of method implementation.

REPORT ztest_28731.
DATA obj TYPE REF TO zcl_test23.
DATA obj_ex TYPE REF TO cx_sy_zerodivide.
PARAMETERS: p_a TYPE string,
p_b TYPE string.
START-OF-SELECTION.
CREATE OBJECT obj.
TRY.
CALL METHOD obj->m1
EXPORTING
i_a = p_a
i_b = p_b
IMPORTING
i_c = DATA(lv_c).
CATCH cx_sy_zerodivide INTO obj_ex.
CALL METHOD obj_ex->get_text
RECEIVING
result = DATA(lv_result).
CALL METHOD obj_ex->get_longtext
RECEIVING
result = DATA(lv_result1).
CALL METHOD obj_ex->get_source_position
IMPORTING
program_name = DATA(lv_prog)
Master Program
source_line = DATA(lv_line).
" ABAP Program: Current
WRITE:/ 'By using get_text method message -', lv_result.
WRITE:/ 'By using get_longtext method message -', lv_result1.
WRITE:/ 'exception is coming in program', lv_prog, 'at line number',
lv_line.
ENDTRY.

Execute the program by giving input p_b = ‘0’.

Exception is handling at the method calling level.
Now I’ll execute the program by using p_b = ‘abc’. The number should not divide by letters. It will
go to dump. Execute the program.

We are not handling the exception for dividing with letters. We need to add exception class in
method declaration level.

Now handle the exception in method calling.

REPORT ztest_28731.
DATA obj TYPE REF TO zcl_test23.
DATA obj_ex TYPE REF TO cx_sy_zerodivide.
DATA obj_ex2 TYPE REF TO cx_sy_conversion_no_number.
PARAMETERS: p_a TYPE string,
p_b TYPE string.
START-OF-SELECTION.
CREATE OBJECT obj.
TRY.
CALL METHOD obj->m1
EXPORTING
i_a = p_a
i_b = p_b
IMPORTING
i_c = DATA(lv_c).
CATCH cx_sy_zerodivide INTO obj_ex.
CALL METHOD obj_ex->get_longtext
RECEIVING
result = DATA(lv_result1).
WRITE:/ 'By using get_longtext method message -', lv_result1.
CATCH cx_sy_conversion_no_number INTO obj_ex2.
CALL METHOD obj_ex2->get_longtext
RECEIVING
result = DATA(lv_result2).
WRITE:/ lv_result2.
ENDTRY.

Execute the program by giving p_b = ‘ABC’, output will come like this.

In one single Try and End try block we can maintain any number of catch blocks.

We can handle exceptions by using custom exception class also.
Create one custom class. The class name should start with ‘ZCX’ or ‘YCX’.

I’ve given exception class name as ZCX_TEST24, clicked on ‘Create’ button, automatically one
super class name is coming like CX_STATIC_CHECK, we can change it to CX_DYNAMIC_CHECK also.
We need to maintain any one super class name for exception class. By default the second radio
button – Exception class is selecting. As of now de select the checkbox – ‘With message of
message classes as exception texts’, and click on ‘SAVE’ button.

By default methods are coming from super class and we can see on extra tab named as ‘Texts’. We
need to provide text ids here with message. Click on ‘Texts’ tab provide the text ids.

By default CX_ROOT, ZCX_TEST24 are coming. I’ve provided id1 and message (GET_TEXT will
handle it). If we want to provide long test, click on text id, and click on ‘Long Text’ button, provide
the long text (GET_LONGTEXT method will take about it).

Click on ok, save button.
Click on ‘Attributes’ tab.

We can see a few default attributes which are coming from super class. I’ve added one text id or
exception id in ‘Texts’ tab, that will come to attributes tab.

Save and activate the class.
Now come to the class where we are diving the variables.

Go to method implementation

If input value is ‘0’, then we are raising the exception. If we are using standard exception class,
then not required this raising part. Automatically it will raise. If we use custom exception class,
then we need to raise the exception externally.

We can handle the exception in method calling.

DATA obj TYPE REF TO zcl_test23.
DATA obj_ex TYPE REF TO zcx_test24.
PARAMETERS: p_a TYPE string,
p_b TYPE string.
START-OF-SELECTION.
CREATE OBJECT obj.
TRY.
CALL METHOD obj->m1
EXPORTING
i_a = p_a
i_b = p_b
IMPORTING
i_c = DATA(lv_c).
CATCH zcx_test24 INTO obj_ex.
CALL METHOD obj_ex->get_longtext
RECEIVING
result = DATA(lv_result1).
WRITE:/ 'By using get_longtext method message -', lv_result1.
ENDTRY.

Execute the program by giving input p_b = 0.

If we want to display messages dynamically, then we need to maintain the messages in Global
message class (SE91 transaction). We can assign those messages in exception class. That exception
class will use in our normal class.

Go to SE91 transaction.

Provide the message like this. In 001, I’ve mentioned like &1. We can add dynamic value in place
of &1.
Create custom exception class.

Select the check box under Exception Class radio button. If we select the check box then only we
can add global message class texts in exception class.

Add one Static attribute to hold the dynamic value.
Click on ‘Texts’ tab.

We can give exception id but text is disable mode. Click on exception id, click on ‘Message Text’
button.

Select the second id, click on ‘Message Text’ button.

Provide message class, message number and in attribute 1, provide static variable name.
Save, check, activate the exception class.
Go to normal class which is raising the exception class.

Go to method implementation

Go to the program.

REPORT ztest_28731.
DATA obj TYPE REF TO zcl_test23.
DATA obj_ex TYPE REF TO zcx_test25.
PARAMETERS: p_a TYPE string,
p_b TYPE string.
START-OF-SELECTION.
CREATE OBJECT obj.
TRY.
CALL METHOD obj->m1
EXPORTING
i_a = p_a
i_b = p_b
IMPORTING
i_c = DATA(lv_c).
CATCH zcx_test25 INTO obj_ex.
CALL METHOD obj_ex->get_longtext
RECEIVING
result = DATA(lv_result1).
WRITE:/ 'By using get_longtext method message -', lv_result1.
ENDTRY.

Execute the program by giving input as ‘0’.

In the message the number 20 is coming as dynamic. We giving input with different number, the
same will apply in message also.

Interface in SAP ABAP

Interface is the collection methods which are declare and not implemented.

  1. It is independent structure, not having method implementation
  2. It has been used to extend the functionality of class
  3. Reusability and maintain the frame work of the project
  4. It can be used in number of classes depending on design

Interface in SE24 transaction:

By default all methods will be under Public section. So system will not show visibility section here.
There is not ‘Source Code’ button because we can not implement the method in Interface.

Create one more interface

Use these two interfaces in one class.

In interfaces tab, need to add the interface names. If we click on Methods tab, it will show list of
methods from interfaces.

By default 2 interfaces methods are coming. Method name will come like ‘Interface name ~
method name’ with different background colour.
Instead of calling method name by using, interface name ~ method name, we can go for alias
name and give any other name for interface methods.

Implemented the methods like this.

method ZIF_TEST124~PR_INFO.
write:/ 'PR Infro method calling from ZCL_TEST_INTF1'.
endmethod.
method ZIF_TEST124~PO_INFO.
WRITE:/ 'PO INFO method calling from ZCL_TEST_INTF1'.
endmethod.
method ZIF_TEST123~SALES_INFO.
write:/ 'Method SALES_INFO calling from ZCL_TEST_INTF1'.
endmethod.
method ZIF_TEST123~DEL_INFO.
WRITE:/ 'Method DEL_INFO from ZCL_TEST_INTF1'.
endmethod.
METHOD m1.
zif_test123~cust_num = '123'.
WRITE:/ 'Method M1 is passing value to custmer as ', zif_test123~cust_num.
ENDMETHOD.

Calling the methods from local program.

REPORT ztest_28731.
DATA obj TYPE REF TO ZCL_TEST_INTF1.
START-OF-SELECTION.
create object obj.
call method obj->m1.
call method obj->sales_info.
call method obj->del_info.
call method obj->po_info.
call method obj->pr_info.

Note: Multiple inheritance is not possible directly. By using interfaces, it is possible.

Abstract Class in SAP ABAP

  1. Abstract method can be defined in abstract class
  2. If normal class contain abstract method, at the time of activation automatically the class
    will convert as Abstract class
  3. Implementation is possible for Abstract class
  4. Abstract method Implementation should be in sub class
  5. We can’t create object for Abstract class
  6. Through sub class object, we can access the Abstract class properties
  7. Abstract class can contain Instance, Static, Abstract methods.

Differences between Abstract class and Interface in SAP ABAP

Abstract ClassInterface
Multiple Inheritance is not possibleMultiple Inheritance is possible
Partial implementation in this classImplementation is not possible
Method can be in Public or Protected or
Private
By default all methods will come under
Public
It can contain both Normal method and
Abstract Methods
It should contain only Abstract methods
Explicitly need to declare as abstract
methods
By default method should be abstract

Leave a Comment