54
IBM Global Services ABAP Objects - Advanced | 1 1.04.01 March-2005 © 2005 IBM Corporation ABAP Objects - Advanced

ABAP Objects Advanced

Embed Size (px)

DESCRIPTION

object oriendted programming

Citation preview

IBM Global Services

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

ABAP Objects - Advanced

IBM Global Services

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

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

IBM Global Services

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

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

IBM Global Services

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

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

Child

Grand Parent

Parent

ChildMultiple Inheritance not supported by ABAP

Objects

Single Inheritance supported by ABAP

Objects

IBM Global Services

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

Inheritance : Concept (Contd.)

The relationship between the base classes and the derived classes can be represented in an Inheritance tree. Where base classes of each derived class can be retraced along an unique path to a single root node of the tree.

In ABAP Objects, root node of the Inheritance tree is the OBJECT class.

In ABAP, if Inheritance is not specified in the class declaration, the class implicitly inherits from OBJECT class.

OBJECT

Public:

Protected:

Private:

Class C1

Public:

Protected:

Private:

Class C2

Public:

Protected:

Private:

Class C3

Root node

Generalized class

Specialized class

More specialized class

IBM Global Services

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

Inheritance : Syntax and Visibility

CLASS super_class DEFINITION.

PUBLIC SECTION.

Public components

PROTECTED SECTION.

Protected components

PRIVATE SECTION.

Private components

ENDCLASS.

CLASS sub_class DEFINITION INHERITING FROM super_class.

PUBLIC SECTION.

Public components

Public components (super_class)

PROTECTED SECTION.

Protected components

Protected components (super_class)

PRIVATE SECTION.

Private section

ENDCLASS.

The public and protected components of the super class are visible in the subclass.

Private section of the super class is not visible in the sub classes.

Private components of the sub class can have same name as the private component of the super class.

Public and Protected components of the sub class can not have the same name as that have been used in super class.

IBM Global Services

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

Inheritance : Abstract classes and methods

CLASS cl_super DEFINITION.

PUBLIC SECTION.

METHODS: demo1 ABSTRACT,

demo2.

ENDCLASS.

CLASS cl_sub DEFINITION INHERITING FROM cl_super.

PUBLIC SECTION.

METHODS demo1 REDEFINITION.

ENDCLASS.

A class defined as ABSTRACT cannot be instantiated , that is one cannot use CREATE OBJECT with reference to the class.

Abstract class can only be accessed using its static components or its subclasses.

Abstract class serves as a template for subclasses

If a class contains any abstract method the whole class becomes abstract.

A method, which is abstract, should be redefined in derived class.

Redefining the Abstract method of Super class in the Sub class

Abstract method

IBM Global Services

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

Inheritance : Final classes and methods

CLASS final_class DEFINITION FINAL.

. . . . . . . .

ENDCLASS.

CLASS derived_class DEFINITION INHERITING FROM final_class .

. .. . . . . . . .

ENDCLASS.

CLASS super_class DEFINITION .

. . . . . . . .

METHODS final_method FINAL.

ENDCLASS.

CLASS sub_class DEFINITION INHERITING FROM super_class .

. .. . . . . . . .

METHODS final_method redefinition.

ENDCLASS.

A final class can not be inherited further.

All Methods of a final class are inherently final and must not be declared as final in the class definition.

A final method can not be redefined further.

IBM Global Services

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

Inheritance : Static components

CLASS base_class DEFINITION.

PUBLIC SECTION.

CLASS-DATA : name TYPE STRING

VALUE ‘ABAP’.

………..

ENDCLASS.

CLASS derived_class DEFINITION INHERITING FROM base_class.

. .. . . . . . . .

ENDCLASS.

START-OF-SELECTION.

base_class=>name.

derived_class=>name.

The public and protected static components (methods + attributes) of the super class are visible in the subclass.

Public static components can be accessed through the class component selector used with any class in the respective path of inheritance tree.

Static component

Static component is accessed through class component

selector from the parent class as well as from the base class

IBM Global Services

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

Inheritance : Method Redefinition

A method can be re-implemented in the derived class using redefinition key word.

The parameters remain same in the derived class as it was in base class.

In the redefined method use the SUPER pseudo reference to access the original method of the base class.

