You are on page 1of 54

IBM Global Services

ABAP Objects - Advanced

ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Objectives

 The participants will be able to :


 Define Inheritance
 Interpret Interfaces
 Interpret Friends
 Define and trigger Events
 Handle Events
 Register Events
 Describe the runtime environment for Event processing
 Define Global classes and interfaces

2 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

ABAP Objects Advanced : Inheritance Topics

 Concept
 Syntax and Visibility
 Abstract Classes and Methods
 Final Classes and Methods
 Static Components in Inheritance
 Method Redefinition
 Constructor in Inheritance
 Object References in Inheritance
 Polymorphism through Inheritance

3 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Concept

 The concept of inheritance is used to incorporate the properties of an existing


class into a new class. The beauty of this feature is that the methods and the
attribute of the existing class need not to be coded into the new class, as well as
new features can be added into the new class.
 Inheritance can be single inheritance ( subclass is inherited from a single super
class) or multiple inheritance (subclass is inherited from multiple super classes).
But ABAP Objects supports only single inheritance.

Parent Parent Grand Parent

Child Parent

Multiple Inheritance not Child Single Inheritance


supported by ABAP supported by ABAP
Objects Objects
4 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation
IBM Global Services

Inheritance : Concept (Contd.)

Root node OBJECT  The relationship between the


base classes and the derived
Class C1 classes can be represented in an
Public: Inheritance tree. Where base
Generalized classes of each derived class can
Protected:
class be retraced along an unique path
Private:
to a single root node of the tree.
Class C2  In ABAP Objects, root node of the
Public: Inheritance tree is the OBJECT
Specialized Protected: class.
class Private:  In ABAP, if Inheritance is not
specified in the class declaration,
Class C3 the class implicitly inherits from
Public: OBJECT class.
Protected: More specialized class
Private:
5 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation
IBM Global Services

Inheritance : Syntax and Visibility


CLASS super_class DEFINITION.
PUBLIC SECTION.
 The public and protected
Public components
PROTECTED SECTION. components of the super class are
Protected components visible in the subclass.
PRIVATE SECTION.  Private section of the super class is
Private components
not visible in the sub classes.
ENDCLASS.
CLASS sub_class DEFINITION INHERITING FROM  Private components of the sub class
super_class.
can have same name as the private
PUBLIC SECTION.
component of the super class.
Public components
Public components  Public and Protected components of
(super_class)
the sub class can not have the
PROTECTED SECTION.
Protected components
same name as that have been used
Protected components in super class.
(super_class)
PRIVATE SECTION.
Private section
ENDCLASS.

6 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Abstract classes and methods

Abstract method  A class defined as ABSTRACT


CLASS cl_super DEFINITION. cannot be instantiated , that is one
PUBLIC SECTION. cannot use CREATE OBJECT with
reference to the class.
METHODS: demo1 ABSTRACT,
demo2.
 Abstract class can only be accessed
using its static components or its
ENDCLASS.
subclasses.
Redefining the Abstract method of Super
class in the Sub class  Abstract class serves as a template
for subclasses
CLASS cl_sub DEFINITION
INHERITING FROM cl_super.  If a class contains any abstract
PUBLIC SECTION.
method the whole class becomes
abstract.
METHODS demo1 REDEFINITION.
 A method, which is abstract, should
ENDCLASS.
be redefined in derived class.

7 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Final classes and methods


CLASS final_class DEFINITION FINAL.
........  A final class can not be inherited
ENDCLASS. further.
CLASS derived_class DEFINITION
INHERITING FROM final_class .
 All Methods of a final class are
. .. . . . . . . . inherently final and must not be
ENDCLASS. declared as final in the class
definition.
CLASS super_class DEFINITION .  A final method can not be redefined
........ further.
METHODS final_method FINAL.
ENDCLASS.
CLASS sub_class DEFINITION INHERITING
FROM super_class .
. .. . . . . . . .
METHODS final_method redefinition.
ENDCLASS.

8 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Static components


CLASS base_class DEFINITION.
PUBLIC SECTION. Static component  The public and protected static
CLASS-DATA : name TYPE STRING components (methods + attributes) of
VALUE ‘ABAP’.
the super class are visible in the
subclass.
………..
 Public static components can be
ENDCLASS.
accessed through the class
CLASS derived_class DEFINITION
component selector used with any
INHERITING FROM base_class.
class in the respective path of
. .. . . . . . . . inheritance tree.
ENDCLASS.

