Beruflich Dokumente
Kultur Dokumente
March-2005
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
March-2005
Method Redefinition
Constructor in Inheritance Object References in Inheritance Polymorphism through Inheritance
March-2005
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
Multiple Inheritance not supported by ABAP Objects
4 ABAP Objects - Advanced | 11.04.01
March-2005
Generalized class
Class C2
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
March-2005
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.
March-2005
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.
March-2005
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.
ENDCLASS.
March-2005
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.
March-2005
........
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.
March-2005
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
No No Yes Yes
12
March-2005
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
March-2005
oref1 = oref3.
oref1 ->a1.
Perfect OK Error
oref1 ->a5.
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
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
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
Demonstration
Showing how Inheritance works with ABAP object.
19
March-2005
Practice
Showing how Inheritance works with ABAP object.
20
March-2005
Defining Interfaces
Implementing Interface in Classes Compound Interfaces Alias Names
Interface References
Polymorphism through interfaces Interface and Inheritance
21
March-2005
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
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
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
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
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
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 .
March-2005
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
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
March-2005
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.
Demonstration
Working with interfaces in ABAP objects
30
March-2005
Practice
Working with interfaces in ABAP objects
31
March-2005
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.
32
March-2005
Demonstration
Using Friend class in ABAP objects
33
March-2005
Practice
Using Friend class in ABAP objects
34
March-2005
Events : Concept
Class C1
Class C2
Mehtods:trigger_e1 Events: e1
Raises event e1
Handles
Methods: event e1 handler_e1
At runtime interested classes and objects register their wish and availability to respond.
35
March-2005
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
March-2005
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.
ENDMETHOD.
ENDCLASS.
37
March-2005
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.
38
March-2005
e1 handler_e1
Class C1
Mehtods:trigger_e1 Events: e1
CREATE OBJECT oref1
Raises event e1
Oref1->trigger_e1
Class C3
handler_e1
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
Demonstration
Creating an Event in a class, triggering that event and handling that event from another class.
40
March-2005
Practice
Creating an Event in a class, triggering that event and handling that event from another class.
41
March-2005
42
March-2005
43
March-2005
44
March-2005
45
March-2005
2 3
46
March-2005
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
March-2005
6
7
48
March-2005
9 10
Macros
Local class
Types
49 ABAP Objects - Advanced | 11.04.01 March-2005
Demonstration
Creating a Global Class using transaction SE24
50
March-2005
Practice
Creating a Global Class using transaction SE24
51
March-2005
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
March-2005
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
March-2005
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?
54
March-2005