CLASS base_class DEFINITION.

PUBLIC SECTION.

METHODS: meth.

. . . . . . . .

ENDCLASS.

CLASS derived_class DEFINITION INHERITING FROM base_class .

PUBLIC SECTION.

METHODS: meth REDEFINITION.

. .. . . . . . . .

ENDCLASS.

CLASS derived_class IMPLEMENTATION .

METHODS meth.

CLASS METHOD SUPER->meth.

………

ENDMETHOD.

ENDCLASS.

Redefining the base class method in the derived class

Calling the super class method using SUPER keyword

IBM Global Services

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

Inheritance : Instance ConstructorsCLASS base_class DEFINITION.

PUBLIC SECTION.

METHODS: constructor IMPORTING arg1 TYPE STRING.

PRIVATE SECTION.

DATA: fld TYPE STRING.

. . . . . . . .

ENDCLASS.

CLASS derived_class DEFINITION INHERITING FROM base_class .

PUBLIC SECTION.

METHODS: constructor IMPORTING arg1 TYPE STRING

arg2 TYPE STRING.

PRIVATE SECTION.

DATA: fld TYPE STRING.

. . . . . . . .

ENDCLASS.

CLASS derived_class IMPLEMENTATION .

METHODS constructor.

CALL METHOD SUPER->constructor

EXPORTING arg1 = arg1.

fld = arg2 .

………

ENDMETHOD.

ENDCLASS.

As all public and protected components, subclass inherits the constructor method if it is present in the inheritance tree.

If an object of subclass is created at this time the inherited instance attributes and private attributes of the super classes must also be initialized.

As the private attributes of the superclass is not visible to subclass, subclass cannot fully initialize its superclass. Therefore to ensure complete initialization of subclass and its super classes, constructor is redefined.

Super class constructor

Sub class constructor

Calling super class constructor

IBM Global Services

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

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 defined in Super class?

Explicit constructor is defined in Sub class?

Explicit call required?

No No No

Yes No No

No Yes Yes

Yes Yes Yes

IBM Global Services

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

Inheritance : Static Constructors

CLASS base_class DEFINITION. PUBLIC SECTION. METHODS: class_constructor. PRIVATE SECTION. DATA: fld TYPE STRING. . . . . . . . .ENDCLASS.CLASS derived_class DEFINITION INHERITING FROM base_class . PUBLIC SECTION. METHODS: class_constructor . PRIVATE SECTION. DATA: fld TYPE STRING. . . . . . . . .ENDCLASS.CLASS derived_class IMPLEMENTATION . METHODS class_constructor. fld = ‘ I am sub’ . ……… ENDMETHOD.ENDCLASS.

The redefinition of static constructor works similarly to instance constructor.

When an static constructor is redefined then REDEFINITION addition is not required.

No parameter interface is possible for static constructor ( with or without inheritance)

The runtime environment automatically ensures that the static constructors are called in the right order, so it is not required to call the static constructor of the super class explicitly.

Super class constructor

Sub class constructor

No call for super class constructor

IBM Global Services

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

Inheritance : Object References

Object

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

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

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

Class C3 Inheriting from C2

DATA oref TYPE REF TO C1.

CREATE OBJECT oref.

DATA oref TYPE REF TO C2.

CREATE OBJECT oref.

DATA oref TYPE REF TO C3.

CREATE OBJECT oref.

IBM Global Services

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

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO C1,

oref3 TYPE REF TO C3.

CREATE OBJECT oref3.

oref1 = oref3.

oref1 ->a1.

oref1 ->a5.

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

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

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

Class C3 Inheriting from C2

Object

OK

Error

Perfect

IBM Global Services

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

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO C1,

oref3 TYPE REF TO C3.

CREATE OBJECT oref1

TYPE C3.

CREATE OBJECT oref3.

oref1 = oref3.

oref1 ->a1.

oref1 ->a5.

Pub: a1, a2

Prot: b1, b2

Priv:c1,c2

Class C1

Pub: a1, a2,a3,a4

Prot: b1,b2,b3,b4

Priv: c3,c4

Class C2 Inheriting from C1

Class C3 Inheriting from C2

Object

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

Prot: b1, b2,b3,b4,b5,b6

Priv:c5,c6