START-OF-SELECTION. Static component is accessed


base_class=>name. through class component
selector from the parent class
derived_class=>name.
as well as from the base class

9 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Method Redefinition


CLASS base_class DEFINITION.
PUBLIC SECTION.  A method can be re-implemented in
METHODS: meth. the derived class using redefinition
........ key word.
ENDCLASS.  The parameters remain same in the
CLASS derived_class DEFINITION INHERITING
FROM base_class .
derived class as it was in base class.
PUBLIC SECTION.
 In the redefined method use the
METHODS: meth REDEFINITION. SUPER pseudo reference to access
. .. . . . . . . . the original method of the base class.
ENDCLASS.

CLASS derived_class IMPLEMENTATION .


METHODS meth. Redefining the base class method in the
CLASS METHOD SUPER->meth. derived class
………
ENDMETHOD. Calling the super class method using
ENDCLASS. SUPER keyword

10 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Instance Constructors


CLASS base_class DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING arg1 TYPE STRING.
PRIVATE SECTION.
 As all public and protected
DATA: fld TYPE STRING. components, subclass inherits the
........ Super class constructor constructor method if it is present in
ENDCLASS. the inheritance tree.
CLASS derived_class DEFINITION INHERITING FROM
base_class .  If an object of subclass is created at
PUBLIC SECTION.
this time the inherited instance
METHODS: constructor IMPORTING arg1 TYPE STRING
arg2 TYPE STRING.
attributes and private attributes of
PRIVATE SECTION. the super classes must also be
DATA: fld TYPE STRING. Sub class constructor initialized.
........
ENDCLASS.
 As the private attributes of the
CLASS derived_class IMPLEMENTATION . superclass is not visible to
METHODS constructor. subclass, subclass cannot fully
CALL METHOD SUPER->constructor
initialize its superclass. Therefore to
EXPORTING arg1 = arg1.
fld = arg2 .
ensure complete initialization of
……… Calling super class constructor subclass and its super classes,
ENDMETHOD. constructor is redefined.
ENDCLASS.

11 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Instance Constructors (Contd.)

 Whether explicit call is required for the instance constructor of the super class or
not , when we instantiate an object of sub class is depends on the following
conditions:

Explicit constructor is Explicit constructor is Explicit call required?


defined in Super class? defined in Sub class?

No No No

Yes No No

No Yes Yes

Yes Yes Yes

12 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Static Constructors


CLASS base_class DEFINITION.
PUBLIC SECTION.
 The redefinition of static constructor
METHODS: class_constructor.
PRIVATE SECTION. works similarly to instance
DATA: fld TYPE STRING. constructor.
........
Super class constructor  When an static constructor is
ENDCLASS.
CLASS derived_class DEFINITION INHERITING redefined then REDEFINITION
FROM base_class . addition is not required.
PUBLIC SECTION.
METHODS: class_constructor .
 No parameter interface is possible for
PRIVATE SECTION. static constructor ( with or without
DATA: fld TYPE STRING. inheritance)
........
Sub class constructor  The runtime environment
ENDCLASS.
CLASS derived_class IMPLEMENTATION . automatically ensures that the static
METHODS class_constructor. constructors are called in the right
fld = ‘ I am sub’ . order, so it is not required to call the
……… No call for super class
static constructor of the super class
ENDMETHOD. constructor
explicitly.
ENDCLASS.

13 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Object References


Object

Class C1
Pub: a1, a2
DATA oref TYPE REF TO C1.
Prot: b1, b2
Priv:c1,c2 CREATE OBJECT oref.

Class C2 Inheriting from C1


Pub: a1, a2,a3,a4 DATA oref TYPE REF TO C2.
Prot: b1,b2,b3,b4
CREATE OBJECT oref.
Priv: c3,c4

Class C3 Inheriting from C2


Pub: a1, a2,a3,a4,a5,a6 DATA oref TYPE REF TO C3.
Prot: b1, b2,b3,b4,b5,b6
CREATE OBJECT oref.
Priv:c5,c6

14 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Object References (Contd.)

Object
DATA : oref1 TYPE REF TO C1,
Class C1
oref3 TYPE REF TO C3.
Pub: a1, a2
Prot: b1, b2
CREATE OBJECT oref3.
Priv:c1,c2

