0% found this document useful (0 votes)
16 views

ABAP OOPS

Uploaded by

Anil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

ABAP OOPS

Uploaded by

Anil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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.

Tcode for creating of class is SE24/SE38

We can create 2 types of classes

• 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.

Creation of the class is a 2-step procedure:

• 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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes

Components of the classes are:


• Properties

• 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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
• Types
o Where we will declare the type declarations and table types
• Aliases
o Providing a shortcut name to class or the interface what we are going to use
• Friends

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:

Each class component has a visibility

In ABAP the visibility has been separated into 3 sessions

• 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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
Interview Questions:

1. What are the different types of class we have


2. What is the syntax of creating of class
3. What are level of your attributes
4. What is meant by the Instance component and static component.
5. What are the visibility of the classes.
6. How can you differ if it’s a static or instance

Constructors

o These are special type of method.


o This method will get automatically called whenever we create an object reference to the
class
o These methods are mainly used to update some default values in the class attributes ( Data
declarations)
o These constructors can only declare public session.
o This method we can’t able to call explicitly

Types of constructors:

Instance constructor:

• We will using a key word called as constructor.


• Constructor method will get executed automatically when ever an object has been created.
• These are mainly used to pass the default values.
• The Instance constructor will have only importing parameters.****
• When we create a object reference to the class automatically this constructor event will get
called. i.e., if I have created 5 instances 5 times the constructor events will get called.

Example program:
REPORT zr_promg1_classes_local.

CLASS lcl_local DEFINITION.


PUBLIC SECTION.
METHODS: constructor.
METHODS: get_so_number EXPORTING ev_vbeln TYPE vbeln_va.
PRIVATE SECTION.
DATA: gv_vbeln TYPE vbeln_va.
ENDCLASS.

CLASS lcl_local IMPLEMENTATION.


METHOD constructor.
gv_vbeln = '1234567'.
ENDMETHOD.

METHOD get_so_number.
4|P ag e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
ev_vbeln = gv_vbeln.
ENDMETHOD.

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:

• These are also a special type of constructors


• This method will only execute once, i.e., if I have created 5 instances only for the first
instance this static constructor will get called
• Mainly we will use these static constructors to fill the global value which are not going to
change during the entire code execution.
• These constructors don’t contain any parameters.
• These are created with key work called class-constructor.
class ZCL_FEB_187_CLASS1 definition
public
create public .

public section.

class-methods CLASS_CONSTRUCTOR .
methods CONSTRUCTOR .
methods GET_SO_NUMBER
exporting
!EV_VBELN type VBELN_VA .
protected section.
private section.

data IV_VBELN type VBELN_VA .


class-data IV_DOC_TYPE type AUART .

5|P ag e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
ENDCLASS.

CLASS ZCL_FEB_187_CLASS1 IMPLEMENTATION.

* <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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
REPORT ZR_PROMG1_CLASSES.

*data: gr_obj TYPE REF TO ZCL_FEB_187_CLASS1.


*CREATE OBJECT gr_obj.

data(gr_obj) = NEW zcl_feb_187_class1( ).


data(gr_obj1) = NEW zcl_feb_187_class1( ).

START-OF-SELECTION.

CALL METHOD gr_obj->get_so_number


IMPORTING
ev_vbeln = data(gv_vblen)
.

WRITE: gv_vblen.

CALL METHOD gr_obj1->get_so_number


IMPORTING
ev_vbeln = gv_vblen
.

WRITE: gv_vblen.

Interview questions:

1. In which type of constructor, we can pass the parameters ➔ INSTANCE CONSTRUCUTOR


2. If I write my entire logic in constructor, can I get the o/p → No, it’s is only to pass the default
values *****
3. Can I create multiple constructors in a class → No
4. I have a constructor(instance) and class constructor(Static) in a class which constructor will
go and call first → Class constructor.
5. I have a constructor(instance) and class constructor (Static) in a class, I have created 2 object
references, will all the constructors will call for every time or only one constructor if then
which constructor → Static constructor will call only for one time where instance
constructor will get called whenever we create a object reference.
6. When we create a normal class all the methods inside class must be implemented

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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
o How to create an interface:
o Tcode: SE24
o Whenever SAP is creating an interface it will start with IF_*, but when we want to create to
create an interface we need start with ZIF_*
o What the method we are creating with the help of interface all those methods / attributes
are public.
o To call the interface in the regular classes, open the class in SE24 click on the interface tab
and provide the interface and we will get all the methods and attributes of your interface.
o Interface must be called in public session only.

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 .

class-data GV_VEBLN type VBELN_VA .

methods GET_SALES
importing
!IV_VBLEN type VBELN_VA
exporting
!ES_VBAK type VBAK .
endinterface.

