OOPS in SAP ABAP
OOPS in SAP ABAP
OOPS in SAP ABAP
1
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 2
Unstructured Programming.
Procedural Programming.
Object Oriented Programming.
Slide 3
report ysubdel.
DATA : sal type p decimals 2,
Characteristics
itax type p decimals 2,
net_sal type p decimals 2 .
Consists of only one main program.
sal = 12000.
IF sal lt 5000 . The program stands for a sequence of
itax = 0.
ELSE. commands which modify data that is
itax = sal * '0.01'. global throughout the whole program.
ENDIF.
net_sal = sal - itax.
write:/5 sal , itax ,
net_sal. Disadvantages
sal = 3500.
IF sal lt 5000 . Difficult to manage once the program
itax = 0.
ELSE. becomes large.
itax = sal * '0.01'.
ENDIF. Same sequence of statements are
net_sal = sal - itax. repeated at multiple places, if they are
write:/5 sal , itax ,
net_sal. needed at multiple locations.
Slide 4
report ysubdel.
DATA : sal type p decimals 2 ,
itax type p decimals 2 ,
net_sal type p decimals 2. Programmer combines related sequences
sal = 12000. of statements into one single place, called
PERFORM sub_calc_tax USING procedure.
sal itax net_sal.
sal = 3500.
A procedure call is used to invoke the
PERFORM sub_calc_tax USING procedure.
sal itax net_sal. After the sequence is processed, flow of
control proceeds right after the position
FORM sub_calc_tax USING
P_SAL P_ITAX P_NET_SAL. where the call was made.
IF p_sal lt 5000 .
p_itax = 0.
ELSE.
p_itax = sal * '0.01'.
ENDIF.
p_net_sal = p_sal - p_itax.
ENDFORM.
Slide 5
Classes and objects are
used to model real world
entity.
Methods inside the classes
perform the functions.
Data used by the classes
are protected between them.
Slide 6
Features Procedure Oriented Object Oriented approach
approach
Emphasis Emphasis on tasks Emphasis on things that does those
tasks
Modularization Programs are divided into Programs are organized into classes
smaller programs known as and objects and the functionalities
functions are embedded into methods of a
class.
Data security Most of the functions share Data can be hidden and cannot be
global data accessed by external sources.
Extensibility Relatively more time consuming New data and functions can be easily
to modify for extending existing added whenever necessary
functionality.
Slide 7
Object Oriented Approach - key features
Slide 8
Requirement for learning OOPS in ABAP
Slide 9
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 10
Class is the template of an object. It is an abstract concept, which is realized by
creating an object from it.
What is your idea about a Maruti-800 car? You will say that it is a four-wheel, five
seater vehicle with a 800 cc engine.
The idea you have in your mind is the class for Maruti-800.Let us call it as class
Maruti-800.
Now, you see a real Maruti-800 car in front of you. So, this is the object of class
Maruti-800. In other words, the real car is realization/ instantiation of the class
Maruti-800.
Slide 11
Global Class Local Class
Slide 12
REPORT YSUBOOPS17 .
CLASS c1 DEFINITION. Defined in the global area of a
local program :-
PUBLIC SECTION.
data : w_num type i value 5. CLASS <class name> DEFINITION.
Slide 13
REPORT YSUBOOPS17 .
Local class in a program is implemented as
CLASS c1 DEFINITION.
follows:-
PUBLIC SECTION.
CLASS <class name> IMPLEMENTATION.
data : w_num type i value 5.
…..
methods : m1.
ENDCLASS.
ENDCLASS.
CLASS c1 IMPLEMENTATION. Methods used by the class are described here.
METHOD M1. A class can be implemented
WRITE:/5 'I am M1 in C1'.
At the end of the program( like subroutines).
ENDMETHOD.
After the class definition.
ENDCLASS.
START-OF-SELECTION. If the latter is adopted, one must then assign
DATA : oref1 TYPE REF TO c1 . subsequent non-declarative statements explicitly
to a processing block, such as START-OF-
CREATE OBJECT : oref1.
SELECTION, so that they can be accessed.
write:/5 oref1->w_num.
CALL METHOD : oref1->m1 .
Slide 14
Class Subclass External
itself of the class user
Slide 16
You are an employee (object of class:- employee). You have put your personal pens
on the desk . These pens can be accesses by you(object of main class) , our son and
daughters( sub-classes) as well as by any other people(users outside the class). So,
your pens are in your public section.
You have a purse(attribute) in your pocket. Any other person (user outside the
class) cannot use your purse(attribute) directly. But, your children(sub-classes of
the class on which you are an object) can have full access to it.So, money in the
purse is in your protected section.
You have a special skill which you have developed by putting constant effort for a
long time. You can only access it. Neither your children(sub-classes of your class),
nor any user have any access to it directly. So, you can consider that skill to be in
your private section.
Slide 17
A Class basically contains the following:-
Attributes:- Any data,constants,types declared within a class form the
attribute of the class.
Events:- A mechanism set within a class which can help a class to trigger
methods of other class.
Slide 18
Instance and Static Components
REPORT YSUBOOPS17 .
CLASS c1 DEFINITION.
PUBLIC SECTION. Instance components exist
data : i_num type i value separately in each instance
5. (object) of the class and are
class-data : referred using instance
s_num type i value 6 .
ENDCLASS.
component selector using ‘’.
Static components only exist
CLASS c1 IMPLEMENTATION. once per class and are valid
ENDCLASS. for all instances of the class.
They are declared with the
START-OF-SELECTION.
DATA : oref1 TYPE REF TO c1 . CLASS- keywords
CREATE OBJECT : oref1. Static components can be
write:/5 oref1->i_num. used without even creating an
write:/5 c1=>s_num .
instance of the class and are
write:/5 oref1->s_num.
referred to using static
component selector ‘ =>’ .
Slide 19
Addition 1 : CLASS class DEFINITION DEFERRED.
Used to refer to a class at some point in a code and the class is not
defined before the line.
Slide 20
Addition 2 : CLASS class DEFINITION LOAD
The compiler normally loads the description of a global class from the class library
the first time you use the class in your program . However, if the first access to a
global class in a program is to its static components or in the definition of an event
handler method , you must load it explicitly using the statement CLASS class
DEFINITION LOAD. This variant has no corresponding ENDCLASS statement.
Slide 21
Creating an Object
report ysubdel. To create an object , the following steps
need to be followed:-
CLASS c1 DEFINITION.
PUBLIC SECTION. Step 1: Create a reference variable with
reference to the class.
DATA : prog(4) type c value
'ABAP'.
DATA : <object name> TYPE REF TO <class
ENDCLASS. name>.
Slide 22
Syntax : CLASS <classname> DEFINITION … [CREATE PUBLIC|PROTECTED|PRIVATE]
Addition of CREATE PRIVATE means the class can only instantiate itself or
the friends of the class can instantiate it.
Slide 23
Attributes are internal data fields within a class that can have any ABAP
data type.
Instance Attributes
Exist separately in each instance (object) of the class .
Declared using the DATA statement.
Static Attributes
Available once for each class .
Declared using the CLASS-DATA statement and are retained throughout
the entire runtime. All the objects within a class can access its static
attributes.
Slide 24
Types and Constants in Classes
Types
TYPES statement is used to define any number of your own ABAP data types
within a class.
Constants
Constants are special static attributes, whose values are specified when they
are declared and which cannot be changed later. The CONSTANTS
statement is used to declare constants. Constants are not instance-specific -
they are available once and once only for all the objects in the class.
Read-only
Read-only data can be changed only by the methods of the class or its
subclasses . But, the external users cannot change the data.
Slide 25
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 26
I E Methods are the internal procedures of a
class .
C Method C Have a parameter interface, through
which the system passes values to them
when they are called, and through which
they can return values to the caller.
X
Can access all the attributes of their class.
Slide 28
All parameters must be declared using the addition TYPE or the addition LIKE.
With LIKE you can only refer to data types of class attributes known at this point in ABAP
Objects and not to data types from the ABAP Dictionary.
The following entries in the table shown below are allowed after TYPE as parameter types.
Syntax Relevance
… TYPE t stands for all data types from the ABAP Dictionary
and all ABAP data types known at this point .
… TYPE ANY stands explicitly for a parameter declaration without
type reference. This means that the parameter
inherits all technical attributes of the parameter that
has been passed when the method is called.
… TYPE REF TO cif stands for a class or interface reference.
… TYPE LINE OF itab stands for the line type of an internal table itab.
TYPE [ANY|INDEX|STANDARD|SORTED| stands for the internal table type of the table type
HASHED] TABLE
specified
Slide 29
All methods declared in the definition part of a
class should be implemented in the implementation
section of the class within the following block:-
METHOD <meth>.
...
ENDMETHOD.
One should not specify any interface parameters at
the time of implementation, since these are defined
in the method declaration.
The interface parameters of a method behave like
local variables within the method implementation. You
can define additional local variables within a method
using the DATA statement.
Slide 30
oLike any other class components , methods
can be static or instance.
oTo declare static methods, use the
following statement:
CLASS-METHODS <meth>...
CALL METHOD <meth>... Methods of the same class(in the implementation part) can
be called directly using their name <meth>.
CALL METHOD <ref>-><meth>... Visible instance methods can be called from outside the
class using this syntax where <ref> is a reference variable
whose value points to an instance of the class.
CALL METHOD <class>=><meth>... . Visible instance methods can be called from outside the
class using this syntax where <class> is the name of the
relevant class.
CALL METHOD <ref>-><meth>... Visible instance methods can be called from outside the
class using this syntax where <ref> is a reference variable
whose value points to an instance of the class.
CALL METHOD <ref>-><meth>( f1 Used to call the method that consists only of IMPORTING
= a1 f2 = a2……. ) parameters a1,a2 etc…
Slide 33
Syntax Relevance
MOVE <ref>->meth( ... ) TO f1. The method has one returning parameter whose values is
transferred to the variable f1.
Slide 34
Dynamic Method Calls
Instance, self-referenced, and static
methods can all be called dynamically; the
class name for static methods can also be
determined dynamically:
oref->(method)
me->(method)
class=>(method)
(class)=>method
(class)=>(method)
Slide 35
Declare the name of the exceptions
while defining the method in the
class definition.
Raise the exceptions within the
method using any of the following
syntaxes:-
RAISE <exception name>
MESSAGE …RAISING <exception>
Handle the exceptions while calling
method with different values of
sy-subrc.
Slide 36
Instead of mentioning all the import,export, changing and receiving parameters
for a dynamically calling method separately while calling it, one can use parameter
table to specify information about the parameters and call the method.
The parameter table itab must be a hashed table of the table type ABAP_PARMBIND_TAB
or of the line type ABAP_PARMBIND. These types are defined in the ABAP type group in
the ABAP dictionary. The table has three columns:
NAME for the name of the formal parameter
KIND for the type of parameter passing
VALUE of the type REF TO DATA for the value of the actual parameter .
The type of parameter passing is defined for each formal parameter in the declaration of the
called method. The content of the column KIND can, therefore, be initial. To check the type of
parameter passing at runtime, one of the following constants from the global class
CL_ABAP_OBJECTDESCR to the column KIND can be assigned:-
CL_ABAP_OBJECTDESCR=><xyz>
where <xyz> is :- EXPORTING/IMPORTING/CHANGING/RECEIVING for export,import,
changing and receiving parameters respectively.
Slide 37
If the specified and actual parameter types do not match each other, the system
generates the exception CX_SY_DYN_CALL_ILLEGAL_TYPE, which can be
handled.
For the value of the actual parameter, the reference VALUE of the table line must
point to a data object that contains the required value. For this purpose, you can
use the command GET REFERENCEOF f INTO g.
Slide 38
Instead of dealing with each and every exception and assigning it to different
sy-subrc values, one can use exception table to handle the exceptions when a
method is called dynamically.
The exception table itab must be a hashed table of the table type ABAP_EXCPBIND_TAB or
of the line type ABAP_EXCPBIND. These types are defined in the ABAP type group in the
ABAP dictionary. The table has two columns:
The column NAME is the unique table key. For each exception, exactly one line of the internal
table can be filled. Here the numeric value is assigned to the component VALUE, which should
be in SY-SUBRC after the exception has been triggered
Slide 39
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 40
Constructors are special methods that are triggered when an object is
instantiated from a class. They are necessary when you want to set the
initial state of an object dynamically.
Slide 41
Executed once for each instance.
Slide 42
REPORT YSUBOOPS2. Static methods, declared as CLASS-METHODS :
CLASS c1 DEFINITION . CLASS_CONSTRUCTOR in the public section of the class
PUBLIC SECTION. definition and are also implemented in the
CLASS-DATA : NUM TYPE I VALUE 5. implementation part.
CLASS-METHODS:CLASS_CONSTRUCTOR.
ENDCLASS. Has no interface parameters and cannot trigger
exceptions.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR. Executed once in each program. It is called
WRITE:/5 'I am class automatically for the class before it is accessed for
constructor'. the first time - that is, before one of the following
ENDMETHOD.
actions:
ENDCLASS.
CREATE OBJECT obj from the class.
START-OF-SELECTION.
WRITE:/5 C1=>NUM. Call a static method :
[CALL METHOD] class=>meth.
Registering a static event handler method using
SET HANDLER class=>meth for obj.
Registering an event handler method for a
static event of the class class.
Addressing a static attribute with class=>a.
Slide 43
Exception:-
The static constructor is always called immediately before the action is executed,
with one exception: If the first access to the class is to address a static attribute,
the static constructor is executed at the beginning of the processing block (dialog
module, event block, procedure) in which the access occurs.
Caution:-
The static constructor must not access its own class. Otherwise a runtime error
will occur.
The point at which the static constructor is executed has not yet been finalized,
and it may yet be changed. SAP only guarantees that it will be executed before the
class is accessed for the first time. It is recommended not to write programs that
require the static constructor to be executed at the beginning of a processing block.
Slide 44
Self-Reference
Internally, each method has an implicit self-
reference variable, the reserved word me
A method can access the components of its
class simply by their name; however,
It may use me simply for clarity
If a method declares a local variable with
the same name as one of the class
components, to avoid ambiguity it must
use me to address the identically-named
component
A method must use me to export a
reference to itself (that is, its object)
Slide 45
Pointer Tables
DATA: oref1 TYPE REF TO c1, We earlier saw that it is possible, in the
oref2 TYPE REF TO c1, same program, to instantiate many
oref3 TYPE REF TO c1. objects of the same class. However, in
many situations internal tables are a
more elegant way of creating a list of the
DATA: oref TYPE REF TO c1,
same kind of data object
oref_tab TYPE TABLE OF REF TO Remember, reference variables are
c1
START-OF-SELECTION.
handled like any other data object with an
elementary data type!
…
DO 3 TIMES.
CREATE OBJECT oref.
APPEND oref to oref_tab.
ENDDO.
---
LOOP AT oref_tab INTO oref.
oref->meth.
ENDDO.
Slide 46
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 47
A single class can give birth to its
subclasses by inheritance.
One parent class can have multiple
subclasses, but one subclass can have only
one parent class.
Subclasses have full access to all the
public and protected components of parent
class. They can modify to re-implement
those components of their own.Also,
subclasses can create new components in
them.
Parent Class Sub Class
Private components of parent classes are
not known to subclasses. There can be
components with same name in private
section in both the parent and subclasses ,
each one working and being identified
separately in their respective classes.
Slide 48
Each class component has a visibility. In ABAP Objects the whole class definition is
separated into three visibility sections: PUBLIC, PROTECTED, and PRIVATE. One can
never change component visibility via inheritance.
PUBLIC: This section is made up of the subclass' own public components together with all
public components of all superclasses. There is unrestricted access to the public section of
the subclass.
PROTECTED: This section is made up of the subclass' own protected components together
with all protected components of all superclasses .
PRIVATE: This section is made up of the subclass' own private components accessible
only in that subclass. Each subclass works with its own private components and can't even
use the private components of its superclasses. But as long as a method inherited from a
superclass is not redefined, it still uses the private attributes of the superclass, not those of
the subclass, even if the subclass has private attributes of the same name.
Slide 49
Subclasses can be created from its
superclass using the syntax:-
CLASS <subclass> DEFINITION
INHERITING FROM <superclass>.
Subclass inherits all the public and
private components of the superclass.
Superclass should not be declared as a
FINAL class.
Slide 50
To redefine a public/protected method
of a superclass in one of its subclasses, use
the syntax in the subclass definition:-
METHOD <method name> REDEFINITION
Slide 51
One cannot create an object from an abstract
class. Only subclasses can be derived from
them.
CLASS <classname> DEFINITION ABSTRACT.
Slide 52
Final classes cannot have subclasses.
Only the class can be instantiated.
CLASS <classname> DEFINITION FINAL.
Slide 53
CLASS c1 DEFINITION.
PUBLIC SECTION . Static attributes only exist
CLASS-DATA : num TYPE I VALUE 5 . once in each inheritance tree.
ENDCLASS. One can change them from
outside the class using the class
CLASS c1 IMPLEMENTATION. component selector with any
ENDCLASS. class name, or within any class in
which they are shared.
CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS. They are visible in all classes in
the inheritance tree.
CLASS c2 IMPLEMENTATION.
ENDCLASS.
START-OF-SELECTION.
c2=>num = 7.
write:/5 c1=>num .
Slide 54
Superclasses and/or subclasses can have explicit constructors of their
own. Constructor of a subclass sometimes have to call the constructor of
the superclass using : CALL METHOD : SUPER->CONSTRUCTOR depending on
the following:-
Slide 55
Every class has a static constructor called CLASS_CONSTRUCTOR.
The first when a subclass in a program is accessed, its static constructor is executed. But,
before it can be executed, the static constructors of all of its superclasses must already
have been executed. A static constructor may only be called once per program. Therefore,
when one first address a subclass, the system looks for the next-highest superclass whose
static constructor has not yet been executed. It executes the static constructor of that class,
followed by those of all classes between that class and the subclass that is addressed.
Slide 56
When a subclass is instantiated , all the superclasses are instantiated at
the same time, and the initialization of superclass attributes is ensured by
the call of the superclass constructors.
For each individual class, the CREATE PUBLIC|PROTECTED|PRIVATE
additions to the CLASS statement control who can create an instance of
the class or, in other words, can call its instance constructor.
There are several cases which needs to be analyzed to understand:-
Slide 57
1. Superclass with addition CREATE PROTECTED
Subclasses can have every CREATE addition. Without addition they inherit the attribute
CREATE PROTECTED. The superclass allows its subclasses unlimited instantiation and
therefore also the publishing of its protected instance constructor.
Slide 58
Static Type and Dynamic Type of a
Variable
The static type is the type (class or interface) with which the reference variable has been
typed.
The dynamic type is the class of the object to which the reference in a reference variable
points .
If the static and dynamic types of a reference variable are different, the static type is
always more general than the dynamic type.
If you make assignments between reference variables, the static type of the target variable
must always be more generic than the dynamic type of the source variable.
Slide 59
class c1 definition.
. . . . . . .
endclass.
With inheritance, a reference variable
class c1 implementation. defined with respect to a class may not only
. . . . . . point to instances of that but also to
endclass. instances of subclasses of the same. One
can even create subclass objects using a
class c2 definition inheriting
from c1.
reference variable typed with respect to a
. . . . . .
super class.
endclass. Polymorphism through inheritance can be
class c2 implementation. achieved by playing with static and dynamic
. . . . . . . type of a reference variable.
endclass.
start-of-selection. Instances of a subclass may be used
data : oref1 type ref to c1, through the super class's interface. When
oref11 type ref to c1, this is done, a client can't access all
oref2 type ref to c2. components defined in the subclass, only
create object oref1 type c2 . those inherited from the respective super
create object oref2. class.
oref11 = oref2.
write:/5 oref1->num ,
oref11->num . Slide 60
OBJECT External Interface
C1
PUB: a1 m1 DATA: oref1 TYPE REF TO C1,
oref3 TYPE REF TO C3.
PROT: pro_a1 pro_m1
...
PRIV: f1 pri_m1 CREATE OBJECT oref3.
oref1 = oref3.
C2 INHERITING FROM C1 a1 a1
m1 a2
PUB: a1 a2 m1 m2 a3
PROT: pro_a1 pro_a2 pro_m1 pro_m2 m1
m2
PRIV: f1 pri_m1 m3
Slide 61
OBJECT External Interface
C1
PUB: a1 m1 DATA: oref1 TYPE REF TO C1.
...
PROT: pro_a1 pro_m1
CREATE OBJECT oref1 TYPE C3.
PRIV: f1 pri_m1
a1
m1
C2 INHERITING FROM C1
PUB: a1 a2 m1 m2
PRIV: f1 pri_m1
Slide 62
Narrowing Cast
DATA: o_ref1 TYPE REF TO “Narrowing” cast means that the assignment changes from
object, a more specialized view (with visibility to more
o_ref2 TYPE REF TO components) to a more generalized view (with visibility
class.
to fewer components).
...
When the static type of the target variable is more general than the
o_ref1 = o_ref2.
static type of the source variable, since the dynamic type of the
source variable at runtime can only be more specialized than its
static type, this can be checked during the syntax check. This is
known as a narrowing cast. The most extreme case of narrowing
cast is shown in the first code fragment, where the static type of
the target variable is object
In some cases, you may wish to make an assignment in which the
static type of the target variable is more specialized than the static
type of the source variable. This is known as a widening cast. The
result of the assignment must still adhere to the rule that the
static type of the target variable must be the same or more general
than the dynamic type of the source variable. However, this can
only be checked during runtime. To avoid the syntax error that
will occur in the second code fragment, use the special casting
operator “?=“ and catch the potential runtime error
move_cast_error
Slide 63
Inheritance – Object References (5)
DATA: o_ref1 TYPE REF TO object, In assignments of reference variables, you must adhere to the rule
o_ref2 TYPE REF TO class. that the static type of a reference variable is always more general
... than the dynamic type. Sometimes this can be checked during the
o_ref1 = o_ref2. syntax check, sometimes only during runtime
When the static type of the target variable is more general than
DATA: o_ref1 TYPE REF TO object, the static type of the source variable, since the dynamic type of the
o_ref2 TYPE REF TO class. source variable at runtime can only be more specialized than its
... static type, this can be checked during the syntax check. This is
o_ref2 = o_ref1. syntax error known as a narrowing cast. The most extreme case of narrowing
cast is shown in the first code fragment, where the static type of
DATA: o_ref1 TYPE REF TO object, the target variable is object
o_ref2 TYPE REF TO class. In some cases, you may wish to make an assignment in which the
... static type of the target variable is more specialized than the static
o_ref2 ?= o_ref1. type of the source variable. This is known as a widening cast. The
... result of the assignment must still adhere to the rule that the
CATH SYSTEM-EXCEPTIONS static type of the target variable must be the same or more general
move_cast_error = 4. than the dynamic type of the source variable. However, this can
o_ref2 ?= o_ref1. only be checked during runtime. To avoid the syntax error that
ENDCATCH. will occur in the second code fragment, use the special casting
operator “?=“ and catch the potential runtime error
move_cast_error
Slide 64
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 65
Independent structures containing definition of its own components.
Incorporated in the public section of a class definition and the methods
of the interface are implemented in the implementation section of that
class.
Slide 66
report ysubdel .
interface i1. Can be declared globally or locally within a
data : num type i . program.
methods : meth1.
endinterface. Locally declared in the global portion of a program
using:-
class c1 definition.
public section. INTERFACE <intf>.
methods : meth1.
interfaces : i1. ...
endclass.
ENDINTERFACE.
class c1 implementation.
method : meth1.
The definition contains the declaration for all
write:/5 'I am meth1 in c1'.
endmethod. components (attributes, methods, events) of the
interface.
method i1~meth1.
write:/5 'I am meth1 from i1'. Interfaces are included in the public section of a
endmethod.
endclass.
class.
start-of-selection.
Interfaces do not have an implementation part,
data : oref type ref to c1. create object oref. since their methods are implemented in the class that
write:/5 oref->i1~num. implements the interface.
call method oref->meth1.
call method oref->i1~meth1.
Slide 67
(1) INTERFACE <intf> DEFERRED.
Usually, an interface is first defined and then used.
But, if there is a need to use an interface in a context before it is defined - the
interface must be made known using the INTERFACE intf DEFERRED variant.
There is no ENDINTERFACE statement for this variant.
(2) INTERFACE <intf> LOAD.
When an interface is used for the first time in a program - particularly if the
interface is used for the first time to type reference variables , the compiler
automatically loads the description of a global interface from the class library .
However, if the first use of a global interface is accessing one of its static
component, or when an event handler method is defined with respect to an event in
the global interface, the interface description must be loaded explicitly using the
INTERFACE intf LOAD (for technical reasons). In such cases, there is no
associated ENDINTERFACE statement.
Slide 68
Addition Effect
... ABSTRACT METHODS meth_1 Assigns the property ABSTRACT to the specified
... meth_n instance methods of the interface.
... FINAL METHODS meth_1 ... Assigns the property FINAL to the specified
meth_n instance methods of the interface.
... ALL METHODS ABSTRACT Assigns the property ABSTRACT to all instance
methods of the interface.
... ALL METHODS FINAL Assigns the property FINAL to all instance
methods of the interface.
... DATA VALUES attr_1 = val_1 Assigns initial values to the attributes specified
... attr_n = val_n (DATA, CLASS-DATA). The values of constants
cannot be changed. The specifications val_n have
the same meaning as the VALUE specification in
the DATA statement.
Slide 69
The interface components are declared in the INTERFACE…ENDINTERFACE block.
One cannot use the VALUE addition with the DATA statement in interface. The
INTERFACES statement is used to nest and compound interfaces, not to implement
them.
Slide 70
Interfaces do not have instances. Rather, they are implemented by classes. For that,
interfaces are included in the public section of a class definition using the following
syntax:-
INTERFACES <intf>.
The class must implement the methods of all interfaces implemented in it.
METHOD <intf~imeth>.
...
ENDMETHOD.
Interfaces can be implemented by different classes and the methods of the interface can
be implemented differently in each class.
Slide 71
Like classes , reference variables can be created with respect to an
interface This kind of reference variable can contain references to
objects of classes that implement the corresponding interface.
To define an interface reference, the addition TYPE REF TO <intf> in the
TYPES or DATA statement is used. <intf> must be an interface that has
been declared to the program before the actual reference declaration
occurs.
Slide 72
If a class <class> implements an interface <intf>, the interface reference
variable <iref> can be assigned the class reference variable <cref> to make
the interface reference in <iref> point to the same object as the class
reference in <cref>. The syntax for that is :-
<iref> = <cref>
Slide 73
One can assign interface references to other interface/class reference variables, using
MOVE statement or the assignment operator (=) . However, there are certain restrictions
while doing so. When one does this in a program, system must be able to recognize in
the syntax check whether an assignment is possible.
Suppose we have a class reference <cref> and interface references <iref>, <iref1>, and
<iref2>. The following assignments with interface references can be checked statically:
Slide 74
Besides static check, dynamic check to ensure consistency of assignment of
interface references.
For that, the casting operator (?= or MOVE …? TO) can be used. The
system then performs at runtime .If the assignment is possible, the
system makes it, otherwise, the catchable runtime error
MOVE_CAST_ERROR occurs.
Slide 75
INTERFACE i1. CLASS c1 DEFINITION.
You can compose a new interface from several existing
METHODS meth. PUBLIC SECTION.
ENDINTERFACE. INTERFACES: i4.
ones. Such an interface is called a compound
ENDCLASS. interface; an interface which is contained in another
INTERFACE i2. interface is called a component interface
METHODS meth. CLASS c1 IMPLEMENTATION.
There is no component hierarchy: All component
INTERFACES i1. METHOD i1~meth.
interfaces are on the same level, so it is not possible to
ENDINTERFACE. …
chain names such as i3~i2~i1
ENDMETHOD.
INTERFACE i3. METHOD i2~meth. – A compound interface contains each component
METHODS meth. … interface only once
INTERFACES: i1, i2. ENDMETHOD.
ENDINTERFACE. METHOD i3~meth. – When a class implements interfaces, each
… component interface is implemented only once
INTERFACE i4. ENDMETHOD.
INTERFACES i3. ENDCLASS.
ENDINTERFACE.
Slide 76
INTERFACE i1. CLASS c1 DEFINITION. The full name of a component which an Interface
METHODS meth. PUBLIC SECTION. adds to a class or another interface is intf~comp.
ENDINTERFACE. INTERFACES: i1. Alias names can be substituted for this name when
ALIASES m1 for
INTERFACE i2.
i1~meth. – defining compound interfaces, or
ENDCLASS.
INTERFACES i1. – declaring Interfaces in a class
ALIASES m1 FOR i1~meth.
ENDINTERFACE.
CLASS c1 IMPLEMENTATION. Since the chain name i3~i2~meth is not allowed,
METHOD i1~meth.
the alias name m1 in i2 enables i3’s own
…
INTERFACE i3. ALIASES statement to address component meth in
ENDMETHOD.
INTERFACES i1, i2. c1
ENDCLASS.
ALIASES m1 FOR i2~m1.
ENDINTERFACE.
The class implementation must still refer to the full
DATA: oref TYPE REF TO c1. name, but the user can use the alias. This permits
CREATE OBJECT oref.
CALL METHOD oref->m1.
– Code that is easier to write and read
– A class publishing its interface components as
class-specific components (m1 rather than
i1`meth)
– Replacing class-specific components (e.g.,
meth) with interfaces (e.g., ALIASES meth for
m1~intf_method), without affecting existing
users of the component
Slide 77
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 78
In classes, there is normally a strict division between outside (PUBLIC) and
inside (PROTECTED or PRIVATE). A user can only access the public
components of a class. A subclass have access to public and protected sections
of its superclass.
In rare cases, classes have to work together so closely that they need access to
one anothers' protected or private components. In order to prevent these
components being available to all users, there is the concept of friendship
between classes.
Slide 79
A class can declare other classes and interfaces (and hence all classes that implement
this interface) as friends by including FRIENDS additions to the CLASS ... DEFINITION
statement which lists all the classes and interfaces that are granted friendship.
These friends gain access to the protected and private components of the class granting
the friendship and can always create instances of this class, independently from the
CLASS statement's CREATE addition.
In principle, granting of friendship is one-sided: A class granting a friendship is not
automatically a friend of its friends. If the class granting the friendship wants to access the
private components of a friend, then the latter has to explicitly grant friendship to the
former.
However, the granting of friendship is not passed on: A friend of a superclass is not
automatically a friend of its subclasses.
Slide 80
The CLASS ... DEFINITION statement has mainly two different
FRIENDS additions to offer friendship to other classes/interfaces:
Slide 81
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 82
Raising events in Object Oriented Programming is a mechanism by which
methods of one class can call methods of same/other classes.
List of To-do’s
1.Declare an event in a class.
Class 1 Class 2
2. Declare a triggering
method and implement it
Event Event Handler In the same class.
Method
3. Declare an event handler
Method in same/other class.
Slide 83
Events are declared in the declaration part of a class or in an interface.
The syntax is :-
Slide 84
An instance event in a class can be triggered by any method in the class.
Static events can be triggered by any static method. The following
syntax should be used inside the triggering event in the implementation
part of the class to trigger the event residing in the same class:-
For each formal parameter <e i > that is not defined as optional, one must
pass a corresponding actual parameter <f i > in the EXPORTING addition.
Slide 85
Any class can contain event handler methods for events from other
classes. Even, one can also define event handler methods in the same class
as the event itself. To declare an instance event handler method in the
PUBLIC section of a class, the following statement should be followed: -
Slide 87
Events – Runtime Environment
C3
Public, Protected,
CREATE OBJECT oref3
Private
register for e1 Methods: handler_e1
e1 handler_e1
SET HANDLER oref3-
e1 handler_e1 >handler_e1 FOR oref1
C1 C4
Public, Protected, Public, Protected,
Private Private CREATE OBJECT oref4
raises event e1
Methods: trigger_e1 register for e1 Methods: handler_e1
oref1->trigger_e1 handler_e2
Events: e1 SET HANDLER oref4-
>handler_e1 FOR oref1
CREATE OBJECT oref1
Each object that can trigger events has an invisible handler table, where the entries occur as a result
of SET HANDLER statements
The RAISE EVENT statement in the triggering method interrupts the execution of the method until
the runtime finishes cascading through each handler registered in the handler table (an event
handler method can itself raise an event). The initial triggering method then resumes execution
Note: Should the program delete, say, reference variable oref3, the object it references is still
pointed to by oref1’s handler table and therefore will not be deleted until the object pointed to by
oref1 is itself deleted
Slide 88
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.
Each handler method can however specify which event parameters it wants to
handle and which it does not.
Slide 89
Different approaches to Programming
Class and objects
Methods
Constructor
Inheritance
Interface
Friendship between Classes
Events
Class-Based Exceptions
BADIs
Slide 90
An exception is a situation that occurs during the execution of an ABAP
program, which renders a normal program continuation pointless.
Slide 91
Exceptions of various kinds can be broadly classified as :-
Exceptions that can be handled.
Exceptions that cannot be handled.
Exceptions that can be handled indicate error situations in the runtime
environment or in the ABAP program, in the case of which the program
execution can be continued - by handling the exception in the ABAP
program - without the system reaching a critical condition. If such a
situation is not handled a runtime error will occur.
Exceptions that cannot be handled indicate critical error situations in
the runtime environment, which cannot be handled with/by ABAP means
and always cause a runtime error. Database space problem can be an
example of such category.
Slide 92
Areas Brief Overview
Slide 93
In Class-based exceptions handling approach, exceptions are generally
represented by objects of exception classes. There are pre-defined
exception classes for error situations in the runtime environment .
Users can also define own exception classes globally/locally, if required
and can raise them using RAISE EXCEPTION statement.
The runtime environment only causes exceptions that are based on pre-
defined classes, while in ABAP programs one can use raise pre-defined as
well as user-specific exception classes.
Class-based exceptions are handled using the control structure
TRY ... ENDTRY.
Class-based exceptions in procedures can be propagated to the caller in
the definition of the interface using the RAISING addition, if the
exception is not to be handled in the procedure.
Slide 94
SAP provided exception-classes are derived from the specific class CX_ROOT
and have the prefix CX_.
Exception classes are normal classes with one limitation:-
Apart from the constructor, no methods can be defined for them. However,
CX_ROOT has some pre-defined methods available, which can then be inherited
by all exception classes.
Slide 96
Class-Based Exceptions – SAP Exception Classes (2)
CX_STATIC_CHECK:
For exceptions that have to be declared. This type should be chosen if you
want to make sure that this exception is always dealt with and if a local
exception handler has a chance to do something useful in an exception
situation
Corresponding exceptions must either be handled or forwarded explicitly with
the RAISING addition and this is checked at syntax check
CX_DYNAMIC_CHECK:
For exceptions that do not have to be declared
Exceptions must be handled or explicitly forwarded with the RAISING
addition though this is not checked at syntax check. Exceptions of this type
are checked at runtime only
Useful for potential error situations that do not have to be handled, since the
program logic can more or less exclude them. Example: cx_sy_zerodivide
Most of the CX_SY_ exceptions inherit from this class
CX_NO_CHECK:
For exceptions that must not be declared (i.e. resource bottlenecks)
Can be handled but not forwarded with RAISING. Otherwise will be
propagated through call chain automatically
Not checked by syntax check or runtime processing
Slide 97
Class-based exceptions are handled using TRY…CATCH…ENDTRY block.
Slide 98
Nested Try…Catch…Endtry Blocks
TRY.
TRY.
Try block
CATCH cx_class INTO oref
Catch block
CATCH cx_class INTO oref
Try block Catch block
…
….
CLEANUP.
Cleanup block
ENDTRY.
ENDTRY.
Slide 99
Report ysubdel.
Used within a TRY…ENDTRY
data : w_num type i.
BLOCK , after all CATCH statements.
try.
Each TRY block can contain
try .
maximum of one CLEANUP area.
w_num = 5 / 0 .
cleanup.
Used to release the external resources
when exception detected in a TRY
write:/5 ‘In cleanup’.
block is not handled within the block ,
endtry .
but is caught further up in the call
catch cx_sy_zerodivide. hierarchy.
write:/5 ‘Div. By zero!’.
Possible only in cases of nested TRY
endtry. blocks.
In cleanup
Div. by zero!
Slide 100
To create a local exception class in a program and use it, follow the steps
outlined below.
REPORT YSUBCLASS_EXCEPTION_3.
CLASS CX_SOME_EXCEPTION DEFINITION INHERITING FROM
CX_STATIC_CHECK.
public section.
methods : meth1.
ENDCLASS.
Slide 101
Step 2 :- Implement methods of the subclass which will raise exception
Step 3 :- Define another class which will call the exception class.
Slide 102
Step 4 :- Implement the method of the other class which will raise exception
of the locally declared exception class.
Slide 103
Step 5 :- Create an object of the other class and call its method which will
raise the exception
Slide 104
Class-Based Exceptions – Debug
Mode
Exception has
occurred and has
been handled
Slide 105
Class-Based Exceptions – Debug
Mode
Trigger point of
exception
Display Exception
Object
Slide 106
Class-Based Exceptions – Debug
Mode
Slide 107
Class-Based Exceptions – Creating a
Global Exception Class (1)
Note Superclass
and class type
SE24
Slide 108
Class-Based Exceptions – Creating a
Global Exception Class (2)
Go to
Methods
Tab
Double click on
the constructor
method to view
code
Click on
previous
object button
to return to
methods tab
Slide 111
Class-Based Exceptions – Creating a
Global Exception Class (5)
Slide 112
Class-Based Exceptions – Creating a
Global Exception Class (6)
Click on
previous
object button
to return to
methods tab
Slide 114
Class-Based Exceptions – Creating a
Global Exception Class (8)
The texts are stored in the Online Text Repository (OTR). The exception object
contains only a key that identifies the text (with system language)
The default text has the same name as the name of the exception class, in this
case ZCX_SOME_EXCEPTION.
You might wish to create an alternate text for the exception. That text can be
entered on this screen with a new exception ID and can be displayed by passing
this value to the parameter textid of the exception constructor.
Slide 115
Class-Based Exceptions – Creating a
Global Exception Class (9)
Slide 116
Class-Based Exceptions – Creating a
Global Exception Class (10)