OK

OK

IBM Global Services

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

Inheritance : Object References (Contd.)

DATA : oref1 TYPE REF TO object,

oref2 TYPE REF TO class.

oref1 = oref2.

oref2 = oref1.

oref2 ?= oref1.

CATCH SYSTEM-EXCEPTIONS

MOVE_CAST_ERROR = 4.

oref2 = oref1.

ENDCATCH.

In assignments of reference variables , static type of a reference variable is always more general than the dynamic type.

When static type of the target variable is more general than the static type of the source variable, since the dynamic type of the source variable at run time can only be more specialized than its static type, this can be checked during the syntax check. This is known as narrowing cast.

When static type of the target variable is more specialized than the static type of the source variable. This is known as widening cast.

Narrowing Cast

Syntax error occurred

Widening Cast

IBM Global Services

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

Inheritance : PolymorphismCLASS super_class DEFINITION.

. . . . . . . . . . METHODS test_method.

ENDCLASS.CLASS sub_class_one DEFINITION INHERITING FROM super_class.

. . . . . . . . . METHODS test_method REDEFINTION.

ENDCLASS.CLASS sub_class_two DEFINITION INHERITING FROM super_class.

. . . . . . . . . METHODS test_method REDEFINTION.

ENDCLASS.

DATA: supr_obj type ref to super_class, sub1_obj type ref to sub_class_one, sub2_obj type ref to sub_class_two.

START-OF-SELECTION.CREATE OBJECT sub1_obj.CREATE OBJECT sub2_obj.supr_obj = sub1_obj.

CALL METHOD supr_obj->test_method.supr_obj = sub2_obj.

CALL METHOD supr_obj->test_method.

Reference variables declared with static reference to a super class can dynamically point to an object of a subclass of this super class and access the components known to the super class. Methods inherited from super class may be redefined in one or more of the subclasses. This is the basis of polymorphism using inheritance.

Polymorphism here means using the same name via same interface to address differently implemented methods, belonging to different objects of different classes in the inheritance tree.

IBM Global Services

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

Demonstration

Showing how Inheritance works with ABAP object.

IBM Global Services

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

Practice

Showing how Inheritance works with ABAP object.

IBM Global Services

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

ABAP Objects Advanced : Interface Topics

Concept

Defining Interfaces

Implementing Interface in Classes

Compound Interfaces

Alias Names

Interface References

Polymorphism through interfaces

Interface and Inheritance

IBM Global Services

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

Interfaces : Concepts

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.

Interface, which is an independent structure, is used to implement in a class to extend the scope of a class.

Interface allows users to address different classes via a universal point of contact.

Public:

Interfaces I1

Class C1

Public:

Interfaces I1

Class C2

DATA:

CLASS-DATA:

METHODS:

CLASS-METHODS:

EVENTS:

CLASS-EVENTS:

Interface I1 OBJECT

Public:

Interfaces I1

Class C3

IBM Global Services

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

Interfaces : Defining Interfaces

INTERFACE I1

DATA:

CLASS-DATA:

METHODS:

CLASS-METHODS:

EVENTS:

CLASS-EVENTS:

ENDINTERFACE

INTERFACE I2.

DATA : name(20).

METHODS: I_method1,

I_method2.

ENDINTERFACE.

Interfaces are defines as independent construct, in an INTERFACE…. ENDINTERFACE block similar to the declaration block of classes.

An interface can declare instance as well as static components like classes.

Interface components are always PUBLIC, so visibility is not explicitly defined.

Interface define the methods but do not Implement them. Similarly how sub classes implement the methods of an abstract class, all classes that wish to use an interface must implement all of its methods.

IBM Global Services

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

Interfaces : Implementing Interface in Classes

INTERFACE my_interface .METHODS my_interface_method.

ENDINTERFACE.

CLASS interface_class DEFINITION. PUBLIC SECTION.

INTERFACES my_interface.ENDCLASS.

CLASS interface_class IMPLEMENTATION.METHOD my_interface~my_interface_method.

DATA: num TYPE I VALUE 10. Write:/ num.

ENDMETHOD.

ENDCLASS.

Each class can implement one or more interfaces.

Interfaces expand public visibility section of the class definition.

The class must implement all the methods of each incorporated interface.

