Sie sind auf Seite 1von 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

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

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 Parent Child Single Inheritance supported by ABAP Objects


2005 IBM Corporation

Child
Multiple Inheritance not supported by ABAP Objects
4 ABAP Objects - Advanced | 11.04.01

March-2005

IBM Global Services

Inheritance : Concept (Contd.)


Root node
OBJECT Class C1

Generalized class
Class C2

Public: Protected: Private:

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.

Specialized class
Class C3

Public: Protected: Private:

Public: Protected: Private:


5

More specialized class

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

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

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.

(super_class)
PROTECTED SECTION. Protected components Protected components (super_class) PRIVATE SECTION. Private section ENDCLASS.

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

Inheritance : Abstract classes and methods


Abstract method CLASS cl_super DEFINITION. PUBLIC SECTION.

METHODS: demo1 ABSTRACT,


demo2. ENDCLASS. Redefining the Abstract method of Super class in the Sub class

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.

CLASS cl_sub DEFINITION INHERITING FROM cl_super.


PUBLIC SECTION. METHODS demo1 REDEFINITION. ENDCLASS.

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

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

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.

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.

ENDCLASS.

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 CLASS-DATA : name TYPE STRING VALUE ABAP. .. ENDCLASS. CLASS derived_class DEFINITION INHERITING FROM base_class. . .. . . . . . . . ENDCLASS. START-OF-SELECTION. base_class=>name. Static component is accessed through class component selector from the parent class as well as from the base class

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.

derived_class=>name.

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

Inheritance : Method Redefinition


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.
10 ABAP Objects - Advanced | 11.04.01

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.

Redefining the base class method in the derived class


Calling the super class method using SUPER keyword

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. DATA: fld TYPE STRING. Super class constructor ........ 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. Sub class constructor ........ ENDCLASS. CLASS derived_class IMPLEMENTATION . METHODS constructor. CALL METHOD SUPER->constructor EXPORTING arg1 = arg1. fld = arg2 . Calling super class constructor ENDMETHOD. ENDCLASS.
11 ABAP Objects - Advanced | 11.04.01

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

Explicit constructor is defined in Sub class?


No No Yes Yes

Explicit call required?

No No 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. METHODS: class_constructor. PRIVATE SECTION. DATA: fld TYPE STRING. ........ Super class constructor ENDCLASS. CLASS derived_class DEFINITION INHERITING FROM base_class . PUBLIC SECTION. METHODS: class_constructor . PRIVATE SECTION. DATA: fld TYPE STRING. ........ Sub class constructor ENDCLASS. CLASS derived_class IMPLEMENTATION . METHODS class_constructor. fld = I am sub . No call for super class constructor ENDMETHOD. ENDCLASS.
13 ABAP Objects - Advanced | 11.04.01

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.

March-2005

2005 IBM Corporation

IBM Global Services

Inheritance : Object References


Object Class C1 Pub: a1, a2 Prot: b1, b2 Priv:c1,c2 Class C2 Inheriting from C1 Pub: a1, a2,a3,a4 Prot: b1,b2,b3,b4 Priv: c3,c4 Class C3 Inheriting from C2 Pub: a1, a2,a3,a4,a5,a6 Prot: b1, b2,b3,b4,b5,b6 Priv:c5,c6
14 ABAP Objects - Advanced | 11.04.01

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.

March-2005

2005 IBM Corporation

IBM Global Services

Inheritance : Object References (Contd.)


Object Class C1 Pub: a1, a2 Prot: b1, b2 Priv:c1,c2 Class C2 Inheriting from C1 Pub: a1, a2,a3,a4 Prot: b1,b2,b3,b4 Priv: c3,c4 Class C3 Inheriting from C2 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

DATA : oref1 TYPE REF TO C1,

oref3 TYPE REF TO C3.


CREATE OBJECT oref3.

oref1 = oref3.
oref1 ->a1.

Perfect OK Error

oref1 ->a5.

2005 IBM Corporation

IBM Global Services

Inheritance : Object References (Contd.)


Object Class C1

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. OK OK

Pub: a1, a2
Prot: b1, b2 Priv:c1,c2 Class C2 Inheriting from C1 Pub: a1, a2,a3,a4 Prot: b1,b2,b3,b4 Priv: c3,c4 Class C3 Inheriting from C2 Pub: a1, a2,a3,a4,a5,a6 Prot: b1, b2,b3,b4,b5,b6 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, oref2 TYPE REF TO class. oref1 = oref2. oref2 = oref1. oref2 ?= oref1. Widening Cast CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 4. oref2 = oref1. ENDCATCH. Narrowing Cast Syntax error occurred

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.
March-2005