Class C2 Inheriting from C1 oref1 = oref3. Perfect


Pub: a1, a2,a3,a4
Prot: b1,b2,b3,b4 oref1 ->a1. OK
Priv: c3,c4

Class C3 Inheriting from C2 oref1 ->a5. Error

Pub: a1, a2,a3,a4,a5,a6


Prot: b1, b2,b3,b4,b5,b6
Priv:c5,c6

15 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Object References (Contd.)


Object
DATA : oref1 TYPE REF TO C1,
Class C1
oref3 TYPE REF TO C3.
Pub: a1, a2
Prot: b1, b2
Priv:c1,c2 CREATE OBJECT oref1
TYPE C3.
Class C2 Inheriting from C1
CREATE OBJECT oref3.
Pub: a1, a2,a3,a4
Prot: b1,b2,b3,b4
oref1 = oref3.
Priv: c3,c4

Class C3 Inheriting from C2 oref1 ->a1. OK


Pub: a1, a2,a3,a4,a5,a6
Prot: b1, b2,b3,b4,b5,b6 OK
oref1 ->a5.
Priv:c5,c6

16 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO object,  In assignments of reference variables ,


oref2 TYPE REF TO class. static type of a reference variable is
… always more general than the dynamic
oref1 = oref2. type.
Narrowing Cast
 When static type of the target variable
oref2 = oref1. is more general than the static type of
Syntax error the source variable, since the dynamic
occurred type of the source variable at run time
oref2 ?= oref1. can only be more specialized than its
Widening Cast static type, this can be checked during
CATCH SYSTEM-EXCEPTIONS the syntax check. This is known as
MOVE_CAST_ERROR = 4. narrowing cast.
oref2 = oref1.  When static type of the target variable
ENDCATCH. is more specialized than the static type
of the source variable. This is known as
widening cast.

17 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Inheritance : Polymorphism
CLASS super_class DEFINITION.
..........
METHODS test_method.  Reference variables declared with static
ENDCLASS. reference to a super class can
CLASS sub_class_one DEFINITION INHERITING
FROM super_class. dynamically point to an object of a
......... subclass of this super class and access
METHODS test_method REDEFINTION.
the components known to the super
ENDCLASS.
CLASS sub_class_two DEFINITION INHERITING class. Methods inherited from super
FROM super_class. class may be redefined in one or more of
.........
METHODS test_method REDEFINTION. the subclasses. This is the basis of
ENDCLASS. polymorphism using inheritance.
DATA: supr_obj type ref to super_class,  Polymorphism here means using the
sub1_obj type ref to sub_class_one, same name via same interface to
sub2_obj type ref to sub_class_two.
START-OF-SELECTION.
address differently implemented
CREATE OBJECT sub1_obj. methods, belonging to different objects
CREATE OBJECT sub2_obj. of different classes in the inheritance
supr_obj = sub1_obj.
CALL METHOD supr_obj->test_method.
tree.
supr_obj = sub2_obj.
CALL METHOD supr_obj->test_method.

18 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Showing how Inheritance works with ABAP object.

19 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Practice

 Showing how Inheritance works with ABAP object.

20 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

ABAP Objects Advanced : Interface Topics

 Concept
 Defining Interfaces
 Implementing Interface in Classes
 Compound Interfaces
 Alias Names
 Interface References
 Polymorphism through interfaces
 Interface and Inheritance

21 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Concepts
 Like Java, ABAP Objects does not
Interface I1 OBJECT
permit multiple inheritance, but using
DATA: interfaces with inheritance we can
CLASS-DATA: Class C1 achieve the same.
METHODS: Public:  Interface is similar to abstract class
CLASS-METHODS: Interfaces I1 but it has only definitions part.
EVENTS:  The interface can be implemented
CLASS-EVENTS: only in the class that uses it.
Class C2
 Interface, which is an independent
Public: structure, is used to implement in a
Interfaces I1 class to extend the scope of a class.
 Interface allows users to address
Class C3 different classes via a universal
point of contact.
Public:
Interfaces I1

22 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Defining Interfaces


INTERFACE I1
 Interfaces are defines as independent
