Chapter 02 - ABAP Objects - Advanced
Chapter 02 - ABAP Objects - Advanced
Chapter 02 - ABAP Objects - Advanced
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
Jan-2007
Method Redefinition
Constructor in Inheritance Object References in Inheritance Polymorphism through Inheritance
Jan-2007
Inheritance : Concept
Inheritance is one of the most powerful feature of object oriented programming.
Inheritance is the process of creating new classes, called derived classes or Sub Classes or child classes from existing classes called base classes or Super Classes or parent classes.
The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. The base class is unchanged by this process. Grand Parent Class Parent Class Parent Class
Parent Class
Child Class Single Inheritance supported by ABAP Objects
4 ABAP Objects - Advanced
Child Class
Jan-2007
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. A Super Class is a Generalization of its Sub Classes and on the other hand a Sub Class is a Specialization of its Super Class.
Specialization
Jan-2007
Inheritance: Advantages
Inheritance permits code Reusability. Reusing existing code removes code redundancy and saves time & money. Common components exist only in the Super Class and maintained Centrally. Once a base class is written and tested, it need not be touched again. This increases program Reliability.
Jan-2007
The public and protected components of the Super Class are visible in the Sub Class. 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.
Jan-2007
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 Sub Classes. Abstract class serves as a template for Sub Classes If a class contains any abstract method the whole class becomes abstract. A method, which is abstract, should be redefined in derived class. An Abstract class can declare and implement non abstract methods which its Sub Classes can use with or without redefining them.
Jan-2007
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.
Jan-2007
PUBLIC SECTION.
CLASS-DATA : name TYPE STRING .. 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 Sub Class. Public static components can be accessed through the class component selector used with any class in the respective path of inheritance tree.
Static component is accessed through class component selector from the derived class as well as from the base class
10
Jan-2007
Redefinition is when the implementation of an inherited instance method is changed for the sub class without changing its signature. Static methods cant be redefined. 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
Jan-2007
As all public and protected components, Sub Class inherits the constructor method if it is present in the inheritance tree. If an object of Sub Class 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 Super Class is not visible to Sub Class, Sub Class cannot fully initialize its Super Class. Therefore to ensure complete initialization of Sub Class and its Super Classes, constructor is redefined.
Jan-2007
No No Yes Yes
13
Jan-2007
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.
Jan-2007
15
Jan-2007
oref1 = oref3.
oref1 ->a1. OK Error
oref1 ->a5.
16
Jan-2007
oref1 ->a5.
17
Jan-2007
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.
Jan-2007
18
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.
19 ABAP Objects - Advanced
Reference variables declared with static reference to a Super Class can dynamically point to an object of a Sub Class 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 Sub Classes. 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.
Jan-2007
Demonstration
Showing how Inheritance works with ABAP object.
20
Jan-2007
Practice
Showing how Inheritance works with ABAP object.
21
Jan-2007
Defining Interfaces
Implementing Interface in Classes Compound Interfaces Alias Names
Interface References
Polymorphism through interfaces Interface and Inheritance
22
Jan-2007
Interfaces : Concepts
Interface I1 DATA: CLASS-DATA: METHODS: CLASS-METHODS: EVENTS: CLASS-EVENTS: 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.
Jan-2007
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.
Jan-2007
24
CLASS interface_class IMPLEMENTATION. METHOD my_interface~my_interface_method. DATA: num TYPE I VALUE 10. Write:/ num. ENDMETHOD. ENDCLASS.
25 ABAP Objects - Advanced
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.
Jan-2007
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 .
Jan-2007
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.
Jan-2007
28
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.
29
Jan-2007
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
31
Jan-2007
Practice
Working with interfaces in ABAP objects
32
Jan-2007
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. Sub Classes of friend and interfaces that receives a friend as a component interface become friend.
33
Jan-2007
Demonstration
Using Friend class in ABAP objects
34
Jan-2007
Practice
Using Friend class in ABAP objects
35
Jan-2007
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.
36
Jan-2007
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.
37
Jan-2007
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.
38
Jan-2007
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.
39
Jan-2007
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.
40
To delete single entries from the handler table use the ACTIVATION addition of the SET HANDLER statement.
ABAP Objects - Advanced Jan-2007
Demonstration
Creating an Event in a class, triggering that event and handling that event from another class.
41
Jan-2007
Practice
Creating an Event in a class, triggering that event and handling that event from another class.
42
Jan-2007
43
Jan-2007
44
Jan-2007
45
Jan-2007
46
Jan-2007
2 3
47
Jan-2007
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.
48
Jan-2007
6
7
49
Jan-2007
9 10
Macros
Local class
Types
50 ABAP Objects - Advanced Jan-2007
Demonstration
Creating a Global Class using transaction SE24
51
Jan-2007
Practice
Creating a Global Class using transaction SE24
52
Jan-2007
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 ( Sub Class is inherited from a single Super Class) or multiple inheritance (Sub Class 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.
53
Jan-2007
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.
54
Jan-2007
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?
55
Jan-2007