You are on page 1of 78

Page 1 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

Sonu Kumar
Verma

Technical Consultant (Vikalp


Solutions)
CLASS AND OBJECT USING REPORT
PROGRAM IN ABAP

Classes​: ​Classes describe objects. From a technical point of view, objects are runtime
instances of a class. In theory, you can create any number of objects based on a single
class. Each instance (object) of a class has a unique identity and its own set of values for
its attributes.

Classes in ABAP Objects can be declared either locally


or globally.

Global Class: ​Global classes and interfaces are defined in the class builder
(​Transaction se24​) ​In

the ABAP Workbench. They are stored centrally in class polls in the class library in the R/3
repository . All the programs in R/3 system can access the global classes.

Local Classes : ​Local classes are define with in the ABAP PROGRAM (Transaction
SE38) and can only be used in the program in which they are declared.

Local Classes : Every class will have two


sections.
1. Definition 2.
Implementation

Definition : ​This section used to declare the components of classes such as


attributes,methods ,events . they are enclosed in the ABAP statements CLASS ....... END
CLASS.

CLASS <class name>


DEFINITION. ..... END
CLASS.

Implementation : ​This section of a class contains implementation of all methods of the


class . the implementation part of local class is processing block . CLASS <class name>
IMPLEMENTATION. ......... END CLASS.

Page 2 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Structure of a class. ​The following statements
define the structure of class.
1. ​A class contain components. ​2. ​Each
component is assigned to a visibility section ​3.
Classes implement methods.

REPORT ​ZS_CLASS. ​*Declaring class


definition attribute and property.
CLASS
FIRST ​DEFINITION. ​*
public section of class.
PUBLIC ​SECTION.
METHODS: SHOWME
,SAWME.
ENDCLASS. ​*Implementation
of that class and its method.
CLASS FIRST
IMPLEMENTATION. ​*
Method implementation.
METHOD
SHOWME.
WRITE ​'SHOW ME
METHOD'​.
ENDMETHOD.

METHOD
SAWME.
WRITE ​/ ​'SAW WITH IN THE
SUPERCLASS'​. ENDMETHOD.

ENDCLASS. ​* ​*class inherating a

super class. CLASS


​ ​SECOND ​DEFINITION
INHERITING FROM ​FIRST..
PUBLIC ​SECTION. ​METHODS
SHOWME REDEFINITION.

ENDCLA
SS.

*SUB CLASS
IMPLEMENTATION. ​CLASS
SECOND IMPLEMENTATION.
* REDEFINE SUBCLASS
METHOD.
METHOD
SHOWME.
WRITE ​/ ​'WITH IN THE SUBCLASS'​.
ENDMETHOD. ENDCLASS. ​*CREATING
OBJECT AND EXECUTING ITS METHOD.

Page 3 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
*always create refernce and object in START-OF-
SELECTION Event because the data extraction come under
this event.

START-OF-SELECTION. ​*creating
​ ATA
reference of the object. D
FIRSTOBJ ​TYPE REF TO ​FIRST.
DATA ​SECONDOBJ ​TYPE REF TO
SECOND.

*creating object of the


​ REATE ​OBJECT
reference. C
FIRSTOBJ. ​*calling the
method of that class.

CALL METHOD
FIRSTOBJ->SHOWME. ​*Creating
object of second class ​CREATE
OBJECT SECONDOBJ. ​*calling
method of subclass and superclass.

CALL METHOD
SECONDOBJ->SHOWME. ​CALL
METHOD ​SECONDOBJ->SAWME.

Components of
class.
1. ​Attributes : ​any data , constants , types declared with in a class from the attribute of
the
class. ​2. ​Methods : ​Block of code providing some functionality offered by the
class can be
compared to function modules . they can access all of the attributes of a
class. Methods are defined in the definition part of class and implement it in the
implementation part using the following processing block.

METHOD <method
name> ......... END
METHOD.

Method are call using the ​CALL METHOD


statement.

3. EVENTS : ​A mechanism set with in class which can help a class to trigger methods
of
another class. ​4. Interfaces : ​Interfaces are independent structures that you
can implement in a class to
extend the scope of the
class.

Page 4 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
INSTANCE AND STATIC
COMPONENTS

• ​Instance components exist separately in each instance (object) of the class and
are referred using instance component selector ​->
• ​Static component only exist once per class and are valid for all instances of the
class. They are declared with the ​CLASS- ​keywords.
• ​Static components can be used without even creating an instance of the class
and are referred to using static component selector ​=>
Here is the sample
code .

*DEFINING CLASS
COMP_EMP C ​ LASS
COMP_EMP DEFINITION.
*public section of class
PUBLIC
SECTION. ​*static data
or class data .
CLASS-DATA ​CNAME(12).
*instance data for each
object.
DATA ​NAME(12). ​*define the constructor with in a class using constructor
k/w with in the class definition * with in constructor you only pass the importing
parameter because constructor didn't have * any return type.

*instance constructor
definitions.
METHODS ​: CONSTRUCTOR ​IMPORTING
A ​TYPE I ​.

*CLASS CONSTRUCTOR NOT HAVE ANY IMPORT


PARAMETER .
CLASS-METHODS
CLASS_CONSTRUCTOR. ​*instance
method definition.
METHODS
SHOW_NAME.
ENDCLASS. ​"COMP_EMP DEFINITION

*------------------------------------------------------------------
----* * CLASS COMP_EMP IMPLEMENTATION
*------------------------------------------------------------------
----* *

Page 5 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
*------------------------------------------------------------------
----* ​CLASS ​COMP_EMP IMPLEMENTATION.

* now define the constructor of the class here you can initialize the
varriable
METHOD ​CONSTRUCTOR. ​*HERE WHEN YOU CALL THIS CONSTRUCTOR
THEN THIS WILL GIVE THE OCTAL VALUE OF A BECAU SE A IS *INITALIZE AFTER
WRITE STATMENT.
WRITE ​:/ ​'THIS IS CNSTRUCTOR
METHOD'​, A. NAME = ​'SONU'​. CNAME =
'STATIC DATA'​. ENDMETHOD.
"CONSTRUCTOR

*HERE THIS IS CLASS CONSTRUCTOR , WHICH WILL CALL AT FIRST WHEN U


CREATE OBJECT OF THIS
C
LASS
*
METHOD
CLASS_CONSTRUCTOR .
WRITE ​: / ​'HELLO CLASS
CONSTRUCTOR' ​. ENDMETHOD.
"CLASS_CONSTRUCTOR

*IMPLEMENTING THE METHOD


SHOW_NAME.
METHOD
SHOW_NAME.
WRITE ​: / ​'AFTER INITIALIZING THE NAME VALUE IN
CONSTRUCTOR :' ​, NAME. ENDMETHOD. ​"SHOW_NAME

ENDCLASS. ​"COMP_EMP IMPLEMENTATIO *always create method with in


start-of-selection event because data extraction come under this *event.

START-OF-SELEC
TION.
DATA ​OBJ ​TYPE REF TO
COMP_EMP. ​* CREATE OBJECT
​ REATE
OBJ EXPORTING A = 13. C
OBJECT OBJ ​EXPORTING ​A = ​12.

*CALLING THE
METHOD.
CALL METHOD
OBJ->SHOW_NAME.

Page 6 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
WRITE ​/ OBJ->NAME. ​"ACESSING INSTANCE DATA USING
OBJECT. W ​ RITE ​/ COMP_EMP=>CNAME. ​"ACCESSING
CLASS DATA USING CLASS
Visibilty of
Components.

Each class component has a visibility . In ABAP Objects the whole class definition is
separated into three visibility sections. :​PUBLIC , PRIVATE, PROTECTED

1. Data declared with in the public section can be accessed by the class itself , by its
subclasses
as well as by other user outside the class. 2. Data declared with in the
protected section can be accessed by the class itself and also by its
subclasses but not by external user outside the class. 3. Data declared with in
the private section can be accessed by the class only , but not by its
subclasses and by external users outside the
class.

CLASS <class_name>
DEFINITION. PUBLIC
SECTION. ........
PROTECTED SECTION.
............... PRIVATE
SECTION. ........... END
CLASS.

Here with in each block you can define public , protected and private attributes and
method for a class.
Page 7 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

Attributes of Object Oriented


Programming

1. ​Inheritance
2. ​Abstraction
3.
Encapsulation
4.
Polymorphism

INHERITAN
CE

Inheritance ​is the concept of adopting the features from the parent and reusing them . It
involves passing the behavior of a class to another class. You can use an existing class to
derive a new class. Derived classes inherit the data and methods of the super class.
However, they can overwrite existing methods, and also add new ones.
Inheritance is of two types: ​Single Inheritance and Multiple
Inheritance

Single Inheriting: ​Acquiring the properties from a single parent. (Children can
be more)

Multiple inheritance​: Acquiring the properties from more than one parent.
Example

Inheritance ​defines the relationship between classes, in which a class (subclass) uses the
structure and behavior that has already been defined in one or more other classes
(superclasses).
Page 8 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

So you might be thinking "Inheritance is about reuse!" Correct! However, it is not quite so
simple.

Allow me to use a concrete example to explain inheritance: ​Collection​. Expressed in the


abstract, a collection is any number of objects (more precisely object references).
However, if you want to work with a concrete collection of objects, ask yourself the
following questions: "What does the collection contain? Are there twin references to one
object? Are the objects sorted according to any particular format?"

You will quickly come to the conclusion that a single type of collection is not sufficient.
Collections are required that guarantee:

• that no double entries are present (Sets)


• that a specific order is adhered to (Sequence)
• ​that the objects are sorted according to their keys (KeySortedCollection)
etc.

You may be thinking: "So I


have many types of collection. This is therefore a good opportunity to implement my new
knowledge. I will implement each type of collection as a class!" In principle this approach is
correct. However, you will soon establish that all collections have several components in
common:

Page 9 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

✓ Each class requires a method in order to add objects to a collection ✓ Each class
requires a method in order to delete objects from a collection ✓ Each class has a
method which identifies the number of object references in the collection ​... You can
probably think of some other similarities