DATA: construct, in an INTERFACE….
CLASS-DATA: ENDINTERFACE block similar to the
METHODS: declaration block of classes.
CLASS-METHODS:  An interface can declare instance as
well as static components like
EVENTS:
classes.
CLASS-EVENTS:  Interface components are always
ENDINTERFACE PUBLIC, so visibility is not explicitly
defined.
INTERFACE I2.  Interface define the methods but do
DATA : name(20). not Implement them. Similarly how
METHODS: I_method1, sub classes implement the methods
of an abstract class, all classes that
I_method2.
wish to use an interface must
ENDINTERFACE. implement all of its methods.

23 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Implementing Interface in Classes


INTERFACE my_interface .
 Each class can implement one or
METHODS my_interface_method.
ENDINTERFACE.
more interfaces.
 Interfaces expand public visibility
CLASS interface_class DEFINITION. section of the class definition.
PUBLIC SECTION.  The class must implement all the
INTERFACES my_interface.
methods of each incorporated
ENDCLASS.
interface.
 Multiple classes can implement the
CLASS interface_class IMPLEMENTATION. same interface.
METHOD  With in the class each component of
my_interface~my_interface_method.
the interface is identified by the name
DATA: num TYPE I VALUE 10.
intf~comp.so identically name
Write:/ num.
componets of different interface do no
ENDMETHOD.
conflict when implemented in the
ENDCLASS.
same class. “~” is called interface
component selector.

24 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Defining and Implementing Compound Interfaces


INTERFACE I1 .
METHODS meth.  A new interface can be created from
ENDINTERFACE. several existing interface. Such an
INTERFACE I2 . interface is called a compound
METHODS meth. interface. An interface which is
INTERFACES I1. contained in another interface is
ENDINTERFACE. called component interface.
CLASS interface_class DEFINITION.
PUBLIC SECTION.  In a compound interface there is no
INTERFACES: I2. component hierarchy. All component
ENDCLASS. interfaces are on the same level. So it
CLASS interface_class IMPLEMENTATION. is not possible to chain names such
METHOD I1~meth. as I2~I1.
….
ENDMETHOD.  A compound interface contains each
METHOD I2~meth. component interface only once.
….  When a class implements interfaces,
ENDMETHOD.
each component interface is
ENDCLASS.
implemented only once.

25 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Alias names


INTERFACE I1 .
METHODS m1.  Full name of a component in the
ENDINTERFACE. interface when added in class or
INTERFACE I2. another interface is intf~comp, Alias
METHODS : m2. names can be used to replaced this
INTERFACES I1.
ALIASES meth1 FOR I1~m1.
names when defining compound
ENDINTERFACE. interface or declaring interfaces in a
class.
CLASS c1 DEFINITION.
PUBLIC SECTION.  The class implementation must still
INTERFACES : I2.
refer to the full name.
ALIASES meth2 FOR I2~m2.
ENDCLASS.

CLASS C1 IMPLEMENTATION.
METHOD I1~m1. START-OF-SELECTION.
... DATA : oref TYPE REF TO c1
ENDMETHOD. CREATE OBJECT oref.
CALL METHOD : oref->I2~meth1.
METHOD : I2~m2.
CALL METHOD : oref->meth2 .
...
ENDMETHOD.
ENDCLASS.

26 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Interface References


INTERFACE I1.
METHODS : m1.
 As class reference variables are
ENDINTERFACE. declared with the static type of a class
CLASS c1 DEFINITION. (oref TYPE REF TO C1), interface
PUBLIC SECTION. reference variables are declared with
INTERFACES : I1. the static type of a interface (iref
ENDCLASS. TYPE REF TO I1).
 Interface reference variables like class
CLASS C1 IMPLEMENTATION.
METHOD I1~m1. reference variables can contain object
... references, which determine their
ENDMETHOD. dynamic type (Iref = oref. or CREATE
ENDCLASS. OBJECT iref TYPE C1.)
START-OF-SELECTION.  When Interface reference variables is
DATA : oref TYPE REF TO C1,
dynamic and contain object
iref TYPE REF TO I1.
CREATE OBJECT oref.
references, at this time it can only
CALL METHOD : oref->I1~meth1. access the interface components
Iref = oref. implemented in the class of that object.
CALL METHOD : iref->meth1.

27 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Polymorphism
INTERFACE I1.
METHODS : M1 .
ENDINTERFACE.
 Interfaces allow to use different