17

ABAP Objects - Advanced | 11.04.01

2005 IBM Corporation

IBM Global Services

Inheritance : Polymorphism
CLASS 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.
18 ABAP Objects - Advanced | 11.04.01

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.

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
Interface I1 DATA: CLASS-DATA: METHODS: CLASS-METHODS: EVENTS: CLASS-EVENTS: OBJECT Class C1

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.

Public: Interfaces I1

Class C2 Public: Interfaces I1 Class C3 Public: Interfaces I1


22 ABAP Objects - Advanced | 11.04.01

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.

March-2005

2005 IBM Corporation

IBM Global Services

Interfaces : Defining Interfaces


INTERFACE I1 DATA: CLASS-DATA: METHODS: CLASS-METHODS: EVENTS: CLASS-EVENTS:

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.

ENDINTERFACE
INTERFACE I2. DATA : name(20). METHODS: I_method1,

I_method2.
ENDINTERFACE.

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.
March-2005

23

ABAP Objects - Advanced | 11.04.01

2005 IBM Corporation

IBM Global Services

Interfaces : Implementing Interface in Classes


INTERFACE my_interface . METHODS my_interface_method. ENDINTERFACE. CLASS interface_class DEFINITION. PUBLIC SECTION. INTERFACES my_interface. 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.
March-2005

CLASS interface_class IMPLEMENTATION. METHOD my_interface~my_interface_method. DATA: num TYPE I VALUE 10. Write:/ num. ENDMETHOD. ENDCLASS.
24 ABAP Objects - Advanced | 11.04.01

2005 IBM Corporation

IBM Global Services

Interfaces : Defining and Implementing Compound Interfaces


INTERFACE 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.
March-2005

25

ABAP Objects - Advanced | 11.04.01

2005 IBM Corporation

IBM Global Services

Interfaces : Alias names


INTERFACE 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.
26

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 .

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

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.
March-2005

27

ABAP Objects - Advanced | 11.04.01

2005 IBM Corporation

IBM Global Services

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.

28

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

Interfaces : Interfaces and Inheritance


Interface I1 DATA: CLASS-DATA: METHODS: CLASS-METHODS: EVENTS: CLASS-EVENTS: OBJECT Class C1

Public: Interfaces I1

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.

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 obj1objn. CLASS c1 DEFINITION LOCAL FRIENDS obj1objn. CLASS c1 DEFINITION GLOBAL FRIENDS obj1objn.

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

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

Mehtods:trigger_e1 Events: e1

Raises event e1

Handles
Methods: event 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. PUBLIC SECTION. Event declaration EVENTS my_event. METHODS event_raising_method. 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 dont have implementation part.

CLASS testing_event IMPLEMENTATION. METHOD event_raising_method. RAISE EVENT my_event. ENDMETHOD. Triggering the event ENDCLASS.

36

ABAP Objects - Advanced | 11.04.01

March-2005

2005 IBM Corporation

IBM Global Services

Events : Handling Events


CLASS test DEFINITION. PUBLIC SECTION. Declaration for event handler method METHODS: event_method FOR EVENT my_event OF testing_event. 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.

CLASS test IMPLEMENTATION. METHOD event_method. ..

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,

object2 TYPE REF TO


testing_event. CREATE OBJECT: object1, object2. SET HANDLER object1->event_method Registering the event FOR object2. handler 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 deregister existing method.

Calling the triggering method CALL 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: handler_e1 event e1


SET HANDLER oref2-> handler_e1 FOR oref1

CREATE OBJECT oref2

Class C1

Mehtods:trigger_e1 Events: e1
CREATE OBJECT oref1

Raises event e1
Oref1->trigger_e1

Class C3

Register for Methods: event e1


SET HANDLER oref3-> handler_e1 FOR oref1

handler_e1

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.

39

To delete single entries from the handler table use the ACTIVATION addition of the SET HANDLER statement.
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. 2. In the Popup window choose yes.

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

2 3

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.
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
7

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.


10. Class components can include local classes, methods, internal types, macros which are not visible out side the class pool.

9 10

Macros
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

Das könnte Ihnen auch gefallen