Inheritance ​is the solution to this situation. You implement all of the similarities in the
class Collection. You then implement the individual types of collection in their own classes
which are ​Subclasses ​of the class Collection. As a subclass, these classes ​inherit ​all of
the components of the ​Superclass​. Attributes, methods and events are inherited. In
addition, you can implement additional attributes, methods and events in the subclass. If a
method from the Public Section or Protected Section of the superclass cannot be used in
this way, then the method can be redefined in the subclass

Syntax : ​CLASS <subclass> DEFINITION INHERITING FROM


<superclass>.

REPORT
ZS_SINGLE_INHERITANCE.
CLASS FIRST ​DEFINITION. ​*
DEFINING ATTRIBUTES.
PUBLIC ​SECTION. ​DATA
FIRST(12). ​"DEFINING
ATTRIBUTES ​METHODS ​ADD.
"DEFINING METHOD.

ENDCLASS.
* IMPLEMENTING
CLASS.
CLASS FIRST
IMPLEMENTATION.
METHOD ​ADD. ​"METHOD
IMPLEMEMNTATION.
WRITE ​/ ​'HELLO METHOD FROM THE
FIRST'​. ENDMETHOD. ENDCLASS.

* DEFINING OTHER CLASS WHICH INHERIT PROPERTY


FROM FIRST.
CLASS ​SECOND ​DEFINITION INHERITING
FROM ​FIRST. ​PUBLIC ​SECTION. ​DATA ​SECOND(13).
METHODS ​HELLO. ​METHODS ADD ​REDEFINITION.
"REDEFINING THE ADD METHOD. E ​ NDCLASS.
CLASS ​SECOND
IMPLEMENTATION.
METHOD
HELLO.
Page 10 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
WRITE ​/ ​'HELLO FROM
SECOND'​. ENDMETHOD.
METHOD ​ADD.
WRITE ​/ ​'FROM THE SUBCLASS
REDEFINITION'​. ENDMETHOD.
ENDCLASS.

*EVENT WHERE DATA EXECUTION


OCCUR.
START-OF-SELEC
TION. ​* CREATE REFERNCE
OF OBJECT. D ​ ATA ​OBJ
TYPE REF TO ​FIRST. ​*
CREATING OBJECT.
CREATE
OBJECT OBJ. ​* CALLING
MEHTOD OF CLASS.
CALL METHOD
OBJ->ADD.

* CREATE REF OF
SECOND CLASS
DATA ​OBJ1 ​TYPE
REF TO ​SECOND. ​*
CREATING OBJECT
CREATE ​OBJECT
OBJ1. ​* CALLING
THE METHOD.
CALL METHOD ​OBJ1->HELLO. ​* CALLING
THE SUPER CLASS METHOD USING SUBCLASS
OBJECT.
CALL METHOD
OBJ1->ADD.

Subclass can access public/protected components( methods, attributes etc)


of superclass.

Subclass can re-implement inherited methods from


superclass ​REPORT ​ZS_CLASS_SUBCLASS. ​* CLASS C1
​ LASS ​C1 DEFINITION. ​"CLASS DEFINITION
DEFINITION C
PUBLIC
SECTION.
METHODS ​: METH1. ​PROTECTED
SECTION. ​METHODS ​METH2. ENDCLASS. ​"C1
DEFINITION END * CLASS C1
IMPLEMENTATION C ​ LASS ​C1
IMPLEMENTATION ​. ​"CLASS IMPLEMENTATION
METHOD ​:
METH1.
WRITE:/5 ​'I am meth1 in class
C1'​.

Page 11 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CALL METHOD
METH2. ENDMETHOD.
":

METHOD ​:
METH2.
WRITE:/5 ​' I am meth2 in class C1 '​. ENDMETHOD. ​":
ENDCLASS. ​"C1 IMPLEMENTATION END * CLASS C2
DEFINITION INHERITING THE PROPERTY FORM SUPER CLASS
​ LASS ​C2 ​DEFINITION INHERITING FROM ​C1.
C1. C
PUBLIC
SECTION.
METHODS ​: METH1 ​REDEFINITION ​.
"REDEFINING THE METHOD. ​PROTECTED ​SECTION.
METHODS ​: METH2 REDEFINITION.
"REDEFINING THE METHOD. ​ENDCLASS. ​"C2
DEFINITION * CLASS C2 IMPLEMENTATION ​CLASS ​C2
IMPLEMENTATION.
METHOD
METH1.
WRITE:/5 ​'I am meth1 in class C2'​. ​CALL
METHOD ​METH2. ENDMETHOD. ​"METH1
METHOD ​: METH2.
WRITE:/5 ​' I am meth2 in
class C2 '​. ENDMETHOD. ​":
ENDCLASS. ​"C2 IMPLEMENTATION

START-OF-SELEC
TION.
DATA ​: OREF1 ​TYPE REF
TO ​C1 , OREF2 ​TYPE REF
TO ​C2. ​CREATE ​OBJECT :
OREF1 , OREF2. ​CALL
METHOD ​: OREF1->METH1 ,
OREF2->MET
H1.

Inheritance is the concept of passing the behavior of a class to


another class.

• You can use an existing class to derive a new


class.
• Derived class inherits the data and methods of a super
class.
• However they can overwrite the methods existing methods and also add new
once.
• Inheritance is to inherit the attributes and methods from a parent
class.

Inheritanc
e:
✓ Inheritance is the process by which object of one class acquire the properties of
another
class. ✓ Advantage of this property is reusability. ✓ This means we can
add additional features to an existing class with out modifying it.

Page 12 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Working with SUPER and ME
keyword

SUPER and ME k/w used for accessing the attribute and method with in subclass
and class .

SUPER k/w ​:- ​This k/w used for accessing the super class attribute and method with in
the subclass you can see the example below . you can use the super k/w only for
accessing the super class method with in the subclass but you can only use it with in the
redefined and reimplementation of that method in subclass , you can not access the super
class method outside of it , and you can not access the super class attribute .

However you can access the attributes and method of super class using the me k/w
because when you inherit the property of super class then it will automatically come under
the subclass.
REPORT
ZS_CLASS_SUPER_ME.

*------------------------------------------------------------------
----* * CLASS FIRST DEFINITION
*------------------------------------------------------------------
----* *

*------------------------------------------------------------------

----* CLASS
​ FIRST ​DEFINITION.
PUBLIC ​SECTION. ​* CREATING PUBLIC
ATTRIBUTES AND METHOD OF CLASS FIRST.
DATA ​FIRST1(12) ​VALUE ​'VIKALP'​. ​DATA ​SECOND(22).
METHODS ​SHOW. ​"ALWAYS DEFINE WITH METHODS NOT
METHOD. M​ ETHODS ​SHOWME. ​* CREATING PRIVATE
ATTRIBUTE AND METHOD OF CLASS FIRST.
PRIVATE
SECTION ​.
DATA ​SS(12) ​VALUE
'PRIVATE'​. ENDCLASS. ​"FIRST
DEFINITION *CLASS FIRST
IMPLEMENTATION.

CLASS FIRST ​IMPLEMENTATION. ​*


METHOD SHOWME
IMPLEMENTATION.
METHOD
SHOWME.
WRITE ​/ ​'THIS IS SHOW
ME METHOD'​. ENDMETHOD.
"SHOWME * METHOD SHOW
IMPLEMENTATION.
METHOD
SHOW.
WRITE ​/ ​'SHOW METHOD'​. ​WRITE ​: / ​'THIS IS ATTRIBUTE
VALUE'​, me->FIRST1. ​* CALLING THE METHOD WITH IN THE
SAME CLASS USING THE ME K/W.
CALL METHOD
ME->SHOWME.
ENDMETHOD. ​"SHOW
Page 13 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
ENDCLASS. ​"FIRST IMPLEMENTATION

*SECOND CLASS DEFINITION WHICH INHERIT THE PROPERTY


OF CLASS FIRST.

CLASS ​SECOND ​DEFINITION INHERITING


FROM FIRST ​. ​* PUBLIC SECTION OF CLASS
SECOND.
PUBLIC
SECTION ​.
DATA ​SECOND1(12). ​*
REDEFINING THE SHOW AND SHOW ME
METHOD.
METHODS ​SHOW REDEFINITION.
METHODS ​SHOWME REDEFINITION.
*NEW METHOD WITH IN THE SECOND
CLASS.
METHODS ​SHOW1.
ENDCLASS. ​"SECOND DEFINITION

*------------------------------------------------------------------
----* * CLASS SECOND IMPLEMENTATION
*------------------------------------------------------------------
----* *

*------------------------------------------------------------------

----* CLASS
​ ​SECOND IMPLEMENTATION.
METHOD ​SHOWME. ​* CALLING THE SUPER CLASS
METHODS USING THE SUPER KEYWORD..
CALL METHOD ​SUPER->SHOWME. ​* SHOWING THE VALUE OF FIRST1 WHICH IS
DEFINED WITH IN THE CLASS FIRST , HERE WE USED ME K/W FOR ACC ESSING THIS *
BECAUSE CLASS SECOND INHERIT THE PROPERTY OF CLASS FIRST.
WRITE ​:/ ​'SHOWING THE FIRST1 VALUE'​,
ME->FIRST1.

ENDMETHOD. ​"SHOWME *IMPLEMENTING


THE HIS OWN CLASS SECOND METHOD SHOW1.
METHOD
SHOW1.
WRITE ​: / ​'SHOW1 METHOD IN SUBCLASS' ​. ENDMETHOD. ​"SHOW1 *REDEFINING THE
METHOD OF SUPER CLASS AND CALLING THE SUPER CLASS METHOD. *YOU WILL ONLY CALL
THE SUPER CLASS METHOD WITH IN THE REDEFINTION OF SAME METHOD USING THE SUPE
R K/W.

METHOD
SHOW.
WRITE ​: / ​'THIS IS IN SUBCLASS METHOD'​. ​CALL METHOD
SUPER->SHOW. ​"CALLING THE SUPER CLASS METHOD.
WRITE ​: / ​'THIS IS SUPER CLASS ATTRIBUTE'​. ENDMETHOD.
"SHOW ​ENDCLASS. ​"SECOND IMPLEMENTATION