8|P ag e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
Calling the interface in the class:

class ZCL_FEB_187_CLASS2 definition


public
create public .

public section.

interfaces ZIF_FEB_187_CLASS .
protected section.
private section.
ENDCLASS.

CLASS ZCL_FEB_187_CLASS2 IMPLEMENTATION.

* <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.

select SINGLE * from vbak INto es_vbak WHERE vbeln = iv_vblen.


endmethod.
ENDCLASS.

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:

o It’s a concept of passing the properties of one class to another class.


o The class where we are passes the properties is called as parent class or SUPER Class
o The class which is receiving the properties is called the as child class or SUB Class or derived
class.

9|P ag e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
o In the derived class if need we can change the logic by adding the additional features or we
can completely remove the code which is there in child class method and we can write our
own code, this we will call it’s a redefinition.
o SUPER → It’s a key word which is used to represent the parent class or Super class, you can
access the methods and attributes of the super class with the help of key of the key word
super.
o REDEFINITION ➔ it’s a key word which is used to overwrite the parent class method in the
child class.
o How to assign the parent class to the child class:
o Open the child class and click on properties and click on the button super class and assign
the super class over then.

Interview questions:

1. What is meant by inheritance?


2. Can we use multi-inheritance in one single class → No
3. Can I call multiple class in one single class → No
4. What is key work which is used to call the parent class method/attributes in child class →
SUPER
5. What is concept behind the redefinition
6. What is meant by method over riding and what is meant by method over loading, and which
is possible in SAP.
7. How can we achieve the method over riding → with the help of redefining.

Note: In SAP single inheritance is possible where the multi-inheritance is not possible.

Grand father → Father → Son → Grand son(Single Inheritance)

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.

This is possible with a key work called as redefinition.

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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
methods GET_SO_NUMBER
exporting
!EV_VBELN type VBELN_VA .

methods GET_SALES_ORG_DATA
redefinition .
protected section.
private section.

data IV_VBELN type VBELN_VA .


class-data IV_DOC_TYPE type AUART .
ENDCLASS.

CLASS ZCL_FEB_187_CLASS1 IMPLEMENTATION.

* <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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
method GET_SO_NUMBER.
ev_vbeln = iv_vbeln.
endmethod.

* <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.

Example on abstract class:

class ZCL_FEB_187_CLASS2 definition


public
abstract
create public .

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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
private section.
ENDCLASS.

CLASS ZCL_FEB_187_CLASS2 IMPLEMENTATION.

* <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.

How to redfine the abstract in the child class:


class ZCL_FEB_187_CLASS1 definition
public
inheriting from ZCL_FEB_187_CLASS2
create public .

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.

data IV_VBELN type VBELN_VA .


class-data IV_DOC_TYPE type AUART .
ENDCLASS.

13 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
CLASS ZCL_FEB_187_CLASS1 IMPLEMENTATION.

* <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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
* +------------------------------------------------------------------------
-------------------------+
* | [<---] EV_VBELN TYPE VBELN_VA
* +------------------------------------------------------------------------
--------------</SIGNATURE>
method GET_SO_NUMBER.
ev_vbeln = iv_vbeln.
endmethod.

* <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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
How to achieve this singleton class:

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

How to define the singleton class:

1. A class is said to be a singleton class if it can have only one single instance.

2. Visibility of the class must be a private

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.

CLASS lcl_singleton DEFINITION CREATE PRIVATE.

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.

CLASS lcl_singleton IMPLEMENTATION.


METHOD: class_constructor.
CREATE OBJECT lo_singleton. " Can only be executed single
ENDMETHOD.

METHOD: set_name.
gv_name = 'VERSIONIT'.
ENDMETHOD.
ENDCLASS.

DATA: lr_sing TYPE REF TO lcl_singleton.

CREATE OBJECT lr_sing.

lr_sing->set_name( ).

Advantages of the singleton class:

16 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
o we can’t able to access the singleton class more than one time.
o with use of singleton class the testability will increase
o these are mainly used in enchantments.

Wide Casting and Narrow casting:

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

Steps for defining the narrow casting:

i. Declare a variable in the super class


ii. Declare a reference variable in the child class
iii. create a instance reference of sub class
iv. assign the sub class reference in 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.

The difference between the narrow casting and wide casting:

Narrow Casting Wide Casting


Its means the copy of instance of sub class to the It means the copy of instance of super class into the
instance of the super class instance of the sub class
Only the inherited components of the sub class be Inherited as well as specific components to the sub
accessed class can be accessed

17 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
Assigning operator will be use (=) Casting operator will be used (?=)
Lr_parent = lr_child Lr_child ?= lr_parent