Multiple classes can implement the same interface.

With in the class each component of the interface is identified by the name intf~comp.so identically name componets of different interface do no conflict when implemented in the same class. “~” is called interface component selector.

IBM Global Services

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

Interfaces : Defining and Implementing Compound InterfacesINTERFACE I1 .

METHODS meth.ENDINTERFACE.INTERFACE I2 .

METHODS meth.INTERFACES I1.

ENDINTERFACE.CLASS interface_class DEFINITION. PUBLIC SECTION.

INTERFACES: I2.ENDCLASS.CLASS interface_class IMPLEMENTATION.METHOD I1~meth. ….ENDMETHOD.METHOD I2~meth. ….ENDMETHOD.ENDCLASS.

A new interface can be created from several existing interface. Such an interface is called a compound interface. An interface which is contained in another interface is called component interface.

In a compound interface there is no component hierarchy. All component interfaces are on the same level. So it is not possible to chain names such as I2~I1.

A compound interface contains each component interface only once.

When a class implements interfaces, each component interface is implemented only once.

IBM Global Services

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

Interfaces : Alias namesINTERFACE I1 . METHODS m1.ENDINTERFACE.

INTERFACE I2. METHODS : m2. INTERFACES I1. ALIASES meth1 FOR I1~m1.ENDINTERFACE.

CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES : I2. ALIASES meth2 FOR I2~m2. ENDCLASS.

CLASS C1 IMPLEMENTATION. METHOD I1~m1. ... ENDMETHOD.

METHOD : I2~m2. ... ENDMETHOD. ENDCLASS.

Full name of a component in the interface when added in class or another interface is intf~comp, Alias names can be used to replaced this names when defining compound interface or declaring interfaces in a class.

The class implementation must still refer to the full name.

START-OF-SELECTION.

DATA : oref TYPE REF TO c1

CREATE OBJECT oref.

CALL METHOD : oref->I2~meth1.

CALL METHOD : oref->meth2 .

IBM Global Services

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

Interfaces : Interface References

INTERFACE I1. METHODS : m1.ENDINTERFACE.CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES : I1.ENDCLASS.

CLASS C1 IMPLEMENTATION. METHOD I1~m1. ... ENDMETHOD.ENDCLASS.START-OF-SELECTION. DATA : oref TYPE REF TO C1, iref TYPE REF TO I1.CREATE OBJECT oref.CALL METHOD : oref->I1~meth1.Iref = oref.CALL METHOD : iref->meth1.

As class reference variables are declared with the static type of a class (oref TYPE REF TO C1), interface reference variables are declared with the static type of a interface (iref TYPE REF TO I1).

Interface reference variables like class reference variables can contain object references, which determine their dynamic type (Iref = oref. or CREATE OBJECT iref TYPE C1.)

When Interface reference variables is dynamic and contain object references, at this time it can only access the interface components implemented in the class of that object.

IBM Global Services

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

Interfaces : Polymorphism

INTERFACE I1. METHODS : M1 .ENDINTERFACE. CLASS C1 DEFINITION. PUBLIC SECTION. INTERFACES : I1. ENDCLASS.

CLASS C1 IMPLEMENTATION. METHOD I1~M1. …. ENDMETHOD. ENDCLASS. CLASS C2 DEFINITION. PUBLIC SECTION. INTERFACES : I1. ENDCLASS. 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.

Interfaces allow to use different classes in a uniform way using interface references. Any no of class can implement the interface differently.

The identical interface reference variable, statically typed to this interface, operates on multiple objects that implement the interface.

IBM Global Services

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

Interfaces : Interfaces and Inheritance

Interfaces can be implemented in the classes of an inheritance tree, each interface only once ( each interface component should have a unique name through out the inheritance tree).

The interface method components can be redefined in the sub classes.

Public:

Interfaces I1

Class C1

Public:

Interfaces I1

Class C2

DATA:

CLASS-DATA:

METHODS:

CLASS-METHODS:

EVENTS:

CLASS-EVENTS:

OBJECT

Public:

Interfaces I1

Class C3

Interface I1

IBM Global Services

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

Demonstration

Working with interfaces in ABAP objects

IBM Global Services

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

Practice

Working with interfaces in ABAP objects