*EVENT WHERE EXECUTION


START.

START-OF-SELEC
TION.

Page 14 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
* CREATING REFERENCE OF CLASS
SECOND.
DATA ​OBJ ​TYPE REF
TO ​SECOND. ​*CREATING
OBJECT OF CLASS SECOND.
CREATE ​OBJECT OBJ. ​CALL
METHOD ​OBJ->SHOWME.
*CREATING REFERENCE OF
CLASS FIRST.
DATA ​OBJ1 ​TYPE
REF TO ​FIRST. ​*CREATING
OBJECT OF CLASS FIRST.
CREATE ​OBJECT OBJ1.
CALL METHOD
OBJ1->SHOW.

ME
K/W

Me k/w used for accessing the attribute and method with in the same class you can use
this k/w with in the same class you can see the example above here I use to call the
attribute and Method using the me k/w like this.

Here in below you can see that I am using the me k/w for accessing the attribute and
method.

* METHOD SHOW
IMPLEMENTATION.
METHOD
SHOW.
WRITE ​/ ​'SHOW METHOD'​. ​WRITE ​: / ​'THIS IS ATTRIBUTE
VALUE'​, me->FIRST1. ​* CALLING THE METHOD WITH IN THE
SAME CLASS USING THE ME K/W.
CALL METHOD
ME->SHOWME.
ENDMETHOD.

USING THE ME K/W FOR ACCESSING THE INSTANCE VALUE


IN METHOD. ​REPORT ​ZS_CLASS_USE_OF_ME. ​* CLASS
FIRST1 DEFINITION

CLASS ​FIRST1
DEFINITION. ​*
DEFINING PUBLIC
SECTION
PUBLIC
SECTION.
DATA ​NUM ​TYPE I
VALUE ​22. ​METHODS
TESTMETHOD.

ENDCLASS. ​"FIRST1 DEFINITION

* CLASS first1
IMPLEMENTATION

CLASS ​FIRST1
IMPLEMENTATION. ​*
defining the method.
METHOD
TESTMETHOD.
WRITE ​: /​'value accessing using the me k/w'​, ME->NUM. ​"accessing the instance value using
the me k/w

Page 15 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
ENDMETHOD. ​"testmethod
ENDCLASS. ​"first1 IMPLEMENTATION

START-OF-SELEC
TION.
DATA ​OBJ ​TYPE REF TO
FIRST1. ​CREATE ​OBJECT OBJ.
CALL METHOD
OBJ->TESTMETHOD. ​Creating
table pointer in ABAP Class.

This program is included in above program this is just snippet of code for testing
purpose.

* CREATING TABLE
POINTER
DATA ​TAB_OBJ ​TYPE TABLE OF REF TO ​FIRST1. ​*creating loops
5 times and create object 5 and append it to tables type object tab_obj.
DO ​5
TIMES.
CREATE ​OBJECT OBJ.
APPEND ​OBJ ​TO
TAB_OBJ. ENDDO.

* looping at table and calling the


method.
LOOP AT ​TAB_OBJ ​INTO
OBJ.
CALL METHOD
OBJ->TESTMETHOD.
ENDLOOP.
Page 16 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

Abstraction: ​Everything is visualized in terms of classes


and objects.

Abstract class ​Objects cannot be created from an abstract class. Only the
subclasses of such class can be instantiated.

With in abstract class there are two types of


method.

1. ​Concrete method : ​which can be implemented with in the implementation of class


we
can define concrete method as we define simple
method like this. ​methods ​method1. ​"defining concreate method.

2. ​Abstract method ​: ​abstract method define with k/w abstract and it can not be
implement with in the class it only implemented with in a sub class where it inherit his
property like this.
methods ​abs_method abstract. ​"defining abstract
method.

report
zs_class_abstract_class.

* CLASS abs_class DEFINITION c​ lass ​abs_class ​definition ​abstract. ​* with in abstract


class you can define two types of method abstract method and concrete method.
public
section.
data ​abdata(12). ​methods ​method1. ​"defining concreate
method. ​methods ​abs_method abstract. ​"defining
abstract method. e ​ ndclass. ​"abs_class DEFINITION

*with in implementation of class you only implement the concrete method definition not for the abstract
method. *if you give implementation of abstract method it will give you compilation error.

class ​abs_class
implementation.
method
method1.
endmethod.
"method1 * method abs_method. "when be open this it will
give compilation error showing that abstract method * endmethod. "can not be implemented.

endclass. ​"abs_class IMPLEMENTATION


Page 17 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
class ​impl_abs_class ​definition inheriting from
abs_class.
public ​section. ​data ​sd(12). ​methods ​abs_method redefinition. ​"redefinition of abstract method must
be necessary with in the sub class. e ​ ndclass.

class ​impl_abs_class
implementation.
method
abs_method.
write ​/ ​'this is abstract
method'​. endmethod.
endclass.

Abstract method will only implemented with in the subclass if you implement it with in the
same class then it will give you error. And with in the subclass you must redefine the
abstract method of super class , then you can implement it with in the implementation of
subclass. As you saw above code.

We can’t create the object of abstract type class , if we create object of abstract
class then it will give compiler error as you saw in below code.

​ ata ​conc_obj ​type ref to


data ​abs_obj ​type ref to ​abs_class. ​"reference of abstract class d
impl_abs_class. ​"reference of concrete class * create object abs_obj. "we can not create the object
of abstract class if we create then it will give compiler * error
create ​object
conc_obj.
abstract method can not be implemented in
abstract class.

You can not give implementation of abstract method with in the abstract class , you can
see the example in above program , if u do it will give compilation error.

Final classes can not be inherited by other classes. ​Class defined with final k/w
can not be inherited from the other classes means subclass , if you do it will give
compilation error. Like this.

report ​zs_class_final_class. ​* CLASS fin_class


DEFINITION c​ lass ​fin_class ​definition ​final. ​"defining final
class using final k/w

Page 18 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
public
section.
methods
show.

endclass. ​"fin_class DEFINITION *


CLASS fin_class IMPLEMENTATION
class ​fin_class implementation.
method
show.
write ​'hi this final method.'​. endmethod.
"show e ​ ndclass. ​"fin_class
IMPLEMENTATION

*inheriting the final class , but as we check using ctrl+f2 it will give error that final class not be
subclassed. ​class ​inherit_final ​definition inheriting from ​fin_class.
endclas
s.

class ​inherit_final
implementation.
endclas
s.

so you must remember that final class never be


instantiated.

Final method can not be redefined in subclass. ​Final method never be


reimplemented with in the subclass if you do this it will give you compilation error as you
show in below code.

REPORT
ZS_CLASS_FINAL_METHOD.

class first
definition.
public ​section. ​methods ​showme final.
"defining the final method. e​ ndclass.

class first
implementation.
method ​showme. ​"implementing the final
method.
write ​'hello this is
method'​. endmethod.
endclass.

class ​second ​definition inheriting from


first.
public
section.
methods ​showme redefinition. ​"redefining the final method will give you compilation
​ ndclass.
error. e

class ​second
implementation.
method
showme.
write ​'hello
method'​.

Page 19 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
end
method.
endclass.

Encapsulation ​: ​The wrapping up of data and methods into a single unit (called class)
is known as Encapsulation. The data is not accessible to the outside world only those
methods, which are wrapped in the class, can access it.

Polymorphism: ​Methods of same name behave differently in different classes​. ​Identical


(identically-named) methods behave differently in different classes. Object-oriented
programming contains constructions called interfaces. They enable you to address
methods with the same name in different objects. Although the form of address is always
the same, the implementation of the method is specific to a particular class.

WORKING WITH
CONSTRUCTOR

Each class has only one instance constructor, which cannot be overloaded. But,
using optional and default parameters allows seeming overloading.

Description of
Constructor:

❖ Constructor is ​automatically called when an object


created. ​❖ Constructor is the same name of the class.
❖ No return value. ❖ With in static method we can only
access class attributes. ❖ Class-constructor does not
have any parameters. ❖ ​Constructor has only import
parameters​.

There are two types of


constructor.

1. Instance
constructor 2. Class
constructor

Page 20 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Constructor can not have export
parameters.

When we call the constructor means during creation of object this import parameter pass
by using export parameter like this.

when we create object then it will automatically call the constructor of that class
then if that con structor have import parameter then we must pass that value using
the export parameter.

Like
this.

REPORT
ZS_CLASS_CONSTRUCTOR1.

CLASS FIRST
DEFINITION.
PUBLIC
SECTION.

* CLASS CONSTRUCTOR WILL BE


EXECUTE FIRST . * DEFINING CLASS
CONSTRUCTOR.
CLASS-METHODS
CLASS_CONSTRUCTOR.

* DEFINING INSTANCE
CONSTRUCTOR.
METHODS ​CONSTRUCTOR ​importing ​today_date
type ​d. ENDCLASS.

* CLASS
IMPLEMENTATION.
CLASS FIRST
IMPLEMENTATION.

*INSTANCE CONSTRUCTOR
IMPLEMENTATION.
METHOD
CONSTRUCTOR.
WRITE ​/ ​'INSTANCE CONSTRUCTOR'​. ​write ​: /
'TODAY DATE IS ' ​, today_date dd/mm/yyyy.
ENDMETHOD. ​* CLASS CONSTRUCTOR
IMPLEMENTATION.
METHOD
CLASS_CONSTRUCTOR.
WRITE ​/ ​'CLASS
CONSTRUCTOR'​.
ENDMETHOD. ENDCLASS.

*EVENT OF REPORT WHERE EXECUTION


OCCCUR.
START-OF-SELECTIO
N. ​* CREATING REFERNCE OF
CLASS FIRS.
DATA ​OBJ ​TYPE REF TO ​FIRST. ​* CREATING OBJECT OF CLASS FIRST THEN IT
WILL CALL FIRST THE CLASS CONSTRUCTOR THEN IT WILL CALL THE * OBJECT
CONSTRUCTOR.

CREATE ​OBJECT OBJ ​exporting ​today_date = sy-datum. ​"when we create object then it will
automatically call the * constructor of that class then if that constructor have import parameter then we
must pass that value using the * export parameter.