CLASS C1 DEFINITION.
PUBLIC SECTION.
classes in a uniform way using
INTERFACES : I1. interface references. Any no of class
ENDCLASS.
can implement the interface
CLASS C1 IMPLEMENTATION.
METHOD I1~M1.
differently.
….
ENDMETHOD.  The identical interface reference
ENDCLASS.
CLASS C2 DEFINITION.
variable, statically typed to this
PUBLIC SECTION. interface, operates on multiple
INTERFACES : I1.
ENDCLASS. objects that implement the interface.
CLASS C2 IMPLEMENTATION.
METHOD I1~M1.
….
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2 ,
IREF TYPE REF TO I1 .
CREATE OBJECT : OREF1 ,
OREF2 .
IREF = OREF1.
CALL METHOD IREF->M1.
IREF = OREF2.
CALL METHOD IREF->M1.

28 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Interfaces : Interfaces and Inheritance


 Interfaces can be implemented in
Interface I1 OBJECT
the classes of an inheritance tree,
DATA: each interface only once ( each
Class C1 interface component should have a
CLASS-DATA:
unique name through out the
METHODS: Public:
inheritance tree).
CLASS-METHODS: Interfaces I1
 The interface method components
EVENTS:
can be redefined in the sub classes.
CLASS-EVENTS:
Class C2

Public:
Interfaces I1

Class C3

Public:
Interfaces I1

29 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Working with interfaces in ABAP objects

30 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Practice

 Working with interfaces in ABAP objects

31 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Friends
CLASS c1 DEFINITION FRIENDS  With the FRIEND relationship one
obj1…objn. class can grant another class or
interface ( and all classes that
CLASS c1 DEFINITION LOCAL FRIENDS implement that interface ) access to
obj1…objn. its PROTECTED and PRIVATE
components.
CLASS c1 DEFINITION GLOBAL  Friendship is one sided: class c1
FRIENDS obj1…objn. granting friendship to class c2 does
not mean c1 is a friend of c2 unless
c2 explicitly grants c1 as friend.
 Subclasses of friend and interfaces
that receives a friend as a
component interface become friend.
 A friend of a super class is not
automatically friend of its sub class

32 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Using Friend class in ABAP objects

33 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Practice

 Using Friend class in ABAP objects

34 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Events : Concept

Class C1 Class C2

Publish and
Subscribe
Raises Handles
Mehtods:trigger_e1 Methods:
event e1 event e1
Events: e1 handler_e1

In ABAP object events use the publish and subscribe approach.


 A class declares an event and implements a method to raise (publish) the event.
 The same and/ or other classes implement a method to respond (subscribe) to the event.
 At runtime interested classes and objects register their wish and availability to respond.

35 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Events : Defining and triggering Events


CLASS testing_event DEFINITION.
 Events can be delcared as PUBLIC,
PUBLIC SECTION. Event declaration
EVENTS my_event. PROTECTED, PRIVATE
METHODS components of any class or
event_raising_method. interface.
ENDCLASS.  Events can be static or instance
CLASS testing_event IMPLEMENTATION.  Events can be triggered by any
method in the class using the RAISE
METHOD event_raising_method. EVENT statement.
RAISE EVENTTriggering
my_event.the event
ENDMETHOD.
 The parameter interface for events is
ENDCLASS. limited to EXPORTING parameters,
passed by VALUE.
 Events don’t have implementation
part.

36 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Events : Handling Events


CLASS test DEFINITION.
 Any class can define event handler
PUBLIC SECTION.
methods for the events of the class
itself or for those of other classes.
Declaration for event handler method  Event handler methods use the FOR
METHODS: event_method FOR EVENT
EVENT (event name) OF {CLASS
my_event OF testing_event.
(class name) | INTERFACE (interface
ENDCLASS.
name)} addition.
 Methods are only executed when the
event associated with these methods
CLASS test IMPLEMENTATION. are triggered.
METHOD event_method.
 An event can have multiple event
…..
handler method associate with it.
ENDMETHOD.
ENDCLASS.

37 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Events : Registering events


START-OF-SELECTION.
DATA: object1 TYPE REF TO test,  To automatically execute the event
object2 TYPE REF TO handler method when the event is
testing_event. raised, the handler method has to
be registered.
CREATE OBJECT: object1,
object2.  The registration process is dynamic
i.e. the link is established at
SET HANDLER object1->event_method
Registering the event FOR object2. runtime. The key statement “SET
handler method HANDLER” can register the
handler method.
 Handler methods can be registered