Example program for Narrow Casting:


REPORT Z10AM_CASTING.

*-- Narrow --> Child class calling in parent


CLASS lcl_anamial DEFINITION.
PUBLIC SECTION.
METHODs: hungry.
ENDCLASS.

class lcl_lion DEFINITION INHERITING FROM lcl_anamial.


PUBLIC SECTION.
METHODS: hungry REDEFINITION,
fasting.
ENDCLASS.

CLASS lcl_anamial IMPLEMENTATION.


METHOD: hungry.
WRITE:/ 'Anamial is hungry'.
ENDMETHOD.
ENDCLASS.

class lcl_lion IMPLEMENTATION.


METHOD hungry.
* super->hungry( ).
Write:/ 'Lion is he king of anamial, it will run fast'.
ENDMETHOD.

METHOD fasting.
WRITE:/ 'No need to run, becuase lion is on fasting today'.
ENDMETHOD.
ENDCLASS.

*-- Creating the object


data: lo_anamial type REF TO lcl_anamial,
lo_lion TYPE REF TO lcl_lion.

*-- Narrow casting


START-OF-SELECTION.
WRITE:/ 'Anamial without narrow casting'.
CREATE OBJECT lo_anamial.
lo_anamial->hungry( ).

WRITE:/ 'Anamial - Narrow casting'.


CREATE OBJECT lo_lion.

18 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
lo_anamial = lo_lion.

lo_anamial->hungry( ).

lo_lion->fasting( ).

Understanding Widening Cast in ABAP


Objects
There are many discussions about Widening cast in SCN. some say, it is Up casting while other say it
is Down casting. I remember in a way as depicted below and still following the same.

Let’s take a simple inheritance tree.

Narrowing Cast:

Assigning/copying the instance of sub class to super


class instance is called Narrowing Cast.

As shown in the figure, assigning sub class to super


class is going up wards. From sub class(es) to super
class while moving Up the path becomes narrow as
shown in the left image. So I simply remember this.
Narrowing cast = Up cast 🙂 .

19 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
Widening Cast:

Assigning/coping the instance of super class to the sub


class instance is called Widening Cast.

This is the reverse of Narrowing cast. As shown in the


figure, assigning super class to sub class is going down
wards. From super class to sub class while moving
Down the path becomes wider as shown in the left
image. So I simply remember this – Widening cast =
Down cast.

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.

Simple Inheritance Report


REPORT zkk_widening_cast_demo.

CLASS lcl_parent DEFINITION.


PUBLIC SECTION.
METHODS parent_method.

ENDCLASS. “lcl_parent DEFINITION

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.


PUBLIC SECTION.
METHODS parent_method REDEFINITION.
METHODS child_method.

ENDCLASS. “lcl_child DEFINITION

CLASS lcl_parent IMPLEMENTATION.

METHOD parent_method.
WRITE / ‘Called -> parent method in Parent Class!’.
ENDMETHOD. “parent_method
ENDCLASS. “lcl_parent IMPLEMENTATION

CLASS lcl_child IMPLEMENTATION.

20 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
Simple Inheritance Report
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

ENDCLASS. “lcl_child IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_parent TYPE REF TO lcl_parent,


lr_child TYPE REF TO lcl_child.

CREATE OBJECT: lr_parent,lr_child.

lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

Now after executing this I got the below output:

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.

continuing the above report.

CREATE OBJECT: lr_parent,lr_child.

WRITE / 'Before Narrowing Cast:'.

21 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

* Narrowing cast
lr_parent = lr_child.

WRITE / 'After Narrowing Cast:'.


lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

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.

CREATE OBJECT: lr_parent,lr_child.

WRITE / 'Before Widening Cast:'.


lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

TRY .
* Widening Cast
lr_child ?= lr_parent.

22 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
WRITE / 'After Widening Cast:'.
lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

The output is as below:

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 😛 .

CLASS lcl_parent DEFINITION.

ENDCLASS. "lcl_parent DEFINITION

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.

ENDCLASS. "lcl_child DEFINITION

CLASS lcl_parent IMPLEMENTATION.

ENDCLASS. "lcl_parent IMPLEMENTATION

23 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
CLASS lcl_child IMPLEMENTATION.

ENDCLASS. "lcl_child IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_parent TYPE REF TO lcl_parent,


lr_child TYPE REF TO lcl_child.

CREATE OBJECT: lr_parent,lr_child.

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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
CLASS lcl_parent DEFINITION.
PUBLIC SECTION.
METHODS parent_method.

ENDCLASS. "lcl_parent DEFINITION

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.


PUBLIC SECTION.
METHODS parent_method REDEFINITION.
METHODS child_method.