Page 21 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Objects must be created at runtime (CREATE OBJECT – instruction in ABAP Objects).
With their creation they also get their own identity. However, there are no fixed attribute
values linked to the identity. You are probably already wondering how objects get to their
initial state. How do objects recognize ​their ​initial attribute values? How does an object
​ nd not a ​Meyer?
know during its creation that it is a ​Miller a ​

The ​Constructor ​concept exists specifically to answer this question. The constructor is a
method, which runs automatically during the creation of an object. The constructor allows
you to define IMPORTING-parameters.

In ABAP Objects you differentiate between instance-dependent and class-dependent


constructors via the language elements METHODSand CLASS-METHODS to be used in
the definition part and via their names constructor and The class constructor is called by
the first access to a class element (method, attribute, event, object), the (instance)
constructor by the creation of an object (CREATE OBJECT).

You can now create small programs with the help of the ABAP Objects language
instructions you have learned. You know the basic elements of classes (attributes and
methods), the differences between classes and their objects and how to express these in
ABAP Objects.

HERE IS SAMPLE PROGRAM WHO USE CLASS CONSTRUCTOR AND


INSTANCE CONSTRUCTOR.

REPORT
ZS_CLASS_CONSTRUCTOR1.

CLASS FIRST
DEFINITION.
PUBLIC
SECTION.

* CLASS CONSTRUCTOR WILL BE


EXECUTE FIRST . * DEFINING CLASS
CONSTRUCTOR.
CLASS-METHODS
CLASS_CONSTRUCTOR.

* DEFINING INSTANCE
CONSTRUCTOR.
METHODS
CONSTRUCTOR.
ENDCLASS.

* CLASS
IMPLEMENTATION.
CLASS FIRST
IMPLEMENTATION.
*INSTANCE CONSTRUCTOR
IMPLEMENTATION.
METHOD
CONSTRUCTOR.
WRITE ​/ ​'INSTANCE
CONSTRUCTOR'​. ENDMETHOD. ​*
CLASS CONSTRUCTOR
IMPLEMENTATION.
METHOD
CLASS_CONSTRUCTOR.
WRITE ​/ ​'CLASS
CONSTRUCTOR'​.
Page 22 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
END
METHOD.
ENDCLASS.

*EVENT OF REPORT WHERE EXECUTION


OCCCUR.
START-OF-SELECTIO
N. ​* CREATING REFERNCE OF
CLASS FIRS.
DATA ​OBJ ​TYPE REF TO ​FIRST. ​* CREATING OBJECT OF CLASS FIRST THEN IT
WILL CALL FIRST THE CLASS CONSTRUCTOR THEN IT WILL CALL THE * OBJECT
CONSTRUCTOR.

CREATE ​OBJECT
OBJ.

SAMPLE PROGRAM FOR UTILIZING THE CONSTRUCOTR WITH IN


ABAP OOPS.

REPORT
ZS_CLASS_CONSTRUCTOR.
*CLASS DEFINITION. ​CLASS
FIRST ​DEFINITION.
PUBLIC ​SECTION. ​DATA ​ONE(12). ​METHODS ​CONSTRUCTOR
IMPORTING ​A ​TYPE ​C. ​"CONSTRUCTOR DEFINITION. ​ENDCLASS.
*CLASS FIRST IMPLEMENTATION. C ​ LASS FIRST
IMPLEMENTATION.
METHOD ​CONSTRUCTOR. ​"CONSTRUCTOR
IMPLEMENTATION.
ONE = A. ​"ASSIGNING THE VALUE TO ONE ​WRITE: / ​'THIS IS
CONSTRUCTRO WHICH CALL WHEN OBJECT CREATED' ​, ONE.

END
METHOD.
ENDCLASS.

START-OF-SELEC
TION.

DATA ​OBJ ​TYPE REF TO FIRST ​. ​"OBJECT REFERENCE TYPE W ​ RITE ​/ ​'BEFOR
OBJECT CREATION'​. ​CREATE ​OBJECT OBJ ​EXPORTING ​A = ​'SDK'​. ​"CREATING
OBJECT WILL CALL THE CONSTRUCTOR.

Page 23 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
METHOD WITH IMPORT , EXPORT , CHANGING PROPERY AND
CALLING IT.

export and import parameter of the method definition change during import and export
parameter respectively and with in export parameter we use to pass value , while with in
import parameter we pass the data object where we get the value of import parameter
same in changing parameter.

Here is the example


below.

REPORT
ZS_CLASS_CONSTRUCTOR.
DATA ​SECOND(12).

*CLASS DEFINITION.
CLASS FIRST
DEFINITION.
PUBLIC ​SECTION. ​DATA ​ONE(12). ​METHODS ​CONSTRUCTOR
IMPORTING ​A ​TYPE ​C. ​"CONSTRUCTOR DEFINITION. * method
definition with all type of parameter.
METHODS ​SHOWME ​IMPORTING ​A ​TYPE C EXPORTING ​B
TYPE C CHANGING ​D ​TYPE ​C. ENDCLASS. ​"FIRST DEFINITION *CLASS
FIRST IMPLEMENTATION. C​ LASS FIRST ​IMPLEMENTATION.
METHOD ​CONSTRUCTOR. ​"CONSTRUCTOR
IMPLEMENTATION.
ONE = A. ​"ASSIGNING THE VALUE TO ONE ​WRITE: / ​'THIS IS
CONSTRUCTRO WHICH CALL WHEN OBJECT CREATED' ​, ONE.
ENDMETHOD. ​"CONSTRUCTOR * METHOD IMPLEMENTATION
METHOD
SHOWME.
D = ​'HELLO CHANGE'​. B =
'HELLO IMPORT
SECODND'​.

WRITE ​: / ​'THE VALUE OF A IS ' ​, A.


WRITE ​: / ​'THE VALUE OF B IS ' ​, B.
WRITE ​: / ​'THE VALUE OF D IS ' ​, D.
ENDMETHOD. ​"SHOWME ​ENDCLASS.
"FIRST IMPLEMENTATION
START-OF-SELEC
TION.
DATA ​OBJ ​TYPE REF TO FIRST ​. ​"OBJECT REFERENCE TYPE W ​ RITE ​/ ​'BEFOR
OBJECT CREATION'​. ​CREATE ​OBJECT OBJ ​EXPORTING ​A = ​'SDK'​. ​"CREATING
OBJECT WILL CALL THE CONSTRUCTOR. * CALLING THE METHOD SHOWME.
CALL METHOD
OBJ->SHOWME

Page 24 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
EXPORTI
NG
A=
'SHOWME'
IMPORTING
B=
SECOND
CHANGIN
G
D = SECOND. ​WRITE ​: / ​'AFTER CHANGE
PARAMETER THE SECOND VALUE IS : ' ​, SECOND.

Static constructor can be triggered at the beginning of a


processing block(form /event/block/procedure)

Static constructor will only call when either we create object or access the class type of
data or call class type method, other wise it will not call or it call atfirst.

REPORT
ZS_CLASS_STATIC_CONSTRUCTOR
​ LASS
. ​* CLASS first DEFINITION C
FIRST ​DEFINITION.
PUBLIC
SECTION.
CLASS-DATA ​NUM ​TYPE I VALUE ​23. ​"defining class or static data. ​CLASS-METHODS
CLASS_CONSTRUCTOR. ​"for defining class constructor always define with class_constructor.
ENDCLASS. ​"first DEFINITION

* CLASS first
IMPLEMENTATION

CLASS FIRST
IMPLEMENTATION.
METHOD ​CLASS_CONSTRUCTOR. ​"constructor
implementation.
WRITE ​/ ​'hi i am class
constructor'​. ENDMETHOD.
​ NDCLASS. ​"first
"class_constructor E
IMPLEMENTATION

START-OF-SELEC
TION.
WRITE ​: / ​'hello selection'​. ​WRITE ​: / ​'hello class data'​,
FIRST=>NUM. ​"as we access the value of class data the
class constructor will call.

*if we comment the second line write code then static consturctor will not , it will only call when either we
create o bject, call static method or accessing the static attributes.

Page 25 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

Working on
Polymorphism
Polymorphism occurs, where classes implement the same functionality with different
methods (one functionality, several methods but the same name). This can occur via an
inheritance relationship, in that the methods belonging to the superclass are ​redefined ​in
the subclasses and implemented differently. ABAP Objects requires the method names to
be the same and the signature to be the same (signature = method interface).

Polymorphism can be achieved in 2


ways:

• ​Twoindependent classes implement methods with the same names and the same
signature with the intention, that the methods should be called dynamically from a third
location.
• ​A
superclass implements a method, and in a subclass you want to re-implement the
same method, as the superclass implementation is not suitable for the subclass.

The first scenario will not occur very often in ABAP Objects, as the interface concept was
created precisely for such cases (see Interfaces).

POLYMORPHISM:- ​Polymorphism is a characteristic of being able to assign a different


behavior or value in a subclass, to something that was declared in a parent class. For
example, a method can be declared in a parent class, but each subclass can have a
different implementation of that method. This allows each subclass to differ, without the
parent class being explicitly aware that a difference exists.

CLAUSES REGARDING
POLYMORPHISM:-

✓ Allows one interface to be used for a general class of actions. ✓ When objects from
different classes react differently to the same procedural call. ✓ User can work with
different classes in a similar way, regardless of their implementation. 4.Allows improved
code organization and readability as well as creation of “extensible” programs.
5.Although the form of address is always the same, the implementation of the method is
specific to a particular class.

Here in below code First class definition we used to define ADD method , with in the same
class we use to define another class second which inherit the property from FIRST class in
which we redefine the method ADD again ,.

REPORT
ZS_SINGLE_INHERITANCE.
CLASS FIRST ​DEFINITION. ​*
DEFINING ATTRIBUTES.

Page 26 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
PUBLIC ​SECTION. ​DATA
FIRST(12). ​"DEFINING
ATTRIBUTES ​METHODS ​ADD.
"DEFINING METHOD.

ENDCLASS.
* IMPLEMENTING
CLASS.
CLASS FIRST
IMPLEMENTATION.
METHOD ​ADD. ​"METHOD
IMPLEMEMNTATION.
WRITE ​/ ​'HELLO METHOD FROM THE
FIRST'​. ENDMETHOD. ENDCLASS.