IBM Global Services

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

Friends

CLASS c1 DEFINITION FRIENDS

obj1…objn.

CLASS c1 DEFINITION LOCAL FRIENDS obj1…objn.

CLASS c1 DEFINITION GLOBAL FRIENDS obj1…objn.

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

Friendship is one sided: class c1 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

IBM Global Services

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

Demonstration

Using Friend class in ABAP objects

IBM Global Services

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

Practice

Using Friend class in ABAP objects

IBM Global Services

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

Events : Concept

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.

Mehtods:trigger_e1

Events: e1 Methods:

handler_e1

Class C1 Class C2

Raises event e1

Pu

blish

and

S

ub

scribe

Handles

event e1

IBM Global Services

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

Events : Defining and triggering Events

CLASS testing_event DEFINITION.   PUBLIC SECTION.      EVENTS my_event.      METHODS event_raising_method.   ENDCLASS.

CLASS testing_event IMPLEMENTATION.

   METHOD event_raising_method.      RAISE EVENT my_event.    ENDMETHOD. ENDCLASS.

Events can be delcared as PUBLIC, PROTECTED, PRIVATE components of any class or interface.

Events can be static or instance

Events can be triggered by any method in the class using the RAISE EVENT statement.

The parameter interface for events is limited to EXPORTING parameters, passed by VALUE.

Events don’t have implementation part.

Event declaration

Triggering the event

IBM Global Services

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

Events : Handling Events

CLASS test DEFINITION.

PUBLIC SECTION.

METHODS: event_method FOR EVENT my_event OF testing_event.

ENDCLASS.

CLASS test IMPLEMENTATION.

METHOD event_method.

…..

ENDMETHOD.

ENDCLASS.

Any class can define event handler methods for the events of the class itself or for those of other classes.

Event handler methods use the FOR EVENT (event name) OF {CLASS (class name) | INTERFACE (interface name)} addition.

Methods are only executed when the event associated with these methods are triggered.

An event can have multiple event handler method associate with it.

Declaration for event handler method

IBM Global Services

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

Events : Registering events

START-OF-SELECTION.

DATA: object1 TYPE REF TO test,

object2 TYPE REF TO

testing_event.

CREATE OBJECT: object1,

object2.

SET HANDLER object1->event_method

FOR object2.

CALL METHOD :

object2-> event_raising_method.

To automatically execute the event handler method when the event is raised, the handler method has to be registered.

The registration process is dynamic i.e. the link is established at runtime. The key statement “SET HANDLER” can register the handler method.

Handler methods can be registered or de-registered dynamically by the optional key word “ACTIVATION” . “ACTIVATION” can register new method or de-register existing method.

Registering the event handler method

Calling the triggering method

IBM Global Services

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

Events : Runtime Environment

Mehtods:trigger_e1

Events: e1 Methods:

handler_e1

Class C1 Class C3Raises event e1

Register for event e1

Class C2

Register for event e1

Methods:

handler_e1

e1 handler_e1

e1 handler_e1

CREATE OBJECT oref1

Oref1->trigger_e1

CREATE OBJECT oref3

CREATE OBJECT oref2SET HANDLER oref2->

handler_e1 FOR oref1

SET HANDLER oref3->

handler_e1 FOR oref1

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.

IBM Global Services

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

Demonstration

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

IBM Global Services

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

Practice

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

IBM Global Services

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

ABAP Objects Advanced : Global classes and interfaces

Concept

Demonstrating How to create Global class.

IBM Global Services

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

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.

IBM Global Services

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

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.

IBM Global Services

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

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.

IBM Global Services

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

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.

2. In the Popup window choose yes.

3. Choose the Object type. Here Class is selected as object type.

1

2

3

IBM Global Services

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

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.

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

4

5

IBM Global Services

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

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

7

IBM Global Services

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

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

8. Activate the class.

9. Test the class.

10. Class components can include local

classes, methods, internal types, macros which are not visible out side the class pool.

8

99

10

Types

Local class

Macros

IBM Global Services

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

Demonstration

Creating a Global Class using transaction SE24

IBM Global Services

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

Practice

Creating a Global Class using transaction SE24

IBM Global Services

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

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.

IBM Global Services

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

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.

IBM Global Services

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

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?