Date post: | 06-Apr-2018 |
Category: |
Documents |
Upload: | anmol-bhat |
View: | 224 times |
Download: | 0 times |
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 1/55
© Copyright IBM Corporation 2007
IBM Global Business Services
ABAP Objects - Advanced
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 2/55
IBM Global Services
© 2007 IBM Corporation2 Jan-2007ABAP 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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 3/55
IBM Global Services
© 2007 IBM Corporation3 Jan-2007ABAP Objects - Advanced
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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 4/55
IBM Global Services
© 2007 IBM Corporation4 Jan-2007ABAP Objects - Advanced
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 SubClasses or child classes from existing classes called base classes or SuperClasses 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 thisprocess.
Grand Parent Class
Parent Class
Child Class
Single Inheritance supported
by ABAP Objects
Parent Class Parent Class
Child Class
Multiple Inheritance not
supported by ABAP
Objects
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 5/55
IBM Global Services
© 2007 IBM Corporation5 Jan-2007ABAP Objects - Advanced
Inheritance : Concept (Contd.)
The relationship between thebase classes and the derivedclasses can be represented in anInheritance tree. Where baseclasses of each derived class can
be retraced along an unique pathto a single root node of the tree.
A Super Class is a Generalizationof its Sub Classes and on theother hand a Sub Class is aSpecialization of its Super Class.
Super Class
Public:
Protected:
Private:
Class C1
Public:
Protected:
Private:
Class C2
Public:
Protected:
Private:
Class C3
Root node
Generalized
Class
Specialized
Class
More specialized class
G e n e r a l i z a t i o n
S p e c i a l i z a t
i o n “IS A”
Relationship
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 6/55
IBM Global Services
© 2007 IBM Corporation6 Jan-2007ABAP Objects - Advanced
Inheritance: Advantages
Inheritance permits code Reusability. Reusing existing code removes coderedundancy 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. Thisincreases program Reliability.
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 7/55
IBM Global Services
© 2007 IBM Corporation7 Jan-2007ABAP Objects - Advanced
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 FROMsuper_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 protectedcomponents of the Super Class arevisible in the Sub Class.
Private section of the Super Class isnot visible in the Sub Classes.
Private components of the SubClass can have same name as theprivate component of the SuperClass.
Public and Protected components of
the Sub Class can not have thesame name as that have been usedin Super Class.
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 8/55
IBM Global Services
© 2007 IBM Corporation8 Jan-2007ABAP Objects - Advanced
Inheritance : Abstract classes and methods
CLASS cl_super DEFINITION.
PUBLIC SECTION.
METHODS: demo1 ABSTRACT,
demo2.ENDCLASS.
CLASS cl_sub DEFINITIONINHERITING FROM cl_super.
PUBLIC SECTION.
METHODS demo1 REDEFINITION.
ENDCLASS.
A class defined as ABSTRACT cannot beinstantiated , that is one cannot use CREATEOBJECT with reference to the class.
Abstract class can only be accessed using itsstatic components or its Sub Classes.
Abstract class serves as a template for SubClasses
If a class contains any abstract method thewhole 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 Classescan use with or without redefining them.
Redefining the Abstract method of
Super class in the Sub class
Abstract method
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 9/55
IBM Global Services
© 2007 IBM Corporation9 Jan-2007ABAP Objects - Advanced
Inheritance : Final classes and methods
A final class can not be inherited further.
All Methods of a final class areinherently final and must not bedeclared as final in the class
definition. A final method can not be redefined
further.
If a method of a non-final class is finalthen that class can be inherited but
that method cannot be redefined.
CLASS final_class DEFINITION FINAL.. . . . . . . .
ENDCLASS.
CLASS derived_class DEFINITION INHERITINGFROM final_class .
. .. . . . . . . .
ENDCLASS.
CLASS super_class DEFINITION .
. . . . . . . .
METHODS final_method FINAL.
ENDCLASS.
CLASS sub_class DEFINITION INHERITINGFROM super_class .
. .. . . . . . . .
METHODS final_method redefinition.
ENDCLASS.
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 10/55
IBM Global Services
© 2007 IBM Corporation10 Jan-2007ABAP Objects - Advanced
Inheritance : Static components
The public and protected staticcomponents (methods + attributes) ofthe Super Class are visible in the SubClass.
Public static components can be
accessed through the classcomponent selector used with anyclass in the respective path ofinheritance tree.
CLASS base_class DEFINITION.PUBLIC SECTION.
CLASS-DATA : name TYPE STRING
VALUE „ABAP‟.
………..
ENDCLASS.CLASS derived_class DEFINITIONINHERITING FROM base_class.
. .. . . . . . . .
ENDCLASS.
START-OF-SELECTION.
base_class=>name.
derived_class=>name.
Static component
Static component is accessedthrough class component
selector from the derived class
as well as from the base class
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 11/55
IBM Global Services
© 2007 IBM Corporation11 Jan-2007ABAP Objects - Advanced
Inheritance : Method Redefinition
Redefinition is when theimplementation of an inheritedinstance method is changed for thesub class without changing itssignature.
Static methods cant be redefined.
The parameters remain same in thederived class as it was in base class.
In the redefined method use theSUPER pseudo reference to accessthe 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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 12/55
IBM Global Services
© 2007 IBM Corporation12 Jan-2007ABAP Objects - Advanced
Inheritance : Instance Constructors
As all public and protectedcomponents, Sub Class inherits theconstructor method if it is present inthe inheritance tree.
If an object of Sub Class is createdat this time the inherited instanceattributes and private attributes ofthe Super Classes must also beinitialized.
As the private attributes of theSuper Class is not visible to Sub
Class, Sub Class cannot fullyinitialize its Super Class. Thereforeto ensure complete initialization ofSub Class and its Super Classes,constructor is redefined.
CLASS base_class DEFINITION.
PUBLIC SECTION.
METHODS: constructor IMPORTING arg1 TYPE STRING.
PRIVATE SECTION.
DATA: fld TYPE STRING.
. . . . . . . .
ENDCLASS.
CLASS derived_class DEFINITION INHERITING FROMbase_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.
Super class constructor
Sub class constructor
Calling Super Class
constructor
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 13/55
IBM Global Services
© 2007 IBM Corporation13 Jan-2007ABAP Objects - Advanced
Inheritance : Instance Constructors (Contd.)
Whether explicit call is required for the instance constructor of the Super Class ornot , when we instantiate an object of Sub Class is depends on the followingconditions:
Explicit constructor isdefined in Superclass?
Explicit constructor isdefined in Sub class?
Explicit call required?
No No No
Yes No No
No Yes Yes
Yes Yes Yes
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 14/55
IBM Global Services
© 2007 IBM Corporation14 Jan-2007ABAP Objects - Advanced
Inheritance : Static Constructors
The redefinition of static constructorworks similarly to instanceconstructor.
When an static constructor isredefined then REDEFINITION
addition is not required. No parameter interface is possible for
static constructor ( with or withoutinheritance)
The runtime environmentautomatically ensures that the staticconstructors are called in the rightorder, so it is not required to call thestatic constructor of the Super Classexplicitly.
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.
Super class constructor
Sub class constructor
No call for Super Class
constructor
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 15/55
IBM Global Services
© 2007 IBM Corporation15 Jan-2007ABAP Objects - Advanced
Inheritance : Object References
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
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.
Class C3 Inheriting from C2
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 16/55
IBM Global Services
© 2007 IBM Corporation16 Jan-2007ABAP Objects - Advanced
Inheritance : Object References (Contd.)
DATA : oref1 TYPE REF TO C1,oref3 TYPE REF TO C3.
CREATE OBJECT oref3.
oref1 = oref3.
oref1 ->a1.
oref1 ->a5.
OK
Error
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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 17/55
IBM Global Services
© 2007 IBM Corporation17 Jan-2007ABAP Objects - Advanced
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.
OK
OK
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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 18/55
IBM Global Services
© 2007 IBM Corporation18 Jan-2007ABAP Objects - Advanced
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 isalways more general than the dynamictype.
When static type of the target variable
is more general than the static type ofthe source variable, since the dynamictype of the source variable at run timecan only be more specialized than itsstatic type, this can be checked duringthe syntax check. This is known as
narrowing cast. When static type of the target variable
is more specialized than the static typeof the source variable. This is knownas widening cast.
Narrowing Cast
Syntax error
occurred
Widening Cast
IBM Gl b l S i
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 19/55
IBM Global Services
© 2007 IBM Corporation19 Jan-2007ABAP Objects - Advanced
Inheritance : Polymorphism
CLASS super_class DEFINITION.
. . . . . . . . . .METHODS test_method.
ENDCLASS.
CLASS sub_class_one DEFINITION INHERITINGFROM super_class.
. . . . . . . . .
METHODS test_method REDEFINTION.
ENDCLASS.
CLASS sub_class_two DEFINITION INHERITINGFROM 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 staticreference to a Super Class candynamically point to an object of a SubClass of this Super Class and access thecomponents known to the Super Class.
Methods inherited from Super Class maybe redefined in one or more of the SubClasses. This is the basis ofpolymorphism using inheritance.
Polymorphism here means using thesame name via same interface to
address differently implementedmethods, belonging to different objectsof different classes in the inheritancetree.
IBM Gl b l S i
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 20/55
IBM Global Services
© 2007 IBM Corporation20 Jan-2007ABAP Objects - Advanced
Demonstration
Showing how Inheritance works with ABAP object.
IBM Gl b l S i
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 21/55
IBM Global Services
© 2007 IBM Corporation21 Jan-2007ABAP Objects - Advanced
Practice
Showing how Inheritance works with ABAP object.
IBM Gl b l S i
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 22/55
IBM Global Services
© 2007 IBM Corporation22 Jan-2007ABAP Objects - Advanced
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 Ser ices
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 23/55
IBM Global Services
© 2007 IBM Corporation23 Jan-2007ABAP Objects - Advanced
Interfaces : Concepts
Like Java, ABAP Objects does notpermit multiple inheritance, but usinginterfaces with inheritance we canachieve the same.
Interface is similar to abstract classbut it has only definitions part.
The interface can be implementedonly in the class that uses it.
Interface, which is an independentstructure, is used to implement in aclass to extend the scope of a class.
Interface allows users to addressdifferent classes via a universalpoint of contact.
Public:
Interfaces I1
Class C1
Public:
Interfaces I1
Class C2
DATA:
CLASS-DATA:
METHODS:
CLASS-METHODS:
EVENTS:
CLASS-EVENTS:
Interface I1
Public:
Interfaces I1
Class C3
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 24/55
IBM Global Services
© 2007 IBM Corporation24 Jan-2007ABAP Objects - Advanced
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 independentconstruct, in an INTERFACE….
ENDINTERFACE block similar to thedeclaration block of classes.
An interface can declare instance aswell as static components like
classes.
Interface components are alwaysPUBLIC, so visibility is not explicitlydefined.
Interface define the methods but do
not Implement them. Similarly howSub Classes implement the methodsof an abstract class, all classes thatwish to use an interface mustimplement all of its methods.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 25/55
IBM Global Services
© 2007 IBM Corporation25 Jan-2007ABAP Objects - Advanced
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_classIMPLEMENTATION.
METHOD
my_interface~my_interface_method.DATA: num TYPE I VALUE 10.
Write:/ num.
ENDMETHOD.
ENDCLASS.
Each class can implement one ormore interfaces.
Interfaces expand public visibilitysection of the class definition.
The class must implement all themethods of each incorporatedinterface.
Multiple classes can implement thesame interface.
With in the class each component ofthe interface is identified by the name
intf~comp.so identically namecomponets of different interface do noconflict when implemented in thesame class. “~” is called interface
component selector.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 26/55
IBM Global Services
© 2007 IBM Corporation26 Jan-2007ABAP Objects - Advanced
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 fromseveral existing interface. Such aninterface is called a compoundinterface. An interface which iscontained in another interface iscalled component interface.
In a compound interface there is nocomponent hierarchy. All componentinterfaces are on the same level. So itis not possible to chain names suchas I2~I1.
A compound interface contains eachcomponent interface only once.
When a class implements interfaces,each component interface isimplemented only once.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 27/55
IBM Global Services
© 2007 IBM Corporation27 Jan-2007ABAP Objects - Advanced
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.
Full name of a component in theinterface when added in class oranother interface is intf~comp, Aliasnames can be used to replaced thisnames when defining compoundinterface 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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 28/55
IBM Global Services
© 2007 IBM Corporation28 Jan-2007ABAP Objects - Advanced
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 aredeclared with the static type of aclass (oref TYPE REF TO C1),interface reference variables aredeclared with the static type of ainterface (iref TYPE REF TO I1).
Interface reference variables likeclass reference variables can containobject references, which determinetheir dynamic type (Iref = oref. orCREATE OBJECT iref TYPE C1.)
When Interface reference variablesis dynamic and contain objectreferences, at this time it can onlyaccess the interface componentsimplemented in the class of thatobject.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 29/55
IBM Global Services
© 2007 IBM Corporation29 Jan-2007ABAP Objects - Advanced
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 differentclasses in a uniform way usinginterface references. Any no of classcan implement the interfacedifferently.
The identical interface reference
variable, statically typed to thisinterface, operates on multipleobjects that implement the interface.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 30/55
IBM Global Services
© 2007 IBM Corporation30 Jan-2007ABAP Objects - Advanced
Interfaces : Interfaces and Inheritance
Interfaces can be implemented inthe classes of an inheritance tree,each interface only once ( eachinterface component should have aunique name through out theinheritance tree).
The interface method componentscan be redefined in the SubClasses.
Public:
Interfaces I1
Class C1
Public:
Interfaces I1
Class C2
DATA:
CLASS-DATA:
METHODS:
CLASS-METHODS:
EVENTS:
CLASS-EVENTS:
Public:
Interfaces I1
Class C3
Interface I1
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 31/55
IBM Global Services
© 2007 IBM Corporation31 Jan-2007ABAP Objects - Advanced
Demonstration
Working with interfaces in ABAP objects
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 32/55
IBM Global Services
© 2007 IBM Corporation32 Jan-2007ABAP Objects - Advanced
Practice
Working with interfaces in ABAP objects
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 33/55
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 34/55
IBM Global Services
© 2007 IBM Corporation34 Jan-2007ABAP Objects - Advanced
Demonstration
Using Friend class in ABAP objects
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 35/55
© 2007 IBM Corporation35 Jan-2007ABAP Objects - Advanced
Practice
Using Friend class in ABAP objects
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 36/55
© 2007 IBM Corporation36 Jan-2007ABAP Objects - Advanced
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
P u b l i sh
an d
S u b s cr i b e
Handles
event e1
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 37/55
© 2007 IBM Corporation37 Jan-2007ABAP Objects - Advanced
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 orinterface.
Events can be static or instance
Events can be triggered by anymethod in the class using the RAISEEVENT statement.
The parameter interface for events islimited to EXPORTING parameters,passed by VALUE.
Events don’t have implementationpart.
Event declaration
Triggering the event
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 38/55
© 2007 IBM Corporation38 Jan-2007ABAP Objects - Advanced
Events : Handling Events
CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: event_method FOR EVENTmy_event OF testing_event.
ENDCLASS.
CLASS test IMPLEMENTATION.
METHOD event_method.
….. ENDMETHOD.
ENDCLASS.
Any class can define event handlermethods for the events of the classitself or for those of other classes.
Event handler methods use the FOREVENT (event name) OF {CLASS(class name) | INTERFACE
(interface name)} addition. Methods are only executed when the
event associated with these methodsare triggered.
An event can have multiple event
handler method associate with it.
Declaration for event handler method
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 39/55
© 2007 IBM Corporation39 Jan-2007ABAP Objects - Advanced
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 eventhandler method when the event israised, the handler method has tobe registered.
The registration process is dynamic
i.e. the link is established atruntime. The key statement “SET
HANDLER” can register the
handler method.
Handler methods can be registeredor 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 triggeringmethod
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 40/55
© 2007 IBM Corporation40 Jan-2007ABAP Objects - Advanced
Events : Runtime Environment
Mehtods:trigger_e1
Events: e1
Methods:
handler_e1
Class C1Class C3Raises
event e1Register for
event e1
Class C2
Register forevent 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 invisiblehandler table, where entries occur as a result of SET HANDLER statement.
The RAISE EVENT statement in the triggering method interrupts theexecution of the method until the run time finishes cascading through eachhandler registered in the handler table . The initial triggering method thenresume execution.
To delete single entries from the handler table use the ACTIVATION additionof the SET HANDLER statement.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 41/55
© 2007 IBM Corporation41 Jan-2007ABAP Objects - Advanced
Demonstration
Creating an Event in a class, triggering that event and handling that event fromanother class.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 42/55
© 2007 IBM Corporation42 Jan-2007ABAP Objects - Advanced
Practice
Creating an Event in a class, triggering that event and handling that event fromanother class.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 43/55
© 2007 IBM Corporation43 Jan-2007ABAP Objects - Advanced
ABAP Objects Advanced : Global classes and interfaces
Concept
Demonstrating How to create Global class.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 44/55
© 2007 IBM Corporation44 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Concept
Global classes and interfaces are created and stored in class pool and interfacepool like function pool (function group).
Global classes and interfaces are managed centrally and available to everyprogram.
Custom Global classes and interfaces must begin with Y* or Z*.
Global and local classes and interfaces have the same components, and there isno difference in how they are used within programs.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 45/55
© 2007 IBM Corporation45 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Concept (Contd.)
Global classes and interfaces aremaintained via transaction SE24 orthrough transaction SE80.
Use Class Browser to view globalclasses and interfaces in therepository.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 46/55
© 2007 IBM Corporation46 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Concept (Contd.)
Set Class Browser filters to select byobject type, by relation ship etc.
Results are displayed in a hierarchytree.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 47/55
© 2007 IBM Corporation47 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Creating Global class
1. Go to transaction SE80 and chooseClass/Interface and enter the nameof your custom class or interface(name must start with Z* or Y*) andpress enter.
2. In the Popup window choose yes.
3. Choose the Object type. Here Classis selected as object type.
1
2
3
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 48/55
© 2007 IBM Corporation48 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Creating Global class (Contd.)
4. In the next popup enter descriptionand press SAVE button. Also checkUsual ABAP class radio button is setand Final check box is checked.
5. In the next popup enter packagename and press save button. In our
example $TMP is selected as apackage.
4
5
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 49/55
© 2007 IBM Corporation49 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Creating Global class (Contd.)
6. Now double click on the class, itwill then display the class builder,define attributes and methods forthe class.
7. Double click on the method nameto implement the method. It will
invoke the code editor where youhave to enter code for the method.
6
7
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 50/55
© 2007 IBM Corporation50 Jan-2007ABAP Objects - Advanced
Global classes and interfaces : Creating Global class (Contd.)
8. Activate the class.9. Test the class.
10. Class components can include localclasses, methods, internal types,macros which are not visible outside the class pool.
8
99
10
Types
Local class
Macros
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 51/55
© 2007 IBM Corporation51 Jan-2007ABAP Objects - Advanced
Demonstration
Creating a Global Class using transaction SE24
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 52/55
© 2007 IBM Corporation52 Jan-2007ABAP Objects - Advanced
Practice
Creating a Global Class using transaction SE24
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 53/55
© 2007 IBM Corporation53 Jan-2007ABAP Objects - Advanced
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 theevent.
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 SuperClasses). 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
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 54/55
© 2007 IBM Corporation54 Jan-2007ABAP Objects - Advanced
Summary (Contd.)
With FRIEND relationship one class can grant another class/interface (and allclasses that implemented the interface) access to its PROTECTED and PRIVATEcomponents.
IBM Global Services
8/3/2019 Chapter 02_ABAP Objects - Advanced
http://slidepdf.com/reader/full/chapter-02abap-objects-advanced 55/55
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?