* DEFINING OTHER CLASS WHICH INHERIT PROPERTY FROM FIRST AND REDEFINIG THE
FIRST CLASS METHOD.
CLASS ​SECOND ​DEFINITION
INHERITING FROM ​FIRST. ​PUBLIC ​SECTION.
DATA ​SECOND(13). ​METHODS ​HELLO. ​*
REDEFINING THE ADD METHOD OF CLASS
FIRST.
METHODS ADD ​REDEFINITION. ​"REDEFINING THE
ADD METHOD. ​ENDCLASS.

CLASS ​SECOND
IMPLEMENTATION.
METHOD
HELLO.
WRITE ​/ ​'HELLO FROM
SECOND'​. ENDMETHOD.
METHOD ​ADD.
WRITE ​/ ​'FROM THE SUBCLASS
REDEFINITION'​. ENDMETHOD.
ENDCLASS.

*EVENT WHERE DATA EXECUTION


OCCUR.
START-OF-SELEC
TION. ​* CREATE REFERNCE
OF OBJECT. D ​ ATA ​OBJ
TYPE REF TO ​FIRST. ​*
CREATING OBJECT.
CREATE
OBJECT OBJ. ​* CALLING
MEHTOD OF CLASS.
CALL METHOD
OBJ->ADD.

* CREATE REF OF
SECOND CLASS
DATA ​OBJ1 ​TYPE
REF TO ​SECOND. ​*
CREATING OBJECT
Page 27 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CREATE ​OBJECT
OBJ1. ​* CALLING
THE METHOD.
CALL METHOD ​OBJ1->HELLO. ​* CALLING
THE SUPER CLASS METHOD USING SUBCLASS
OBJECT.
CALL METHOD
OBJ1->ADD.

Working with
Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An


interface only has a declaration part, and do not have visibility sections. Components
(Attributes, methods, constants, types) can be defined the same way as in classes.

✓ ​Interfaces are listed in the definition part of the class, and must always be in the
PUBLIC
SECTION. ​✓ ​Operations defined in the interface are implemented as methods
of the class. All methods
of the interface must be present in the implementation part of the class. ​✓
Attributes, events, constants and types defined in the interface are automatically
available
to the class carrying out the implementation. ​✓ ​Interface components are
addressed in the class by <interface name>~<component name>

REPORT
ZS_INTERFACE.
*DEFINING THE INTERFACE AND ITS METHOD OR ATTRIBUTES I​ NTERFACE ​FIRST. ​*METHOD
DEFININTION ALWAYS STARTS WITH METHODS NOT METHOD AND IN INTERFACE ATTRIBUTES
AND MET HODS ARE PUBLIC. ​DATA ​NAME(12). ​METHODS ​SHOW.

ENDINTERFA
CE.

* IMPLEMENTING THE
INTERFACE .
CLASS ​IMPLEMENT_INTERFACE DEFINITION. ​"CLASS
DEFINITION.
PUBLIC ​SECTION. ​* INTERFACE WHICH YOU WANT TO IMPLEMENT WITH IN
A CLASS MUST PASS WITH IN PUBLIC SECTION
INTERFACES ​FIRST. ​"INTERFACE WHICH I WANT TO IMPLEMENT WITH
IN THIS CLASS.
METHODS
METHOD1.
ENDCLASS.

* IMPLEMENTING
CLASS.
Page 28 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CLASS ​IMPLEMENT_INTERFACE
IMPLEMENTATION. ​* IMPLEMENTIONG
INTERFACE METHOD.
METHOD
FIRST~SHOW.
WRITE ​/ ​'THIS IS INTERFACE
METHOD.'​. ENDMETHOD.

* IMPLEMENTING CLASS
METHOD.
METHOD
METHOD1.
WRITE ​/ ​'THIS IS CLASS
METHOD'​. ENDMETHOD.

ENDCLA
SS.

* EVENT
START-OF-SELECTION.
START-OF-SELECTION. ​DATA ​OBJ
TYPE REF TO ​IMPLEMENT_INTERFACE.
CREATE ​OBJECT OBJ. ​* CALLING THE
INTERFACE METHOD. ​CALL METHOD
OBJ->FIRST~SHOW. ​* CALLING THE CLASS
METHOD.
CALL METHOD
OBJ->METHOD1.

Interfaces can only be implemented in the public section of a class if you implement it
either in private or protected section it will give you compilation error.

A class with an interface should implement all the methods of that interface other wise it
will give compiler error.

ABAP
Objects :

• ABAP Objects is an upwards-compatible extension of the existing ABAP


language.
• You can use existing ABAP statements within ABAP
Objects.
• You can use ABAP Objects within existing
programs.
• ABAP Objects is full integrated in the ABAP
debugger.
• Instances of function groups as
objects
• Example: Function group as
counter.

Page 29 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Elements of ABAP
Objects :

• Classes
• Interfaces
• Objects
• Object
References

Inheritance
Classes :

• Classes are the central element of


Object-orientation
• A class describes a general element or a general concept, for example the abstract
concepts Business Partner, Material, Transaction, Equipment or list. Classes realize an
abstract data types.
• You can define classes globally using the class builder ​(Transaction SE24) ​or locally
in an ABAP program.
• Local classes and interfaces can only be used in the program in which they are
defined.

Components of
classes.

• Attributes
• Methods
• Constructor
• Events
• Types and
Constants.

In ABAP Objects classes are made up of a definition and an


implementation part.

*Declaring class definition attribute and


property.
CLASS FIRST ​DEFINITION. ​* BODY OF THIS CLASS OR DEFINE METHODS
CONSTRUCTOR TYPES AND EVETNT
ENDCLA
SS.

*Implementation of that class and its


method.
CLASS FIRST ​IMPLEMENTATION. ​*IMPLEMENT METHODS AND CLASS WITH IN THE
CLASS.

ENDCLA
SS.
Page 30 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Attribute
s.

Attributes can take on values within an object at runtime. The sum of all attributes and
values describes the state of an object.

In ABAP Objects attributes are defined in the definition part via the ABAP keyword DATA.
In this example we have defined that the name of a business partner may be 50
characters long and is of the data type Character:

CLASS cl_party DEFINITION. PUBLIC SECTION. "I will return later to the meaning of
the language element Public Section see ​Visibility ​DATA: name(50) type C.
ENDCLASS.

Here name is a attribute of class


cl_party .

Attributes can be defined as ​instance dependent a


​ s well as ​Class
dependent​.

Class attributes (Class attributes are also called ​static attributes​) are not tied to a single
instance, rather they "belong" to all instances of the Class. These attributes exist only
once in main memory. Instance-dependent attributes exist once per instance and are tied
to a single instance. For example, the attribute name is an instance-dependent attribute,
while the attribute instance_count could be a Class-dependent attribute, in which the
number of instances of the Class cl_party could be noted.

In ABAP Objects you differentiate between instance-dependent and class-dependent


attributes by means of the ABAP keywords ​DATA ​or ​CLASS-DATA ​to be used in the
definition part:

CLASS cl_party DEFINITION. PUBLIC SECTION.


CLASS-DATA: instance_count type i. DATA: name(50) type c.
ENDCLASS. START-OF-SELECTION. DATA: instance TYPE
REF TO cl_party. * Access to the different attribute types is
also defined in the syntax: * Class attribute:
cl_class​=>​instance_count * Instance attribute: instance​->​name

Page 31 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
IF cl_class=>instance_count
< 50. CREATE OBJECT

instance. ... ​ENDIF. IF



instance->name <> 'Miller'. ...

ENDIF.

*Declaring class definition attribute and


property.
CLASS
FIRST ​DEFINITION. ​*
public section of class.
PUBLIC ​SECTION. ​data
instance_data(12).
class-data ​class_data(12).
METHODS: SHOWME
,SAWME.
ENDCLASS. ​*Implementation
of that class and its method.
CLASS FIRST
IMPLEMENTATION. ​*
Method implementation.
METHOD
SHOWME.
WRITE ​'SHOW ME
METHOD'​.
ENDMETHOD.

METHOD
SAWME.
WRITE ​/ ​'SAW WITH IN THE
SUPERCLASS'​. ENDMETHOD.

ENDCLASS. ​* ​*always create refernce and object

in START-OF- SELECTION
​ Event because the data extraction
come under this event.

START-OF-SELEC
TION.

*creating reference of the


object. ​DATA ​FIRSTOBJ ​TYPE
REF TO ​FIRST. ​*creating
object of the reference.
CREATE ​OBJECT FIRSTOBJ.

*using the class and instance


object.

FIRSTOBJ->instance_data = ​'INSTNCE DATA'​. ​"GIVING THE VALUE TO


INSTANCE ATTRIBUTE. ​FIRSTOBJ->CLASS_DATA = ​'CLASS DATA'​. ​"GIVING
THE VALUE TO CLASS ATTRIBUTE.
Page 32 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
WRITE ​: / ​'ACCESSING CLASS DATA : '​, FIRSTOBJ->CLASS_DATA.
WRITE ​: / ​'FIRST OBJECT INSTANCE METHOD DATA : ' ​,
FIRSTOBJ->INSTANCE_DATA.

*CREATING ANTOTHER REFERENCE AND OBJ OF


FIRST CLASS. ​DATA ​FIRSTOBJ1 ​TYPE REF TO
FIRST. ​CREATE ​OBJECT FIRSTOBJ1.

*ACCESSING THE CLASS DATA OF FIRSTOBJ1. W ​ RITE ​: / ​'ACCESSING


THE CLASS DATA USING ANOTHER OBJECT :' ​, FIRSTOBJ1-
>CLASS_DATA. ​"HERE IT WILL GIVE OUTPUT *BECUASE THIS IS CLASS
DATA. ​WRITE ​: / ​'ACCESSING THE INSTANCE DATA USING ANOTHER
OBJECT : ' ​, FIRSTOBJ1- >INSTANCE_DATA. ​"HERE IS GIVE BLNAK
VALUE *BECUASE HERE NOT SPECIFY THE VALUE OF INSTANCE_DATA
FOR OBJECT FIRSTOBJ1 ,