ENDCLASS. "lcl_child DEFINITION

CLASS lcl_parent IMPLEMENTATION.

METHOD parent_method.
WRITE / 'Called -> parent method in Parent Class!'.
ENDMETHOD. "parent_method
ENDCLASS. "lcl_parent IMPLEMENTATION

CLASS lcl_child 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

ENDCLASS. "lcl_child IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_parent TYPE REF TO lcl_parent,


lr_child TYPE REF TO lcl_child.

CREATE OBJECT: lr_parent,lr_child.

WRITE / 'Before Widening Cast:'.


lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).

lr_parent = lr_child. " Narrowing cast

25 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
TRY .
* Widening Cast
lr_child ?= lr_parent.
WRITE / 'After Widening Cast:'.
lr_parent->parent_method( ).
lr_child->parent_method( ).
lr_child->child_method( ).
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

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!

So, when this Widening cast is actually useful?

To explain this, I will take a simple example using interfaces:

REPORT zkk_widening_cast_demo.

INTERFACE lif_test.
METHODS interface_method.

26 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
ENDINTERFACE. "lif_test

CLASS lcl_myclass DEFINITION.


PUBLIC SECTION.
INTERFACES lif_test.
ENDCLASS. "lcl_class1 DEFINITION

CLASS lcl_myclass IMPLEMENTATION.


METHOD lif_test~interface_method.
WRITE / 'Interface method call in My Class'.
ENDMETHOD. "lif_test~interface_method
ENDCLASS. "lcl_class1 IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_intf TYPE REF TO lif_test,


lr_mine TYPE REF TO lcl_myclass.

CREATE OBJECT lr_intf TYPE lcl_myclass.


TRY .
* Widening Cast
lr_mine ?= lr_intf.
WRITE /'Widening Cast Successful!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

Output:

Since we cannot directly assign the interface reference to class reference,

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

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
So we do a Widening cast, lr_mine ?= lr_intf , which won’t give syntax check and at run time if lr_intf
contains the same reference of lr_mine, the reference will be copied else the exception will be
thrown.

Now lets take one more class to understand better.

REPORT zkk_widening_cast_demo.

INTERFACE lif_test.
METHODS interface_method.
ENDINTERFACE. "lif_test

CLASS lcl_myclass DEFINITION.


PUBLIC SECTION.
INTERFACES lif_test.
ENDCLASS. "lcl_class1 DEFINITION

CLASS lcl_yourclass DEFINITION.


PUBLIC SECTION.
INTERFACES lif_test.
ENDCLASS. "lcl_class2 DEFINITION

CLASS lcl_myclass IMPLEMENTATION.


METHOD lif_test~interface_method.
WRITE / 'Interface method call in My Class'.
ENDMETHOD. "lif_test~interface_method
ENDCLASS. "lcl_class1 IMPLEMENTATION

CLASS lcl_yourclass IMPLEMENTATION.

METHOD lif_test~interface_method.
WRITE / 'Interface method call in Your class'.
ENDMETHOD. "lif_test~interface_method
ENDCLASS. "lcl_class2 IMPLEMENTATION

START-OF-SELECTION.

DATA: lr_intf TYPE REF TO lif_test,


lr_mine TYPE REF TO lcl_myclass,
lr_your TYPE REF TO lcl_yourclass.

* CREATE OBJECT lr_intf TYPE lcl_myclass.


CREATE OBJECT lr_intf TYPE lcl_yourclass.

TRY .
* Widening Cast
lr_mine ?= lr_intf.

28 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)
ABAP – OOPS Classes
WRITE /'Widening Cast Successful!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

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.

Now, If we look at the below statement

CREATE OBJECT lr_intf TYPE lcl_yourclass.


TRY .
* Widening Cast
lr_mine ?= lr_intf.
WRITE /'Widening Cast Successful!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

is nothing but Narrowing cast before widening cast,

DATA lr_mine1 TYPE REF TO lcl_myclass.


CREATE OBJECT lr_mine1.

lr_intf = lr_mine1. " Narrowing Cast


TRY .
* Widening Cast
lr_mine ?= lr_intf.
WRITE /'Widening Cast Successful!'.
CATCH cx_sy_move_cast_error.
WRITE / 'Widening Cast Failed!'.
ENDTRY.

So in this context, “Do Narrowing cast before Widening cast” makes sense without confusion!

Hope I tried to explain Widening cast to some extent!.

29 | P a g e

Copy Rights @SR Trainings


Contact Details: SR Trainings
Email: [email protected]
YouTube: https://www.youtube.com/@SRTrainings123
Call: +91-740-633-3656(WhatsApp/Telegram)

You might also like