or de-registered dynamically by
Calling the triggering the optional key word
method “ACTIVATION” . “ACTIVATION”
CALL METHOD : can register new method or de-
register existing method.
object2-> event_raising_method.

38 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Events : Runtime Environment


Class C2
e1 handler_e1
e1 handler_e1 Register for Methods:
event e1 handler_e1
SET HANDLER oref2->
handler_e1 FOR oref1
CREATE OBJECT oref2
Class C1
Raises Class C3
Register for
Mehtods:trigger_e1 event e1 Methods:
event e1
Events: e1 Oref1->trigger_e1 handler_e1
SET HANDLER oref3->
handler_e1 FOR oref1
CREATE OBJECT oref1 CREATE OBJECT oref3

 Each object or class that can trigger instance or static events has an invisible
handler table, where entries occur as a result of SET HANDLER statement.
 The RAISE EVENT statement in the triggering method interrupts the execution
of the method until the run time finishes cascading through each handler
registered in the handler table . The initial triggering method then resume
execution.
 To delete single entries from the handler table use the ACTIVATION addition of
the SET HANDLER statement.
39 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation
IBM Global Services

Demonstration

 Creating an Event in a class, triggering that event and handling that event from
another class.

40 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Practice

 Creating an Event in a class, triggering that event and handling that event from
another class.

41 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

ABAP Objects Advanced : Global classes and interfaces

 Concept
 Demonstrating How to create Global class.

42 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Concept

 Global classes and interfaces are created and stored in class pool and interface
pool like function pool (function group).
 Global classes and interfaces are managed centrally and available to every
program.
 Custom Global classes and interfaces must begin with Y* or Z*.
 Global and local classes and interfaces have the same components, and there is
no difference in how they are used within programs.

43 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Concept (Contd.)

 Global classes and interfaces are


maintained via transaction SE24 or
through transaction SE80.
 Use Class Browser to view global
classes and interfaces in the
repository.

44 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Concept (Contd.)

 Set Class Browser filters to select by


object type, by relation ship etc.
 Results are displayed in a hierarchy
tree.

45 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Creating Global class

1. Go to transaction SE80 and choose


Class/Interface and enter the name
of your custom class or interface
(name must start with Z* or Y*) and
press enter. 1
2. In the Popup window choose yes.
3. Choose the Object type. Here Class
is selected as object type.

46 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Creating Global class (Contd.)

4. In the next popup enter description


and press SAVE button. Also check
Usual ABAP class radio button is set

and Final check box is checked. 4


5. In the next popup enter package
name and press save button. In our
example $TMP is selected as a
package.

47 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Creating Global class (Contd.)

6. Now double click on the class, it


will then display the class builder,
define attributes and methods for
the class.
7. Double click on the method name
to implement the method. It will
invoke the code editor where you
have to enter code for the method.
6

48 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Global classes and interfaces : Creating Global class (Contd.)

8. Activate the class.


9. Test the class.
8
10. Class components can include local

classes, methods, internal types,


macros which are not visible out
side the class pool.

9 Macros
9
10
Local class

Types

49 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Demonstration

 Creating a Global Class using transaction SE24

50 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Practice

 Creating a Global Class using transaction SE24

51 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Summary

 In ABAP object, events use the publish and subscribe approach.


 A class declares an event and implements a method to raise (publish) the event.
 The same and/or other classes implement a method to respond (subscribe) to the
event.
 At runtime interested classes and objects register their wish and availability to
respond.
 Inheritance can be single inheritance ( subclass is inherited from a single super
class) or multiple inheritance (subclass is inherited from multiple super classes).
But ABAP Objects supports only single inheritance.
 Like Java, ABAP Objects does not permit multiple inheritance, but using
interfaces with inheritance we can achieve the same.
 Interface is similar to abstract class but it has only definitions part.
 The interface can be implemented only in the class that uses it.

52 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Summary (Contd.)

 With FRIEND relationship one class can grant another class/interface (and all
classes that implemented the interface) access to its PROTECTED and PRIVATE
components.

53 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Questions

 What is an Event?
 What statement we have to use to register an event handler for a specific Event?
 What is Inheritance?
 How can we achieve multiple Inheritance in ABAP?
 How can we access nested interface?
 What is a Friend class?
 Which transaction we use to create Global class?

54 ABAP Objects - Advanced | 11.04.01 March-2005 © 2005 IBM Corporation

You might also like