Here in above program we can see that the class attribute value remain same for all object
same type class , where as the instance data of class have different value for each class.

Method
s

As well as attributes, Classes have so-called ​Methods ​(you could say functions instead of
methods but, as mentioned in the introduction: New technologies require new names).
While attributes describe the static structure of a class and its objects, Methods describe
the ​behavior ​of objects within a class. With the help of methods, the system provides
operations, services and functions. Via methods, a user can manipulate the objects in a
class or also the class itself. As for attributes, there are instance-dependent as well as
class-dependent (static) methods.

In order to carry out instance-dependent (or instance-dependent) methods, the calling


program needs a specific instance of the class. That is, the calling program must have
a defined reference variable, that points to a specific instance. Class methods are not
instance- dependent. They can be called at any time by a user. To see how the syntax
calls the various method types, see the following example.

REPORT
ZS_CLASS.

*Declaring class definition attribute and


property. ​CLASS FIRST ​DEFINITION. ​*
public section of class.
PUBLIC
SECTION.
data ​instance_data(12). ​"INSTANCE DATA
DELCARATION.
Page 33 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
class-data ​class_data(12). ​"CLASS DATA DELCARATION.
METHODS: SHOWME ,SAWME. ​"INSTANCE METHODS
DECLARATION. ​CLASS-METHODS:CLASSMETHOD_SHOWME.
"CLASS METHOD DECALRATION.

ENDCLASS. ​"FIRST DEFINITION


*Implementation of that class and its
method. ​CLASS FIRST
IMPLEMENTATION. ​* INSTNACE
Method implementation.
METHOD
SHOWME.
WRITE ​'SHOW ME
METHOD'​. ENDMETHOD.
"SHOWME *INSTANSE METHOD
IMPLEMENTATION.
METHOD
SAWME.
WRITE ​/ ​'SAW WITH IN THE
SUPERCLASS'​. ENDMETHOD.
"SAWME *CLASS METHOD
IMPLEMENTATION.
METHOD ​CLASSMETHOD_SHOWME. ​WRITE ​/ ​'THIS IS CLASS METHOD.'​. ENDMETHOD.
ENDCLASS. ​"END OF FIRST CLASS IMPLEMENTATION


* *always create refernce and object in START-OF-
SELECTION Event because the data extraction come under
this event.

START-OF-SELEC
TION.

*creating reference of the


object.
DATA ​FIRSTOBJ ​TYPE REF TO
FIRST. ​DATA ​SECONDOBJ ​TYPE
REF TO ​SECOND.

*creating object of the


reference.
CREATE ​OBJECT
FIRSTOBJ. ​*calling the
method of that class.

CALL METHOD
FIRSTOBJ->SHOWME. ​*Creating
​ REATE
object of second class C
OBJECT SECONDOBJ. ​*calling
method of subclass and superclass.

CALL METHOD
SECONDOBJ->SHOWME. ​CALL
METHOD ​SECONDOBJ->SAWME.

*CALLING CLASS
METHOD.
Page 34 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CALL METHOD
FIRSTOBJ->CLASSMETHOD_SHOWME.

*using the class and instance


object.

FIRSTOBJ->instance_data = ​'INSTNCE DATA'​. ​"GIVING THE VALUE TO


INSTANCE ATTRIBUTE. ​FIRSTOBJ->CLASS_DATA = ​'CLASS DATA'​. ​"GIVING
THE VALUE TO CLASS ATTRIBUTE. W ​ RITE ​: / ​'ACCESSING CLASS DATA : '​,
FIRSTOBJ->CLASS_DATA. ​"ACCESSING CLASS DATA . W ​ RITE ​: / ​'FIRST
OBJECT INSTANCE METHOD DATA : ' ​, FIRSTOBJ- >INSTANCE_DATA.
"ACCESSING THE INSTACNE DATA.

*CREATING ANTOTHER REFERENCE AND OBJ OF


FIRST CLASS.
DATA ​FIRSTOBJ1 ​TYPE REF
TO ​FIRST. ​CREATE ​OBJECT
FIRSTOBJ1.

*ACCESSING THE CLASS DATA OF


FIRSTOBJ1.
WRITE ​: / ​'ACCESSING THE CLASS DATA USING
ANOTHER OBJECT :' ​, FIRSTOBJ1- >CLASS_DATA. ​"HERE IT WILL
GIVE OUTPUT *BECUASE THIS IS CLASS DATA.
WRITE ​: / ​'ACCESSING THE INSTANCE DATA USING ANOTHER
OBJECT : ' ​, FIRSTOBJ1- >INSTANCE_DATA. ​"HERE IS GIVE BLNAK
VALUE *BECUASE HERE NOT SPECIFY THE VALUE OF INSTANCE_DATA
FOR OBJECT FIRSTOBJ1 ,

CONSTRUCTOR IN ABAP
CLASS

Objects must be created at runtime (CREATE OBJECT – instruction in ABAP Objects).


With their creation they also get their own identity. However, there are no fixed attribute
values linked to the identity. You are probably already wondering how objects get to their
initial state. How do objects recognize ​their ​initial attribute values? How does an object
​ nd not a ​Meyer?
know during its creation that it is a ​Miller a ​ The ​Constructor ​concept
exists specifically to answer this question. The constructor is a method which runs
automatically during the creation of an object. ​The constructor allows you to define
IMPORTING-parameters.

In ABAP Objects you differentiate between instance-dependent and class-dependent


constructors via the language elements METHODSand CLASS-METHODS to be used in
the definition part and via their names constructor and class_constructor:

Page 35 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
There are two types of constructor in ABAP
CLASS.

1. ​Instance
construcror ​2. ​Class
constructor

Instance constructor will call when you create object using that
constructor.

EVENTS IN ABAP
CLASS.

The steps to be followed are as


follows:-

❖ Create an event in a class ❖ Create a triggering method in the


same class which will raise the event. ❖ Create an event handler
method for the event in same/other class. ❖ Register the event
handler method in the program.

Now, your settings are complete. Create an object from the class containing the event and
call the triggering method to raise the event.

* CLASS c1
DEFINITION c​ lass ​c1
definition.
public
section. ​*(1)Creating
event : E1
events: e1. ​*(2) Creating an event handling
method. This method can belong to * same or different class
methods: m1 ​for
event ​e1 ​of ​c1. ​* Method to
raise the event
methods ​: t1. endclass. ​"c1
DEFINITION * CLASS c1 IMPLEMENTATION
class ​c1 implementation. ​*Method : M1 will be
called when the event is raised
method ​:
m1.
write:/5 ​'I am the event
handler method'​. endmethod. ​": *
Method : T1 will raise the event
method ​:
t1.
write:/5 ​'I am T1, going to raise event
E1'​. ​raise event ​e1. endmethod. ​":
endclass. ​"c1 IMPLEMENTATION

*start of selection , form where execution


occurs. ​start-of-selection.
Page 36 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
data: oref ​type ref to ​c1. ​create
object: oref . ​* Registering the event
handler method
set handler ​oref->m1 ​for ​oref .
* Calling the event which will raise the
event.
call method
oref->t1.

HANDLING EVENT OF ONE CLASS IN


ANOTHER CLASS.

REPORT
ZS_CLASS_EVENT5.
CLASS ​c1 DEFINITION.
PUBLIC ​SECTION. ​*
Creating event : E1
EVENTS: E1. ​*
Triggering method : T1
METHODS ​: T1.
ENDCLASS.

CLASS ​C2
DEFINITION.
PUBLIC ​SECTION. ​*
Creating an event handling
method. ​METHODS: M1 ​FOR
EVENT ​E1 ​OF ​c1. endclass.

CLASS ​c1
IMPLEMENTATION. ​* Method
: T1 will raise the event
METHOD ​:
T1.
write:/5 ​'I am T1, going to raise event
E1'​. ​raise event ​E1. ENDMETHOD.
ENDCLASS.

class ​c2 implementation. ​* Method : M1 will be


called when the event is raised M​ ETHOD ​: M1.
write:/5 ​' I am the event handler
method in c2'​. ENDMETHOD. endclass.

Start-of-selection. Data: oref1 ​type ref to


c1, oref2 ​type ref to ​c2. ​Create ​object:
oref1 , oref2 . ​* Registering the event
handler method ​SET HANDLER
oref2->M1 ​FOR ​oref1 . ​* Calling the
event which will raise the event.
call method
oref1->T1.

Page 37 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)

More than one event handler method can exist for


same event

For an event in a class, there can be more than one event handler methods in same or
different class. However, at runtime only one event handler method will be triggered at a
time, based on the registration.

❖ ​Class ​C1 ​contains an event ​E1​, for which the triggering method is ​T1 ​and the event
handler
methods are :- ➢ ​M1 ​in same class ​C1​. ➢ ​M2 ​in another class ​C2​. ​❖ ​In the
START-OF-SELECTION block, objects are created from class ​C1 ​and ​C2​. ​❖ ​First,
registration is made using method ​M1 ​of class ​C1 ​as event handler method. ​❖ ​Then,
the event ​E1 ​is raised, calling method ​T1​. This raises event handler method ​M1 ​of
class ​C1​. ​❖ ​After that, the earlier registration is de-activated and new
registration is made for method
M2 ​of class ​C2 ​as event handler method . ​❖ ​Event ​E1 ​is raised calling
method ​T1​. This raises event handler method ​M2 ​of class ​C2​.

REPORT ​ZS_CLASS_EVENT6.
CLASS ​c1 DEFINITION. ​PUBLIC
SECTION. ​* Creating event : E1
EVENTS: E1. ​* Creating an event
handling method. ​METHODS: M1
FOR EVENT ​E1 ​OF ​c1. ​* Method
to raise the event ​METHODS ​: T1.
ENDCLASS.

CLASS ​C2
DEFINITION.
PUBLIC ​SECTION. ​*
Creating an event handling
method. ​METHODS: M2 ​FOR
EVENT ​E1 ​OF ​c1. endclass.

