Beruflich Dokumente
Kultur Dokumente
Page 1 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 1/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Sonu Kumar
Verma
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.
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 2/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
sections.
1. Definition 2.
Implementation
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 3/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
ENDCLA
SS.
*SUB CLASS
IMPLEMENTATION. CLASS
SECOND IMPLEMENTATION.
* REDEFINE SUBCLASS
METHOD.
METHOD
SHOWME.
WRITE / 'WITH IN THE SUBCLASS'.
ENDMETHOD. ENDCLASS. *CREATING
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 4/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
reference of the object. DATA
FIRSTOBJ TYPE REF TO FIRST.
DATA SECONDOBJ TYPE REF TO
SECOND.
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 5/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
METHOD <method
name> ......... END
METHOD.
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 =>
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 6/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
*DEFINING CLASS
COMP_EMP CLASS
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 .
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 7/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
*------------------------------------------------------------------
----* * 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 8/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
CONSTRUCTOR' . ENDMETHOD.
"CLASS_CONSTRUCTOR
START-OF-SELEC
TION.
DATA OBJ TYPE REF TO
COMP_EMP. * CREATE OBJECT
OBJ EXPORTING A = 13. CREATE
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. WRITE / COMP_EMP=>CNAME. "ACCESSING
CLASS DATA USING CLASS
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 9/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 10/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Page 7 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 11/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 12/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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:
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 13/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 14/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 15/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
THE METHOD.
CALL METHOD OBJ1->HELLO. * CALLING
THE SUPER CLASS METHOD USING SUBCLASS
OBJECT.
CALL METHOD
OBJ1->ADD.
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. ":
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 16/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 17/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 18/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
REPORT
ZS_CLASS_SUPER_ME.
*------------------------------------------------------------------
----* * CLASS FIRST DEFINITION
*------------------------------------------------------------------
----* *
*------------------------------------------------------------------
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 19/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Page 13 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
ENDCLASS. "FIRST IMPLEMENTATION
*------------------------------------------------------------------
----* * CLASS SECOND IMPLEMENTATION
*------------------------------------------------------------------
----* *
*------------------------------------------------------------------
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 20/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 21/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
CLASS FIRST1
DEFINITION. *
DEFINING PUBLIC
SECTION
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 22/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
PUBLIC
SECTION.
DATA NUM TYPE I
VALUE 22. METHODS
TESTMETHOD.
* 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 23/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 24/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Page 16 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Abstract class Objects cannot be created from an abstract class. Only the
subclasses of such class can be instantiated.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 25/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
*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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 26/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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. endclass.
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.
data abs_obj type ref to abs_class. "reference of abstract class data conc_obj type ref to
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 27/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
Page 18 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
public
section.
methods
show.
*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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 28/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
s.
class inherit_final
implementation.
endclas
s.
REPORT
ZS_CLASS_FINAL_METHOD.
class first
definition.
public section. methods showme final.
"defining the final method. endclass.
class first
implementation.
method showme. "implementing the final
method.
write 'hello this is
method'. endmethod.
endclass.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 29/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
section.
methods showme redefinition. "redefining the final method will give you compilation
error. endclass.
class second
implementation.
method
showme.
write 'hello
method'.
Page 19 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
endm
ethod.
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.
WORKING WITH
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 30/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
CONSTRUCTOR
Each class has only one instance constructor, which cannot be overloaded. But,
using optional and default parameters allows seeming overloading.
Description 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 31/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Like
this.
REPORT
ZS_CLASS_CONSTRUCTOR1.
CLASS FIRST
DEFINITION.
PUBLIC
SECTION.
* 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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 32/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
METHOD
CLASS_CONSTRUCTOR.
WRITE / 'CLASS
CONSTRUCTOR'.
ENDMETHOD. ENDCLASS.
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
know during its creation that it is a Miller and not a Meyer?
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 33/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
REPORT
ZS_CLASS_CONSTRUCTOR1.
CLASS FIRST
DEFINITION.
PUBLIC
SECTION.
* DEFINING INSTANCE
CONSTRUCTOR.
METHODS
CONSTRUCTOR.
ENDCLASS.
* CLASS
IMPLEMENTATION.
CLASS FIRST
IMPLEMENTATION.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 34/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
*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.
CREATE OBJECT
OBJ.
REPORT
ZS_CLASS_CONSTRUCTOR.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 35/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
END
METHOD.
ENDCLASS.
START-OF-SELEC
TION.
DATA OBJ TYPE REF TO FIRST . "OBJECT REFERENCE TYPE WRITE / '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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 36/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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. CLASS 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'.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 37/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
START-OF-SELEC
TION.
DATA OBJ TYPE REF TO FIRST . "OBJECT REFERENCE TYPE WRITE / '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 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.
* CLASS first DEFINITION CLASS
FIRST DEFINITION.
PUBLIC
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 38/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
"class_constructor ENDCLASS. "first
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 39/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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).
• Two independent 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.
•Asuperclass 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).
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 40/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 41/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 42/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
✓ 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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 43/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
*DEFINING THE INTERFACE AND ITS METHOD OR ATTRIBUTES INTERFACE 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 44/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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 :
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 45/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Page 29 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
Elements of ABAP
Objects :
• Classes
• Interfaces
• Objects
• Object
References
•
Inheritance
Classes :
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 46/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
ENDCLA
SS.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 47/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 48/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
Page 31 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
IF cl_class=>instance_count
< 50. CREATE OBJECT
ENDIF.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 49/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
START-OF-SELEC
TION.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 50/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 51/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
REPORT
ZS_CLASS.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 52/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
START-OF-SELEC
TION.
CALL METHOD
FIRSTOBJ->SHOWME. *Creating
object of second class CREATE
OBJECT SECONDOBJ. *calling
method of subclass and superclass.
CALL METHOD
SECONDOBJ->SHOWME. CALL
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 53/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
METHOD SECONDOBJ->SAWME.
*CALLING CLASS
METHOD.
Page 34 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
CALL METHOD
FIRSTOBJ->CLASSMETHOD_SHOWME.
CONSTRUCTOR IN ABAP
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 54/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
CLASS
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 55/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
CLASS.
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 class 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 56/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
method :
t1.
write:/5 'I am T1, going to raise event
E1'. raise event e1. endmethod. ":
endclass. "c1 IMPLEMENTATION
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 57/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
Page 37 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 58/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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 METHOD : M1.
Page 38 of 227 Prepared By: Sonu Kumar Verma (SAP ABAP Technical Consultant)
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 59/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 60/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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 METHOD : M1.
write:/5 ' I am the event handler
method M1 in c1'. ENDMETHOD. ENDCLASS.
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 61/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 62/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
REPORT
ZS_CLASS_EVENT.
*-----------------------------------------------------------------
----* * INTERFACE lif_employee
*-----------------------------------------------------------------
----* INTERFACE lif_employee.
METHOD
S:
add_employe
e
IMPORTING im_no
TYPE i
im_name TYPE string im_wage TYPE i. ENDINTERFACE.
******************************************************* * Super
class LCL_CompanyEmployees
******************************************************* CLASS
lcl_company_employees DEFINITION.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 63/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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 =
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 64/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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:
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 65/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 66/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 67/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 68/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
REPORT
ZS_CLASS_EVENT1.
*CLASS DEFININTION.
CLASS cl_class
DEFINITION.
PUBLIC
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 69/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
SECTION.
EVENTS: object_created EXPORTING value(ex_reference) TYPE REF
TO cl_class. METHODS CONSTRUCTOR.
*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.
EN
DMETHOD.
ENDCLASS.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 70/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
START-OF-SELEC
TION. *CREATING
OBJECT. CREATE
OBJECT OBJ.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 71/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
*-----------------------------------------------------------------
----* 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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 72/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 73/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
* 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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 74/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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 .
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 75/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
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.
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
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 76/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
supports only strong coupling b/w callers(object) and procedure(method). Which means
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.
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 77/78
4/14/2019 Comprehensive-Abap-Oops-Tutorial-Document - Google Docs
https://docs.google.com/document/d/1dMEGJAifxRDMVH1cRLaMHqBaynjJ4Xsg6nNsXd8k0HI/edit 78/78