CLASS ​c1
IMPLEMENTATION. ​* Method
: T1 will raise the event
METHOD ​:
T1.
write:/5 ​'I am T1, going to raise event E1'​. ​raise
event ​E1. ENDMETHOD. ​* Method : M1 will be
called when the event is raised M ​ ETHOD ​: M1.
Page 38 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
write:/5 ​' I am the event handler
method M1 in c1'​. ENDMETHOD. ENDCLASS.

class ​c2 implementation. ​* Method : M2 will be


called when the event is raised M​ ETHOD ​: M2.
write:/5 ​' I am the event handler
method M2 in c2'​. ENDMETHOD. endclass.

Start-of-selection. Data: oref1 ​type ref to


c1, oref2 ​type ref to ​c2. ​Create ​object:
oref1 , oref2 . ​* Registering the event
handler method ​SET HANDLER
oref1->M1 ​FOR ​oref1 . ​* Calling the
event which will raise the event.
call method ​oref1->T1. ​* De-Registering
the earlier event handler method ​SET HANDLER
oref1->M1 ​FOR ​oref1 ACTIVATION space . ​*
Registering the new event handler method ​SET
HANDLER ​oref2->M2 ​FOR ​oref1 . ​* Calling the
event which will raise the event.
call method
oref1->T1.

Use of static event ​Static methods can only raise static events. The FOR...addition
is not required to register for static events. ​Here Class ​C1 ​contains a static event ​E1 ​and

static triggering method ​T1​. The event handler method


​ ​M1 ​is in class ​C1 ​itself. At the time
of registering event ​M1 ​as event handler method, the ​FOR...​addition is omitted.

REPORT ​ZS_CLASS_EVENT7.
CLASS ​c1 DEFINITION. ​PUBLIC
SECTION. ​* Creating event : E1
CLASS-EVENTS: E1. ​* Creating
an event handling method.
METHODS: M1 ​FOR EVENT ​E1
OF ​c1. ​* Method to raise the event
CLASS-METHODS ​: T1.
ENDCLASS.

CLASS ​c1
IMPLEMENTATION.

Page 39 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
* Method : T1 will raise the
event
METHOD ​:
T1.
write:/5 ​'I am T1, going to raise event E1'​. ​raise
event ​E1. ENDMETHOD. ​* Method : M1 will be
called when the event is raised M ​ ETHOD ​: M1.
write:/5 ​' I am the event handler
method M1 in c1'​. ENDMETHOD. ENDCLASS.

Start-of-selection. Data: oref1 ​type ref to


c1. ​Create ​object: oref1 . ​* Registering
the event handler method S ​ ET
HANDLER ​oref1->M1 . ​* Calling the
event which will raise the event.
call method
oref1->T1.

Event with export parameters. ​Events can have export parameters, which it
passes to its event handler method. The triggering method must pass values for all the
exporting parameters of the event while raising the event using RAISE EVENT statement.
The interface of an event handler method consists of a list of ​IMPORTING ​parameters,
whose names are identical with those in the EXPORTING list and which are automatically
created from the interface of the event. Each handler method can however specify which
event parameters it wants to handle and which it does not.
Here Class ​C1 ​contains event E1 which exports two parameters , ​NUM1 ​and ​NUM2 ​to its
event handler method , ​M1 ​in class ​C1​. Method ​T1 ​is the triggering method for the event,
which passes values to the EXPORTING parameters of the event at the time of ​RAISE
EVENT ​statement.

REPORT
ZS_CLASS_EVENT8.
CLASS ​c1 DEFINITION.
PUBLIC
SECTION.
EVENTS ​: E1 ​EXPORTING
value(NUM1) ​TYPE I ​value(NUM2)
TYPE ​I.

METHODS ​: M1 ​FOR EVENT ​E1


OF ​C1
IMPORTING ​NUM1
NUM2 . ​METHODS ​: T1.
ENDCLASS.

Page 40 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CLASS ​C1
IMPLEMENTATION.
METHOD ​:
M1.
WRITE:/5 ​'First input ' ​, num1
. write:/5 ​'Second input ' ​,
num2 . ENDMETHOD.

METHOD
T1.
RAISE EVENT ​E1 ​exporting
num1 = ​2
n
um2 = ​3.
ENDMETHOD.
ENDCLASS.

START-OF-SELEC
TION.
DATA ​: oref ​TYPE REF TO
c1. ​CREATE ​OBJECT oref.
SET HANDLER ​oref->M1 ​for
oref. ​call method ​oref->T1.

THIS IS THE SAMPLE PROGRAM WHICH USE INTERFACE AND EVENTS


WITH IN CLASS.

REPORT
ZS_CLASS_EVENT.

*-----------------------------------------------------------------
----* * INTERFACE lif_employee
*-----------------------------------------------------------------
----* I​ NTERFACE ​lif_employee.
METHOD
S:
add_employ
ee
IMPORTING ​im_no
TYPE i
im_name ​TYPE ​string im_wage ​TYPE ​i. ENDINTERFACE.
******************************************************* * Super
class LCL_CompanyEmployees
******************************************************* ​CLASS
lcl_company_employees DEFINITION.
PUBLIC
SECTION.
TYPES: ​BEGIN OF
t_employee,
no TYPE ​i, name ​TYPE ​string, wage ​TYPE ​i, ​END OF
t_employee. ​* Declare event. Note that declaration could
also be placed in the * interface
EVENTS:
employee_added_to_list

Page 41 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
EXPORTING ​value(ex_employee_name) ​TYPE ​string.
* CLASS-EVENTS: Events can also be defined as
class-events
INTERFACES
lif_employee.
METHODS:
constructor,
display_employee_list,
display_no_of_employees,
* Declare event method
on_employee_added_to_list ​FOR EVENT ​employee_added_to_list ​OF
lcl_company_employees
IMPORTING
ex_employee_name sender. ​PRIVATE
SECTION.
CLASS-DA
TA:
i_employee_list ​TYPE TABLE OF
t_employee, no_of_employees ​TYPE ​i.
ENDCLASS. ​*-- CLASS
LCL_CompanyEmployees IMPLEMENTATION
CLASS ​lcl_company_employees
IMPLEMENTATION.
METHOD
constructor.
no_of_employees =
no_of_employees + ​1. ​ENDMETHOD.
METHOD ​lif_employee~add_employee. ​*
Adds a new employee to the list of
employees
DATA: l_employee ​TYPE ​t_employee.
l_employee-no = im_no.
l_employee-name = im_name.
l_employee-wage = im_wage.
APPEND ​l_employee ​TO
i_employee_list. ​* Raise event
employee_added_to_list
RAISE EVENT
employee_added_to_list
EXPORTING ​ex_employee_name =
l_employee-name. ENDMETHOD. ​METHOD
display_employee_list. ​* Displays all employees and
there wage ​DATA: l_employee ​TYPE ​t_employee.
WRITE: / ​'List of Employees'​. ​LOOP AT
i_employee_list ​INTO ​l_employee.
WRITE: / l_employee-no, l_employee-name,
l_employee-wage. ENDLOOP. ENDMETHOD. ​METHOD
display_no_of_employees. ​* Displays total number of
employees
SKIP ​3. ​WRITE: / ​'Total number of employees:'​,
no_of_employees. ENDMETHOD. ​METHOD
on_employee_added_to_list. ​* Event method
WRITE: / ​'Employee added to list'​,
ex_employee_name. ENDMETHOD. ENDCLASS.
*******************************************************

Page 42 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
* Sub class LCL_BlueCollar_Employee
******************************************************* ​CLASS
lcl_bluecollar_employee ​DEFINITION
INHERITING FROM
lcl_company_employees. ​PUBLIC ​SECTION.
METHOD
S:
constructo
r
IMPORTING ​im_no ​TYPE i ​im_name ​TYPE
string im_hours ​TYPE i ​im_hourly_payment
TYPE ​i, lif_employee~add_employee
REDEFINITION.. ​PRIVATE ​SECTION.
DATA:no ​TYPE ​i,
name ​TYPE ​string,
hours ​TYPE ​i,
hourly_payment
TYPE ​i.

ENDCLASS. ​CLASS ​lcl_bluecollar_employee


IMPLEMENTATION.
METHOD ​constructor. ​* The superclass constructor
method must be called from the subclass * constructor method
CALL METHOD ​super->constructor. ​no ​= im_no. name =
im_name. hours = im_hours. hourly_payment =
im_hourly_payment. ENDMETHOD. ​METHOD
lif_employee~add_employee. ​* Calculate wage an call the
superclass method add_employee to add * the employee to the
employee list
DATA: l_wage ​TYPE ​i. l_wage = hours *
hourly_payment. ​CALL METHOD
super->lif_employee~add_employee
EXPORTING ​im_no
= ​no
im_name = name
im_wage = l_wage.
ENDMETHOD.

ENDCLASS. ​******************************************************* * Sub class


LCL_WhiteCollar_Employee *******************************************************
CLASS ​lcl_whitecollar_employee ​DEFINITION INHERITING FROM
lcl_company_employees.
PUBLIC
SECTION.
METHOD
S:
constructo
r
IMPORTING ​im_no ​TYPE i
im_name ​TYPE ​string

Page 43 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
im_monthly_salary ​TYPE i
im_monthly_deductions ​TYPE ​i,
lif_employee~add_employee
REDEFINITION. ​PRIVATE ​SECTION.
DAT
A:
no TYPE ​i, name ​TYPE
string, monthly_salary
TYPE ​i,
monthly_deductions ​TYPE
i.

ENDCLASS. ​CLASS ​lcl_whitecollar_employee


IMPLEMENTATION. ​METHOD ​constructor. ​* The superclass
constructor method must be called from the subclass *
constructor method
CALL METHOD ​super->constructor. ​no ​= im_no. name =
im_name. monthly_salary = im_monthly_salary.
monthly_deductions = im_monthly_deductions. ENDMETHOD.
METHOD ​lif_employee~add_employee. ​* Calculate wage an call
the superclass method add_employee to add * the employee to
the employee list
DATA: l_wage ​TYPE ​i. l_wage =
monthly_salary - monthly_deductions. ​CALL
METHOD ​super->lif_employee~add_employee
EXPORTING ​im_no
= ​no
im_name = name
im_wage = l_wage.
ENDMETHOD.

ENDCLASS. ​*******************************************************
*REPORT
******************************************************* ​DATA: ​*
Object references
o_bluecollar_employee1 ​TYPE REF TO
lcl_bluecollar_employee, o_whitecollar_employee1 ​TYPE
REF TO ​lcl_whitecollar_employee. START-OF-SELECTION.
* Create bluecollar employee obeject
CREATE ​OBJECT
o_bluecollar_employee1
EXPORTING ​im_no
= ​1
im_name = ​'SONU KUMAR' ​im_hours =
38 ​im_hourly_payment = ​75. ​* Register
event for o_bluecollar_employee1
SET HANDLER
o_bluecollar_employee1->on_employee_added_to_list
FOR
o_bluecollar_employee1.

Page 44 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
* Add bluecollar employee to employee
list
CALL METHOD
o_bluecollar_employee1->lif_employee~add_employee
EXPORTING ​im_no
= ​1
im_name = ​'VIKALP' ​im_wage = ​0.
* Create whitecollar employee
obeject
CREATE ​OBJECT
o_whitecollar_employee1
EXPORTING ​im_no
= ​2
im_name = ​'VAIBHAV'
im_monthly_salary = ​10000
im_monthly_deductions = ​2500. ​*
Register event for
o_whitecollar_employee1
SET HANDLER
o_whitecollar_employee1->on_employee_added_to_list
FOR
o_whitecollar_employee1.

* Add bluecollar employee to employee


list
CALL METHOD
o_whitecollar_employee1->lif_employee~add_employee
EXPORTING ​im_no
= ​1
im_name = ​'AMIT' ​im_wage = ​0. ​* Display employee list and
number of employees. Note that the result * will be the same
when called from o_whitecollar_employee1 or *
o_bluecolarcollar_employee1, because the methods are defined
* as static (CLASS-METHODS)
CALL METHOD
o_whitecollar_employee1->display_employee_list. ​CALL
METHOD
o_whitecollar_employee1->display_no_of_employees.

ANOTHER EXAMPLE USING EVENTS


IN ABAP

REPORT
ZS_CLASS_EVENT1.
*CLASS DEFININTION.
CLASS ​cl_class
DEFINITION.
PUBLIC
SECTION.
EVENTS: object_created ​EXPORTING ​value(ex_reference) ​TYPE REF
TO ​cl_class. ​METHODS ​CONSTRUCTOR.

* DEFINING EVENTS FOR HANDLING THE


RAISED EVENTS.
METHODS ​HANDLEEVENT ​FOR EVENT
OBJECT_CREATED ​OF ​CL_CLASS. ENDCLASS.

*CLASS IMPLEMENTATION.
CLASS ​cl_class
IMPLEMENTATION.
METHOD ​CONSTRUCTOR. ​*
setting the handler TYPE FOR ALL
INSTANCES.
SET HANDLER ​HANDLEEVENT ​FOR ALL
INSTANCES. ​* RAISING THE EVENT WHEN THE OBJECT
CREATED OF THIS CLASS.

Page 45 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
RAISE EVENT ​object_created ​EXPORTING ​ex_reference = me. ​"me references the
current instance

ENDMETHO
D.

*IMPLEMENTING THE EVENT HANDLER


METHOD.
METHOD
HANDLEEVENT.
WRITE ​/ ​'EVENT
RAISED'​.

EN
DMETHOD.
ENDCLASS.
*CREATING REFERENCE OF CLASS
​ ATA: OBJ ​TYPE REF TO
CL_CLASS. D
cl_class.

START-OF-SELEC
TION. ​*CREATING
OBJECT. C ​ REATE
OBJECT OBJ.

OTHER EXMPLE FOR HANDLING


EVNET ​REPORT
ZS_CLASS_EVENT2. ​CLASS
lcl_dog DEFINITION.
PUBL
IC ​SECTION. ​*
Declare events
EVENT
S:
dog_is_hung
ry
EXPORTING
value(ex_time_since_last_meal) ​TYPE ​i.
METHODS:
constructo
r
IMPORTING ​im_name
TYPE ​string,
set_time_since_last_meal
IMPORTING ​im_time ​TYPE ​i,
on_dog_is_hungry ​FOR EVENT ​dog_is_hungry ​OF
lcl_dog
IMPORTING
ex_time_since_last_meal. ENDCLASS.
*-----------------------------------------------------------------
----* * CLASS lcl_dog IMPLEMENTATION
*-----------------------------------------------------------------
----* ​CLASS ​lcl_dog IMPLEMENTATION. ​*
constructor.
METHOD
constructor.
WRITE: / ​'I am a dog and my name
is'​, im_name. ENDMETHOD. ​METHOD
set_time_since_last_meal.
IF ​im_time <
4.
SKIP ​1. ​WRITE: / ​'You fool, I am not
hungry yet'​. ELSE. ​* Subsrcribe for
event:

Page 46 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
* set handler <Event handler method> * FOR
<ref_sender>!FOR ALL INSTANCES [ACTIVATION
<var>]
SET HANDLER ​on_dog_is_hungry ​FOR ALL
INSTANCES ACTIVATION ​'X'​. ​* Raise event
RAISE EVENT
dog_is_hungry
EXPORTING ​ex_time_since_last_meal =
im_time. ENDIF. ENDMETHOD. ​METHOD
on_dog_is_hungry. ​* Event method, called when the event
dog_is_hungry is raised
SKIP ​1. ​WRITE: / ​'Hey owner i have not take
meal since'​,
ex_time_since_last_meal, ​' hours'​.
WRITE: / ​'Give me something to eat NOW!'​.
ENDMETHOD. ENDCLASS.
*-----------------------------------------------------------------
----* * R E P O R T
*-----------------------------------------------------------------
----* ​DATA: o_dog1 ​TYPE REF TO ​lcl_dog.

START-OF-SELEC
TION.
CREATE ​OBJECT o_dog1 ​EXPORTING
im_name = ​'kalu'​. ​CALL METHOD
o_dog1->set_time_since_last_meal
EXPORTING ​im_time = ​2. ​* This
method call will raise the event dog_is_hungy *
because time > 3
CALL METHOD
o_dog1->set_time_since_last_meal
EXPORTING ​im_time
= ​5.

ANOTHER EXAMPLE FOR HANDLING


THE EVENT.

REPORT
ZS_CLASS_EVENT3.

*DEFINING THE
CLASS. ​CLASS
VIKALP DEFINITION.
PUBLIC
SECTION. ​*
CREATING A
EVENT.
EVENTS ​EVENT1. ​* CREATING AN EVENT HANDLER METHOD THIS METHOD
CAN BELONG TO SAME OR DIFFERENT CLASS.
METHODS ​METHOD1 ​FOR EVENT
EVENT1 ​OF ​VIKALP. ​* METHODTO RAISE THE
EVENT.
METHODS
TRIGGER.

ENDCLA
SS.
* IMPLEMENTING THE
CLASS.
CLASS ​VIKALP
IMPLEMENTATION.
METHOD
METHOD1.
Page 47 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
WRITE ​/ ​'I AM EVENT HANDLER
METHOD'​.

ENDMETHO
D.

METHOD
TRIGGER.
WRITE ​/ ​'I M RAISING AN EVENT
EVENT1'​. ​RAISE EVENT ​EVENT1.
"RAISING THE EVENT.

ENDMETH
OD.
ENDCLASS.

START-OF-SELECTION. ​DATA ​OBJ ​TYPE


REF TO ​VIKALP. ​CREATE ​OBJECT OBJ. ​*
REGISTERING THE EVENT HANDLER
METHOD.
SET HANDLER ​OBJ->METHOD1 ​FOR
OBJ. ​* CALLING THE METHOD WHICH WILL
TRIGGER THE EVENT.
CALL METHOD
OBJ->TRIGGER.
Events allow for the loose coupling of components (classes or objects) in a system. The
event trigger does not normally know at the time of coding who is going to react to the
event. Those components, which want to react to the event, register at the event runtime,
in that they tell the runtime environment which method is to be executed when the event is
raised. In this way many components can register for an event.

Event handler methods can proceed synchronously as well as asynchronously. At present,


ABAP Objects only supports synchronous calling of the event handler method.

Events in
class.

When an event occurs – for example the event ‘mouse click occurred’ in the user
interfaces or new account created in a business application , it needs to be made known to
the interested event handlers. Event and event handling can be compared to
procedure(method) calls : raising and

Page 48 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
event corresponds to calling procedure and handling an event corresponds to the called
procedure (the callee) , The main difference b/w procedure calls and event concept is that
a procedure caller(object) and callee(method) are tightly coupled but with events they are
decoupled .
While procedural ABAP programs can handle events from the runtime environment ,
ABAP objects provides a complete event concept that allows classes and objects to both
raise and handle events.

The Procedural approach to Events. ​With in


procedural ABAP development , we can not execute program without handling events
because ABAP is event-driven procedural programming language.

In order to executed by SUBMIT , a program must contain START-OF-SELECTION event


, which is triggered by the runtime environment. The screen flow logic must contain
handlers for both PBO and PAI events triggerd by the runtime environment during the
screen processing . Further you can handle AT SELECTION-SCREEN events from the
selection screens and AT LINE-SELECTION and AT USER-COMMAND events from the
ABAP Lists.

One of the main problem with procedural ABAP events is their implicitness. With only two
exceptions (PUT which trigger the GET event from logical database programs and SET
USER- COMMAND which triggers the AT USER-COMMAND event) procedural ABAP
events can not be triggered from an ABAP program .

you have to know program particular process (the reporting process, the dialog
process , the selection screen process , the list process , e.t.c) in the ABAP runtime
environment in order to understand your program flow so that you can implement each
handler in an appropriate way.

Further more there is one to one relationship b/w handler and the event you can not
implement different handler for one event and you can not deactivate a handler once it is
implemented.

Means you can not define and trigger its own event meaning that procedural ABAP
​ hich means
supports only strong coupling b/w callers(object) and procedure(method). W
that moment you call a procedure , it must exist or(otherwise) a runtime error
occurs.

Page 49 of 227 ​Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Object Oriented Approach to
Events.

You might also like