98432616 abap objects

Upload: chetonas

Post on 04-Jun-2018

258 views

Category:

Documents


2 download

TRANSCRIPT

  • 8/13/2019 98432616 ABAP Objects

    1/74

    ABAP Objects

    General

    What is Object Orientation?

    From Function Groups to Objects

    The Basics of ABAP ObjectsClasses

    Object Handling

    eclaring and Calling !ethods

    "pecial "ections on ABAP Objects

    #nheritance

    #nterfaces

    Triggering and Handling $%ents

    Class and #nterface Pools

    What is Object Orientation?

    In the past, information systems used to be defined primarily by their functionality: data and functionswere kept separate and linked together by means of input and output relations.

    The object-oriented approach, however, focuses on objects that represent abstract or concrete thingsof the real world. These objects are first defined by their character and their properties which arerepresented by their internal structure and their attributes (data . The behaviour of these objects isdescribed by methods (functionality .

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/ec/d9ab291b0b11d295400000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/ec/d9ab291b0b11d295400000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    2/74

    !bjects form a capsule which combines the character to the respective behaviour. !bjects shouldenable programmers to map a real problem and its proposed software solution on a one-to-one basis.

    Typical objects in a business environment are, for e"ample, #$ustomer%, #!rder%, or #Invoice%. &rom'elease .) onwards, the *usiness !bject 'epository (*!' of + eb pplicaton +erver *has contained e"amples of such objects. The *!' object model will be integrated into * !bjectsin the ne"t 'elease by migrating the *!' object types to the * class library.

    comprehensive introduction to object orientation as a whole would go far beyond the limits of thisintroduction to * !bjects. This documentation introduces a selection of terms that are useduniversally in object orientation and also occur in * !bjects. In subse/uent sections, it goes on todiscuss in more detail how these terms are used in * !bjects. The end of this section contains alist of further reading , with a selection of titles about object orientation.

    Objects

    Objects are instances of classes& The' contain data and pro%ides ser%ices& The data forms the attributes ofthe object& The ser%ices are (no)n as methods *also (no)n as operations or functions+& T'picall',methods operate on pri%ate data *the attributes, or state of the object+, )hich is onl' %isible to the methodsof the object& Thus the attributes of an object cannot be changed directl' b' the user, but onl' b' themethods of the object& This guarantees the internal consistenc' of the object&

    Classes

    Classes describe objects& From a technical point of %ie), objects are runtime instances of a class& #ntheor', 'ou can create an' number of objects based on a single class& $ach instance *object+ of a class hasa uni-ue identit' and its o)n set of %alues for its attributes&

    Object References

    #n a program, 'ou identif' and address objects using uni-ue object references& Object references allo) 'outo access the attributes and methods of an object&

    In object-oriented programming, objects usually have the following properties:

    Encapsulation

    Objects restrict the %isibilit' of their resources *attributes and methods+ to other users& $%er' object has aninterface , )hich determines ho) other objects can interact )ith it& The implementation of the object isencapsulated, that is, in%isible outside the object itself&

    Inheritance

    .ou can use an e/isting class to deri%e a ne) class& eri%ed classes inherit the data and methods of thesuperclass& Ho)e%er, the' can o%er)rite e/isting methods, and also add ne) ones&

    Polymorphism

    #dentical *identicall'0named+ methods beha%e differentl' in different classes& #n ABAP Objects, pol'morphism is implemented b' redefining methods during inheritance and b' using constructs calledinterfaces &

  • 8/13/2019 98432616 ABAP Objects

    3/74

    Uses of Object Orientation

    *elow are some of the advantages of object-oriented programming:

    Comple/ soft)are s'stems become easier to understand, since object0oriented structuring pro%ides acloser representation of realit' than other programming techni-ues&

    #n a )ell0designed object0oriented s'stem, it should be possible to implement changes at class le%el,)ithout ha%ing to ma(e alterations at other points in the s'stem& This reduces the o%erall amount ofmaintenance re-uired&

    Through pol'morphism and inheritance, object0oriented programming allo)s 'ou to reuse indi%idualcomponents&

    #n an object0oriented s'stem, the amount of )or( in%ol%ed in re%ising and maintaining the s'stem isreduced, since man' problems can be detected and corrected in the design phase&

    chieving these goals re/uires:

    Object0oriented programming languages

    Object0oriented programming techni-ues do not necessaril' depend on object0oriented programminglanguages& Ho)e%er, the efficienc' of object0oriented programming depends directl' on ho) object0oriented language techni-ues are implemented in the s'stem (ernel&

    Object0oriented tools

    Object0oriented tools allo) 'ou to create object0oriented programs in object0oriented languages& The'allo) 'ou to model and store de%elopment objects and the relationships bet)een them&

    Object0oriented modeling

    The object0orientation modeling of a soft)are s'stem is the most important, most time0consuming, andmost difficult re-uirement for attaining the abo%e goals& Object0oriented design in%ol%es more than justobject0oriented programming, and pro%ides logical ad%antages that are independent of the actualimplementation&

    This section of the * 0ser%s 1uide provides an overview of the object-oriented e"tension of the * language. e have used simple e"amples to demonstrate how to use the new features.2owever, these are not intended to be a model for object-oriented design. 3ore detailed informationabout each of the * !bjects statements is contained in the keyword documentation in the *4ditor. &or a comprehensive introduction to object-oriented software development, you should read oneor more of the titles listed below.

    Further Reading There are many books about object orientation, object-oriented programming languages, object-oriented analysis and design, project management for !! projects, patterns and frameworks, andso on. This is a small selection of good books covering the most important topics:

    Scott Ambler, The Object Primer, SIGS Books & Multime ia !"##$%, ISB ' "(()()*"+(

  • 8/13/2019 98432616 ABAP Objects

    4/74

    A %er' good introduction to object orientation for programmers& #t pro%ides comprehensi%e e/planationsof all essential OO concepts, and contains a procedure model for learning OO -uic(l' and thoroughl'& #t iseas' to read and practical, but still theoreticall'0founded&

    Gra Booch, Object Solutions' Mana-in- the Object.Oriente Project, A ison.Wesle Pub/o !"##0%, ISB ' 1(10210#)+

    A good boo( about all of the non0technical aspects of OO that are e-uall' important for effecti%e object0oriented programming& $as' to read and full of practical tips&

    Martin 3o4ler, 5M6 7istille ' A88l in- the Stan ar Object Mo elin- 6an-ua-e, A ison.Wesle Pub /o !"##+%, ISB ' 1*1"2*0$2*

    An e/cellent boo( about 1!2 *1nified !odeling 2anguage 0 the ne) standardi3ed OO language andnotation for modeling+& Assumes (no)ledge and e/perience of object orientation&

    9rich Gamma, :ichar ;elm, :al8h

  • 8/13/2019 98432616 ABAP Objects

    5/74

    Before 697 6elease :&;, the nearest e-ui%alent of objects in ABAP )ere function modules and functiongroups& "uppose )e ha%e a function group for processing orders& The attributes of an order correspond tothe global data of the function group, )hile the indi%idual function modules represent actions thatmanipulate that data *methods+& This means that the actual order data is encapsulated in the functiongroup, and is ne%er directl' addressed, but instead onl' through the function modules& #n this )a', thefunction modules can ensure that the data is consistent&

    When 'ou run an ABAP program, the s'stem starts a ne) internal session& The internal session has amemor' area that contains the ABAP program and its associated data& When 'ou call a function module,

    an instance of its function group plus its data, is loaded into the memor' area of the internal session& AnABAP program can load se%eral instances b' calling function modules from ifferent function groups&

    The instance of a function group in the memor' area of the internal session almost represents an object inthe sense of object orientation& "ee also the definition in the section What is Object Orientation? & When'ou call a function module, the calling program uses the instance of a function group, based on itsdescription in the Function Builder& The program cannot access the data in the function group directl', butonl' through the function module& The function modules and their parameters are the interface bet)eenthe function group and the user&

    The main difference bet)een real object orientation and function groups is that although a program can)or( )ith the instances of se eral function groups at the same time, it cannot )or( )ith se%eral instancesof a sin-le function group& "uppose a program )anted to use se%eral independent counters, or processse%eral orders at the same time& #n this case, 'ou )ould ha%e to adapt the function group to include

    instance administration, for e/ample, b' using numbers to differentiate bet)een the instances&

    #n practice, this is %er' a)()ard& Conse-uentl', the data is usuall' stored in the calling program, and thefunction modules are called to )or( )ith it *structured programming+& One problem is, for e/ample, thatall users of the function module must use the same data structures as the function group itself& Changingthe internal data structure of a function group affects man' users, and it is often difficult to predict theimplications& The onl' )a' to a%oid this is to rel' hea%il' on interfaces and a techni-ue that guaranteesthat the internal structures of instances )ill remain hidden, allo)ing 'ou to change them later )ithoutcausing an' problems&

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    6/74

    This re-uirement is met b' object orientation& ABAP Objects allo)s 'ou to define data and functions inclasses instead of function groups& 1sing classes, an ABAP program can )or( )ith an' number ofinstances *objects+ based on the same template&

    #nstead of loading a single instance of a function group into memor' implicitl' )hen a function module iscalled, the ABAP program can no) generate the instances of classes e/plicitl' using the ne) ABAPstatement CREATE OBJECT& The indi%idual instances represent uni-ue objects& .ou address these usingobject references& The object references allo) the ABAP program to access the interfaces of the instaces&

    The follo)ing sections contain more information about classes, objects, interfaces, and object referencesin ABAP Objects&

    Instances of 3unction Grou8s' 9@am8leThe follo)ing e/ample sho)s the object0oriented aspect of function groups in the simple case of acounter&

    "uppose )e ha%e a function group called counter counter& 2ocal #nterface #!PO6T#=G @A21$*"$T>@A21$+ count set>%alue&$= F1=CT#O=&

    F1=CT#O= increment>counter& A 8 TO count&$= F1=CT#O=&

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18756b27011d194ef0000e8353423/frameset.htm
  • 8/13/2019 98432616 ABAP Objects

    7/74

    F1=CT#O= get>counter& 2ocal #nterface< $ PO6T#=G @A21$*G$T>@A21$+ get>%alue count&$= F1=CT#O=&

    The function group counter is used as a counter& #t has a global integer field count, and three functionmodules, set>counter, increment>counter, and get>counter, that )or( )ith the field& T)o of the functionmodules ha%e input and output parameters& These form the data interface of the function group& From onobject0oriented point of %ie), a function gorup has e/clusi%el' pri%ate attributes and public methods&

    An' ABAP program can then )or( )ith this function group& For e/ample T4.

    you can ensure that objects of the class class can be created only in the class itself or in itssubclasses. This makes for uni/ue instance management.

    See also'

    !verview 1raphic

    4"ample

    /lasses' O er ie4 Gra8hic

    The left0hand side of the illustration sho)s the declaration and implementation parts of a local class C0&The right0hand side illustrates the structure of the class )ith the components in their respecti%e %isibilit'areas, and the implementation of the methods&

    The public components of the class form the interface bet4een the class an its users & The protectedcomponents are an interface to the subclasses of C0& The pri%ate components are not %isible e/ternall',and are full' encapsulated in the class& The methods in the implementation part ha%e unrestricted access toall components of the class&

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/d1875cb27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d1875cb27011d194ef0000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d1875cb27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    13/74

    /lasses . Intro uctor 9@am8le

    The follo)ing simple e/ample uses ABAP Objects to program a counter& For comparison, see also thee/ample in From Function Groups to Objects

    CLA%% counter DE!#"#T#O".

    PUBL#C %ECT#O". ET1OD%2 (et # PORT#"' )*+ue3(et_)*+ue4 TYPE i5 increment5 get E&PORT#"' )*+ue3get_)*+ue4 TYPE i. PR#VATE %ECT#O". DATA count TYPE i.E"DCLA%%.

    CLA%% counter # PLE E"TAT#O". ET1OD (et. count , (et_)*+ue.

    E"D ET1OD. ET1OD increment. ADD 0 TO count. E"D ET1OD. ET1OD get. get_)*+ue , count. E"D ET1OD.E"DCLA%%.

    The class counter contains three public methods (et , increment , and get & $ach of these )or(s)ith the pri%ate integer field count & T)o of the methods ha%e input and output parameters& These form

    the data interface of the class& The field count is not out)ardl' %isible&The e/ample in the section Wor(ing )ith Objects sho)s ho) 'ou can create instances of the classcounter &

    Object ;an lin-

    Objects

    !bjects are instances of classes. 4ach object has a uni/ue identity and its own attributes. ll transientobjects reside in the conte"t of an internal session (memory area of an * program . ersistentobjects in the database are not yet available. class can have any number of objects (instances .

    Object References

    To access an object from an * program, you use only object references. !bject references are pointers to objects. In * , they are always contained in object reference variables.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18756b27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18756b27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    14/74

    Object Reference Variables

    There are only two types of variable that can contain references in * : object reference variablesand data reference variables for data references . !bject reference variables contain references toobjects. n object reference variable is either initial or contains a reference to an e"isting object. Theidentity of an object depends on its object reference. reference variable that points to an objectknows the identity of that object. The instance-dependent components of an object can only beaddressed using reference variables that point to the object.

    0sers cannot access the identity of the object directly. 'eference variables are based on referencesemantics. hen reference semantics is used and a reference variable is assigned to another one,then it is only the address of the object = the object reference = which is passed and not the attributesof the object. !bject attributes can themselves also be reference variables.

    6ike all variables, you initiali7e object reference variables using the CLEAR statement. The initial valueof a reference variable is a reference that does not point to an object.

    Data ypes for References

    * contains a predefined data type for references, comparable to the data types for structures orinternal tables. The full data type is not defined until the declaration in the * program. The datatype of an object reference variable determines how the program handles its value (that is, the objectreference . There are two general types of object reference: class references and interface references(see Interfaces .

    5ou define class references using the

    ... T5 4 '4& T! class

    addition in the TYPES or DATA statement, where class refers to a class. reference variable with thetype class reference is called a class reference variable, or class reference for short.

    class reference cre allows a user to create an instance (object of the corresponding class. 0sing

    cref-Acomp

    the user can access all visible components co!" of the object which are also contained in the classclass .

    Creating Objects

    ;ow that you defined a class reference variable cre with reference to a class class , you can nowcreate an instance (object of the class.

    $'4 T4 !*B4$T cref CT5 4 classD.

    This statement creates an instance of the class class , and the reference variable cre contains areference to the object. 5ou do not need the TYPE class addition in this case. This addition is onlyimportant in the following two situations: ). if you use inheritance and want to create an instance of aclass class with a class reference variable cre which does not have the type of the class class , or8. if you use interfaces and want to create an instance of a class class with an interface referencevariable #re . fter TYPE you can specify the class dynamically as the contents of a field using thestandard parenthesis synta".

    http://help.sap.com/saphelp_nw2004s/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/14/11e70b0c5c11d3b9350000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    15/74

    !ddressing the Components of Objects

    rograms can only access the instance components of an object using references in object referencevariables. This is done using the object component selector -A ( re is a reference variable :

    To access an attribute *ttr

  • 8/13/2019 98432616 ABAP Objects

    16/74

    cref) E cref8

    the two class references cre & and cre ' must have the same type, that is, they must either refer tothe same class, or the class of cre & must be a superclass of the class of cre ' . In particular, cre &can be the predefined empty class O$%ECT . The class O$%ECT is the superclass of all classes in *!bjects and does not have any components. It has the same function for reference variables as thedata type ANY has for normal variables. 'eference variables with the type O$%ECT can function ascontainers for passing references. 2owever, you cannot use them to address objects.

    In addition, the following relations with interface reference variables can be checked statically.

    iref) E iref8.

    *oth interface references must refer to the same interface, or the interface of #re ' must be nested,containing the interface of #re & as a component.

    iref) E cref).

    The class of the class reference cre & or one of its superclasses must implement the interface of theinterface reference #re & . The interface can be implemented either directly or by using a nestedinterface containing the interface of #re & as a component.

    cref) E iref).

    In this case, the class of cre & must be the class O$%ECT , that is, the reference variable cre & musthave the type REF TO O$%ECT .

    Casting

    henever a static type check is not possible or when the type checks are to be performed at programruntime, you must use the statement

    3!>4 ... FT! ...

    or the casting operator ( () . The casting assignment replaces the assignment operator ( ) . In the MOVE*** ( TO statement, or when you use the casting assignment, there is no static type check.

    Instead, the system checks at runtime whether the object reference in the source variable points to anobject to which the object reference in the target variable can also point. If the assignment is possible,the system makes it, otherwise, the catchable runtime error 3!>4G$ +TG4''!' occurs.

    +ynta" rules force you to use casting whenever a static type check is not possible, for e"ample:

    cref) FE iref).

    2ere, an interface reference is assigned to a class reference. &or the casting to be successful, theobject to which #re r points must be an object of the same class as the class of the class variablecre & or one of its subclasses..

    Object Lifetime

    n object e"ists for as long as it is being used in the program. n object is in use by a program for aslong as at least one reference points to it, or at least one method of the object is registered as an eventhandler.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    17/74

    s soon as there are no more references to an object, and so long as none of its methods areregistered as event handlers, it is deleted by the automatic memory management (garbage collection .The I of the object then becomes free, and can be used by a new object.

    See also'

    !verview 1raphic

    4"ample

    Objects' O er ie4 Gra8hic

    Objects as Instances of a Class

    The above illustration shows a class C& on the left, with its instances represented in the internalsession of an * program on the right. To distinguish them from classes, instances are drawn withrounded corners. The instance names above use the same notation as is used for reference variablesin the ebugger.

    Objects . Intro uctor 9@am8le

    The follo)ing e/ample sho)s ho) to create and handle objects of the class counter from the e/ample in the section Classes &

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3776b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3779b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3779b33a11d194f00000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3776b33a11d194f00000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3776b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3779b33a11d194f00000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    18/74

    The DATA statement creates a class reference variable CREF1 with type C_COUNTER. This variable can c ntainreferences t all instances f the class C_COUNTER. The class C_COUNTER m!st be "n wn t the pr #ram whenthe DATA statement cc!rs. $ ! can either %eclare it l cally bef re the %ata statement in the same pr #ram& r#l bally !sin# the Class '!il%er. The c ntents f CREF1 are initial. The reference % es n t p int t an instance.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b823b3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    19/74

    The CREATE O'(ECT statement creates an b)ect *instance+ f the class C_COUNTER. The reference in thereference variable CREF_1 p ints t this b)ect.

    This instance f the class C_COUNTER is calle% C_COUNTER,1-& beca!se this is h w the c ntents f the b)ectvariable REF_COUNTER_1 are %isplaye% in the %eb!##er after the CREATE O'(ECT statement has beene ec!te%. This name is nly !se% f r internal pr #ram a%ministrati n / it % es n t cc!r in the A'A0 pr #ram itself.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/e4/222536b43611d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad377cb33a11d194f00000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    20/74

    The A'A0 pr #ram can access the p!blic c mp nents f the b)ect !sin# the reference variable CREF1& that is inthis case& it can call the p!blic meth %s f the class C_COUNTER. After the pr #ram n the left/han% si%e has beene ec!te%& the pr #ram variables NU 'ER an% the private b)ect attrib!te COUNT b th have the val!e 2.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b826b3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b823b3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    21/74

    This %eclares three %ifferent class reference variables f r the class C_COUNTER. All f the references are initial.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/e4/222539b43611d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad377cb33a11d194f00000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    22/74

    The system creates three b)ects f the class fr m the three class reference variables. The references in the threereference variables each p int t ne f these b)ects.

    3n the internal pr #ram mana#ement& the in%ivi%!al instances are calle% C_COUNTER,1-& C_COUNTER,4-& an%C_COUNTER,5-. They are name% in the r%er in which they were create%.

    Example

    The A'A0 pr #ram can !se the reference variables t access in%ivi%!al b)ects& that is in this case& t call thep!blic meth %s f the class C_COUNTER.

    Each b)ect has its wn state& since the private instance attrib!te COUNT has a separate val!e f r each b)ect. Thepr #ram n the left/han% si%e a%ministers several in%epen%ent c !nters.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b829b3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/e4/222539b43611d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/e4/22253cb43611d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b826b3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    23/74

    6ere& three class reference variables are %eclare% f r the class C_COUNTER& an% tw b)ects are create% f r theclass. The references in the reference variables CREF1 an% CREF4 each p int t ne f these b)ects. Thereference in CREF5 is initial.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b82cb3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b826b3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    24/74

    After the O7E statement& CREF5 c ntains the same reference as CREF4& an% b th references p int t the b)ectC_COUNTER,4-. A !ser can th!s !se either reference t a%%ress the b)ect C_COUNTER,4-.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b82fb3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b829b3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    25/74

    The C8EAR statement resets the reference in CREF4 t its initial val!e. The reference variable CREF4 thenc ntains the same val!e as it %i% imme%iately after its %eclarati n& an% % es n t p int t an b)ect any m re.

    Example

    http://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b832b3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b82cb3fd11d194f20000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    26/74

    The effect f the assi#nment statement is t c py the reference fr m CREF1 t CREF4. As a res!lt& the reference inCREF5 als p ints t the b)ect C_COUNTER,1-. N m re references p int t the b)ect C_COUNTER,4-& an% itis a!t matically %elete% by the #arba#e c llecti n. The internal name C_COUNTER,4- is n w free a#ain.

    7eclarin- an /allin- Metho s

    This section contains e"plains how to work with methods in * !bjects. &or precise details of therelevant * statements, refer to the corresponding keyword documentation in the * 4ditor. Thee"ample shows how to declare, implement, and call methods.

    (eclaring "ethods

    5ou can declare methods in the declaration part of a class or in an interface. To declare instancemethods, use the following statement:

    34T2! + meth I3 !'TI;1 C> 604(D # ) i 8 ... C D T5 4 type C! TI!; 6D... 4H !'TI;1 C> 604(D e ) e 8 ... C D T5 4 type ...

    $2 ;1I;1 C> 604(D c ) c 8 ... C D T5 4 type C! TI!; 6D... '4T0';I;1 > 604(r4H$4 TI!;+ e+c ) e"c 8 ... .

    and the appropriate additions.

    To declare static methods, use the following statement:

    $6 ++-34T2! + meth...

    *oth statements have the same synta".

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/08/d27c03b81011d194f60000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/73/f6b82fb3fd11d194f20000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    27/74

    hen you declare a method, you also define its parameter interface using the additions IMPORTIN, ,E PORTIN, , CHAN,IN, , and RET.RNIN, . The additions define the input, output, and input output

    parameters, and the return code. They also define the attributes of the interface parameters, namelywhether a parameter is to be passed by reference or value ( VAL.E , its type ( TYPE , and whether it isoptional ( OPTIONAL , DEFA.LT . 0nlike in function modules, the default way of passing a parameter ina method is by reference. To pass a parameter by value, you must do so e"plicitly using the VAL.Eaddition. The return value ( RET.RNIN, parameter must always be passed e"plicitly as a value. This issuitable for methods that return a single output value. If you use it, you cannot use E PORTIN, orCHAN,IN, parameters.

    s in function modules, you can use e"ception parameters ( E CEPTIONS to allow the user to react toerror situations when the method is e"ecuted.

    Implementing "ethods

    5ou must implement all of the methods in a class in the implementation part of the class in a

    34T2! meth. ...4; 34T2! .

    block. hen you implement the method, you do not have to specify any interface parameters, sincethese are defined in the method declaration. The interface parameters of a method behave like localvariables within the method implementation. 5ou can define additional local variables within a methodusing the DATA statement.

    s in function modules, you can use the RAISE e+ce"t#on and MESSA,E RAISIN, statements tohandle error situations.

    hen you implement a static method, remember that it can only work with the static attributes of yourclass. Instance methods can work with both static and instance attributes.

    Static "ethod Call

    To call a method, use the following statement:

    $ 66 34T2! meth 4H !'TI;1 i ) E f ) i 8 Ef 8 ...

    I3 !'TI;1 e ) E g ) e 8 Eg 8 ... $2 ;1I;1 c ) E f ) c 8 Ef 8 ... '4$4I>I;1 r E h

    4H$4 TI!;+ e ) E rc ) e 8 Erc 8 ...

    The way in which you address the method !et/o0 depends on the method itself and from where youare calling it. ithin the implementation part of a class, you can call the methods of the same classdirectly using their name !et/ .

    $ 66 34T2! meth...

    !utside the class, the visibility of the method depends on whether you can call it at all. >isible instancemethods can be called from outside the class using

    $ 66 34T2! ref-Ameth...

  • 8/13/2019 98432616 ABAP Objects

    28/74

    where re is a reference variable whose value points to an instance of the class. >isible instancemethods can be called from outside the class using

    $ 66 34T2! EAmeth...

    where class is the name of the relevant class.

    hen you call a method, you must pass all non-optional input parameters using the E PORTIN, orCHAN,IN, addition in the CALL METHODstatement. 5ou can (but do not have to import the output

    parameters into your program using the IMPORTIN, or RECEIVIN, addition. 4/ually, you can (but donot have to handle any e"ceptions triggered by the e"ceptions using the E CEPTIONS addition.2owever, this is recommended.

    5ou pass and receive values to and from methods in the same way as with function modules, that is,with the synta":

    ... &ormal parameter E ctual parameter

    after the corresponding addition. The interface parameters (formal parameters are always on the left-hand side of the e/uals sign. The actual parameters are always on the right. The e/uals sign is not anassignment operator in this conte"tJ it merely serves to assign program variables to the interface

    parameters of the method.

    If the interface of a method consists only of a single IMPORTIN, parameter, you can use the followingshortened form of the method call:

    $ 66 34T2! method( f .

    The actual parameter is passed to the input parameters of the method.

    If the interface of a method consists only of IMPORTIN, parameters, you can use the followingshortened form of the method call:

    $ 66 34T2! method( i ) E f ) i 8 E f 8 ... .

    4ach actual parameter ) is passed to the corresponding formal parameter # ) .

    (ynamic "ethod Call

    0sing the standard * parenthesis semantics you can call methods dynamically.

    $alling an instance method !et/ :

    CA22 !$THO ref0D*f+

    Calling a static method met6 OB $CT $"C6 D#!PO6T#=G for # PORT#"' parameters

    C2>ABAP>OB $CT $"C6 DCHA=G#=G for C1A"'#"' parameters

    C2>ABAP>OB $CT $"C6 D6$C$#@#=G for RECE#V#"' parameters

    The descriptions depend on the view of the caller. If the specified and actual parameter types differ, thesystem generates the catchable runtime error 5;G$ 66G34T2G ' 3G

  • 8/13/2019 98432616 ABAP Objects

    30/74

    The ; 34 column is the uni/ue table key. &or each e"ception, you can fill e"actly one line of theinternal table. The > 604 component is assigned the numeric value to appear in s2-su1rc after thee"ception is triggered.

    )unctional "ethods

    &unctional methods are methods with any number of IMPORTIN, parameters and one RET.RNIN, parameter. In addition to CALL METHOD you can also use the following e"pressions at operand positions to call functional methods:

    IMPORTING parameters $/pression;one meth( !ne meth( f oder meth( p E f

    n meth( p ) E f ) ... p n E f n

    This notation is currently supported:

    for the source field of the OVE statement

    in arithmetic e/pressions of the CO PUTE statement

    in logical e/pressions

    in the CA%E statement of the CA%E control structure

    in the /1E" statement of the CA%E control structure

    in the /1ERE condition of the statements LOOP AT , DELETE and OD#!Y for internal tables

    The functional method is entered instead of an operand. hen the statement is e"ecuted, the methodis called and the RET.RNIN, parameter passed back is used as the operand.

    E ent *andler "ethods

    4vent handler methods are special methods that cannot all be called using the CALL METHODstatement. Instead, they are triggered using events. 5ou define a method as an event handler methodusing the addition

    ... &!' 4>4;T evt !& cif ...

    in the METHODS or CLASS-METHODS statement.

    The following special rules apply to the interface of an event handler method:

    The interface ma' onl' consist of # PORT#"' parameters&

    $ach # PORT#"' parameter must be an E&PORT#"' parameter of the e%ent e)t &

    The attributes of the parameters are defined in the declaration of the e%ent e)t *EVE"T% statement+and are adopted b' the e%ent handler method&

    +ee also Triggering and 2andling 4vents

    http://help.sap.com/saphelp_nw2004s/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    31/74

    Constructors

    $onstructors are special methods that cannot be called using CALL METHOD . Instead, they are calledautomatically by the system to set the initial state of a new object or class. There are two types ofconstructors - instance constructors and static constructors. $onstructors are methods with a

    predefined name. To use them, you must declare them e"plicitly in the class.

    The instance constructor of a class is the predefined instance method CONSTR.CTOR . 5ou declare it inthe public section as follows:

    34T2! + $!;+T'0$T!'I3 !'TI;1 C> 604(D # ) i 8 ...C D T5 4 type C! TI!; 6D...

    4H$4 TI!;+ e+c ) e"c 8 ... .

    and implement it in the implementation section like any other method. The system calls the instanceconstructor once for each instance of the class, directly after the object has been created in theCREATE O$%ECT statement. 5ou can pass the input parameters of the instance constructor andhandle its e"ceptions using the E PORTIN, and E CEPTIONS additions in the CREATE O$%ECTstatement.

    The static constructor of a class is the predefined static method CLASS_CONSTR.CTOR . 5ou declare itin the public section as follows:

    $6 ++-34T2! + $6 ++G$!;+T'0$T!'.

    and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessedfor the first time. The static constructor cannot therefore access the components of its own class.

    The methods e"ample shows how to use instance and static constructors.

    Methods in ABAP Objects - ExampleThe f ll win# e ample sh ws h w t %eclare& implement& an% !se meth %s in A'A0 Ob)ects.

    Overview

    This e ample !ses three classes calle% C_TEA & C_'39ER& an% C_'3C$C8E. A !ser *a pr #ram+ can createb)ects f the class C_TEA . On a selecti n screen& the class C_TEA as"s f r the n!mber f members f eachteam.

    Each b)ect in the class C_TEA can create as many instances f the class C_'39ER as there are members in theteam. Each instance f the class C_'39ER creates an instances f the class C_'3C$C8E.

    Each instance f the class C_TEA can c mm!nicate with the pr #ram !ser thr !#h an interactive list. Thepr #ram !ser can ch se in%ivi%!al team members f r acti ns. The instances f the class C_'39ER all w thepr #ram !ser t ch se the acti n n a f!rther selecti n screen.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b6554f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    32/74

  • 8/13/2019 98432616 ABAP Objects

    33/74

    Constraints

    The A'A0 statements !se% f r list pr cessin# are n t yet f!lly available in A'A0 Ob)ects.6 wever& t pr %!ce a simple test !tp!t& y ! can !se the f ll win# statements:

    o ;R3TE , ffset-*,len#th-+ ,f-o U83NE

    o ?930o NE;/83NE

    N te: The behavi r f f rmattin# an% interactive list f!ncti ns in their c!rrent state are n t#!arantee%. 3nc mpatible chan#es c !l% cc!r in a f!t!re release.

    Declarations

    This e ample is implemente% !sin# local classes& since selecti n screens bel n# t an A'A0 pr #ram& an% cann tbe %efine% r calle% in #l bal classes. 'el w are the %efiniti ns f the tw selecti n screens an% three classes:

    99999999999999999999999999999999999999999999999999999999999999999999 '+ob*+ %e+ection %creen(9999999999999999999999999999999999999999999999999999999999999999999

    %ELECT#O"8%CREE" BE'#" O!2 %CREE" 0:: T#TLE T#T05 L#"E. PARA ETER% E BER% TYPE # DE!AULT 0:.%ELECT#O"8%CREE" E"D O!2 L#"E5 %CREE" 0::.

    9888888888888888888888888888888888888888888888888888888888888888888

    %ELECT#O"8%CREE" BE'#" O! %CREE" ;:: T#TLE T#T;. PARA ETER%2 DR#VE RAD#OBUTTO" 'ROUP ACT"5 %TOP RAD#OBUTTO" 'ROUP ACT"5 'EARUP RAD#OBUTTO" 'ROUP ACT"5 'EARDO/" RAD#OBUTTO" 'ROUP ACT".%ELECT#O"8%CREE" E"D O! %CREE" ;::.

    99999999999999999999999999999999999999999999999999999999999999999999 C+*(( Definition(9999999999999999999999999999999999999999999999999999999999999999999

    CLA%%2 C_B#

  • 8/13/2019 98432616 ABAP Objects

    34/74

    TE&T03 4 TYPE C5 #D TYPE #5 TE&T;3=4 TYPE C5 TE&T-3>4 TYPE C5 'EAR TYPE #5 TE&T?3=4 TYPE C5 %PEED TYPE #5 E"D O! %TATU%_L#"E_TYPE.

    CLA%%8 ET1OD%2 CLA%%_CO"%TRUCTOR.

    ET1OD%2 CO"%TRUCTOR5 CREATE_TEA 5 %ELECT#O"5 E&ECUT#O".

    PR#VATE %ECT#O".

    CLA%%8DATA2 TEA _ E BER% TYPE #5 COU"TER TYPE #.

    DATA2 #D TYPE #5 %TATU%_L#"E TYPE %TATU%_L#"E_TYPE5

    %TATU%_L#%T TYPE %ORTED TABLE O! %TATU%_L#"E_TYPE /#T1 U"#@UE

  • 8/13/2019 98432616 ABAP Objects

    35/74

    CLA%% C_B#CYCLE DE!#"#T#O".

    PUBL#C %ECT#O".

    ET1OD%2 DR#VE E&PORT#"' VELOC#TY TYPE #5 %TOP E&PORT#"' VELOC#TY TYPE #5 C1A"'E_'EAR # PORT#"' C1A"'E TYPE # RETUR"#"' VALUE3'EAR4 TYPE # E&CEPT#O"% 'EAR_ #" 'EAR_ A&.

    PR#VATE %ECT#O".

    DATA2 %PEED TYPE #5 'EAR TYPE # VALUE 0.

    CO"%TA"T%2 A&_'EAR TYPE # VALUE 0 5 #"_'EAR TYPE # VALUE 0.

    E"DCLA%%.

    9999999999999999999999999999999999999999999999999999999999999999999

    N te that n ne f the three classes has any p!blic attrib!tes. The states f the classes can nly be chan#e% by theirmeth %s. The class C_TEA c ntains a static c nstr!ct r C8A??_CON?TRUCTOR. C_TEA an% C_'39ER b thc ntain instance c nstr!ct rs.

    Implementations

    The implementati n parts f the classes c ntain the implementati ns f all f the meth %s %eclare% in thec rresp n%in# %eclarati n parts. The interfaces f the meth %s have alrea%y been %efine% in the %eclarati ns. 3n theimplementati ns& the interface parameters behave li"e l cal %ata.

    Methods of Class C !EAM

    The f ll win# meth %s are implemente% in the secti n

    CLA%% C_TEA # PLE E"TAT#O".

    ...

    E"DCLA%%.

    C"A## CO$#!%&C!O%

    ET1OD CLA%%_CO"%TRUCTOR. T#T0 , $Te*m member( $. CALL %ELECT#O"8%CREE" 0:: %TART#"' AT -. #! %Y8%UBRC "E :. LEAVE PRO'RA . EL%E. TEA _ E BER% , E BER%. E"D#!. E"D ET1OD.

    The static c nstr!ct r is e ec!te% bef re the class C_TEA is !se% f r the first time in a pr #ram. 3t calls theselecti n screen 1@@ an% sets the static attrib!te TEA _ E 'ER? t the val!e entere% by the pr #ram !ser. Thisattrib!te has the same val!e f r all instances f the class C_TEA .

  • 8/13/2019 98432616 ABAP Objects

    36/74

    CO$#!%&C!O%

    ET1OD CO"%TRUCTOR. COU"TER , COU"TER 0. #D , COU"TER. E"D ET1OD.

    The instance c nstr!ct r is e ec!te% %irectly after each instance f the class C_TEA is create%. 3t is !se% t c !ntthe n!mber f instance f C_TEA in the static attrib!te COUNTER& an% assi#ns the c rresp n%in# n!mber t theinstance attrib!te 3D f each instance f the class.

    C%EA!E !EAM

    ET1OD CREATE_TEA . DO TEA _ E BER% T# E%. CREATE OBJECT B#

  • 8/13/2019 98432616 ABAP Objects

    37/74

    CALL ET1OD B#

  • 8/13/2019 98432616 ABAP Objects

    38/74

    #E"EC! AC!IO$

    ET1OD %ELECT_ACT#O". DATA ACT#V#TY TYPE #. T#T; , $%e+ect *ction for B#

  • 8/13/2019 98432616 ABAP Objects

    39/74

    L#"E8TE&T; , $%t*tu(2$. L#"E8TE&T- , $'e*r , $. L#"E8'EAR , 'EAR_%TATU%. L#"E8TE&T? , $%peed , $. L#"E8%PEED , %PEED_%TATU%. E"D ET1OD.

    The p!blic instance meth % ?TATU?_83NE can be calle% by any !ser f the class c ntainin# a reference variablewith a reference t an instance f the class. 3t fills the str!ct!re% !tp!t parameter 83NE with the c!rrent attrib!teval!es f the c rresp n%in# instance.

    Methods of Class C BIC*C"E

    The f ll win# meth %s are implemente% in the secti n

    CLA%% C_B#CYCLE # PLE E"TAT#O".

    ...

    E"DCLA%%.

    D%I+E

    ET1OD DR#VE. %PEED , %PEED 'EAR 9 0:. VELOC#TY , %PEED. E"D ET1OD.

    The p!blic instance meth % DR37E can be calle% by any !ser f the class c ntainin# a reference variable with areference t an instance f the class. The meth % chan#es the val!e f the private attrib!te ?0EED an% passes it tthe caller !sin# the !tp!t parameter 7E8OC3T$.

    #!OP

    ET1OD %TOP.

    %PEED , :. VELOC#TY , %PEED. E"D ET1OD.

    The p!blic instance meth % ?TO0 can be calle% by any !ser f the class c ntainin# a reference variable with areference t an instance f the class. The meth % chan#es the val!e f the private attrib!te ?0EED an% passes it tthe caller !sin# the !tp!t parameter 7E8OC3T$.

    C,A$ E EA%

    ET1OD C1A"'E_'EAR. 'EAR , E8 'EAR. 'EAR , 'EAR C1A"'E. #! 'EAR 'T A&_'EAR. 'EAR , A&_'EAR. RA#%E 'EAR_ A&. EL%E#! 'EAR LT #"_'EAR. 'EAR , #"_'EAR. RA#%E 'EAR_ #". E"D#!. E8 'EAR , 'EAR. E"D ET1OD.

    The p!blic instance meth % C6AN E_ EAR can be calle% by any !ser f the class c ntainin# a reference variablewith a reference t an instance f the class. The meth % chan#es the val!e f the private attrib!te EAR. ?ince the

  • 8/13/2019 98432616 ABAP Objects

    40/74

    f rmal parameter with the same name bsc!res the attrib!te in the meth %& the attrib!te has t be a%%resse% !sin#the self/reference E/- EAR.

    &sin. the Classes in a Pro.ram

    The f ll win# pr #ram sh ws h w the ab ve classes can be !se% in a pr #ram. The %eclarati ns f the selecti nscreens an% l cal classes& an% the implementati ns f the meth %s m!st als be a part f the pr #ram.

    REPORT OO_ ET1OD%_DE O "O %TA"DARD PA'E 1EAD#"'.

    99999999999999999999999999999999999999999999999999999999999999999999 Dec+*r*tion( *nd #mp+ement*tion(9999999999999999999999999999999999999999999999999999999999999999999

    ...

    99999999999999999999999999999999999999999999999999999999999999999999 '+ob*+ Progr*m D*t*9999999999999999999999999999999999999999999999999999999999999999999

    TYPE% TEA TYPE RE! TO C_TEA .

    DATA2 TEA _BLUE TYPE TEA 5 TEA _'REE" TYPE TEA 5 TEA _RED TYPE TEA .

    DATA COLOR3 4.

    99999999999999999999999999999999999999999999999999999999999999999999 Progr*m e)ent(9999999999999999999999999999999999999999999999999999999999999999999

    %TART8O!8%ELECT#O".

    CREATE OBJECT2 TEA _BLUE5 TEA _'REE"5 TEA _RED.

    CALL ET1OD2 TEA _BLUE8 CREATE_TEA 5TEA _'REE"8 CREATE_TEA 5

    TEA _RED8 CREATE_TEA .

    %ET P!8%TATU% $TEA L#%T$.

    /R#TE $ %e+ect * te*mH $ COLOR , ;.

    9888888888888888888888888888888888888888888888888888888888888888888

    AT U%ER8CO A"D. CA%E %Y8UCO . /1E" $TEA _BLUE$. COLOR , $BLUE $. !OR AT COLOR , 0 #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _BLUE8 %ELECT#O".

    /1E" $TEA _'REE"$. COLOR , $'REE"$. !OR AT COLOR , #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _'REE"8 %ELECT#O".

  • 8/13/2019 98432616 ABAP Objects

    41/74

    /1E" $TEA _RED$. COLOR , $RED $. !OR AT COLOR , > #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _RED8 %ELECT#O".

    /1E" $E&ECUT#O"$. CA%E COLOR. /1E" $BLUE $. !OR AT COLOR , 0 #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _BLUE8 %ELECT#O".

    CALL ET1OD TEA _BLUE8 E&ECUT#O". /1E" $'REE"$.

    !OR AT COLOR , #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _'REE"8 %ELECT#O".CALL ET1OD TEA _'REE"8 E&ECUT#O".

    /1E" $RED $. !OR AT COLOR , > #"TE"%#!#ED O" #"VER%E O". CALL ET1OD TEA _RED8 %ELECT#O".

    CALL ET1OD TEA _RED8 E&ECUT#O". E"DCA%E.

    E"DCA%E.

    9999999999999999999999999999999999999999999999999999999999999999999

    The pr #ram c ntains three class reference variables that refer t the class C_TEA . 3t creates three b)ects fr m

    the class& t which the references in the reference variables then p int. 3n each b)ect& it calls the meth %CREATE_TEA . The meth % C8A??_CON?TRUCTOR f class C_TEA is e ec!te% bef re the first f the b)ectsis create%. The stat!s TEA 83?T f r the basic list all ws the !ser t ch se ne f f !r f!ncti ns:

    ;hen the !ser ch ses a f!ncti n& the event AT U?ER/CO AND is tri##ere% an% p!blic meth %s are calle% inne f the three instances f C_TEA & %epen%in# n the !ser s ch ice. The !ser can chan#e the state f an b)ectby selectin# the c rresp n%in# line in the stat!s list.

    Inheritance

    Inheritance allows you to derive a new class from an e"isting class. 5ou do this using the INHERITIN,FROM addition in the

    $6 ++ subclass 4&I;ITI!; I;24'ITI;1 &'!3 superclass.

    statement. The new class su1class inherits all of the components of the e"isting class su"erclass .The new class is called the subclass of the class from which it is derived. The original class is calledthe superclass of the new class.

    If you do not add any new declarations to the subclass, it contains the same components as thesuperclass. 2owever, only the public and protected components of the superclass are visible in thesubclass. lthough the private components of the superclass e"ist in the subclass, they are not visible.5ou can declare private components in a subclass that have the same names as private componentsof the superclass. 4ach class works with its own private components. 3ethods that a subclass inheritsfrom a superclass use the private attributes of the superclass, and not any private components of thesubclass with the same names.

    If the superclass does not have a private visibility section, the subclass is an e"act replica of thesuperclass. 2owever, you can add new components to the subclass. This allows you to turn the

    http://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/frameset.htm
  • 8/13/2019 98432616 ABAP Objects

    42/74

    subclass into a speciali7ed version of the superclass. If a subclass is itself the superclass of furtherclasses, you introduce a new level of speciali7ation.

    class can have more than one direct subclass, but it may only have one direct superclass. This iscalled single inheritance . hen subclasses inherit from superclasses and the superclass is itself thesubclass of another class, all of the classes involved form an inheritance tree, whose degree ofspeciali7ation increases with each new hierarchical level you add. $onversely, the classes becomemore generali7ed until you reach the root node of the inheritance tree. The root node of all inheritancetrees in * !bjects is the predefined empty class O$%ECT . This is the most generali7ed class

    possible, since it contains neither attributes nor methods. hen you define a new class, you do nothave to specify it e"plicitly as the superclass - the relationship is always implicitly defined. ithin aninheritance tree, two adjacent nodes are the direct superclass or direct subclass of one another. !therrelated nodes are referred to as superclasses and subclasses. The component declarations in asubclass are distributed across all levels of the inheritance tree.

    Redefining "ethods

    ll subclasses contain the components of all classes between themselves and the root node in aninheritance tree. The visibility of a component cannot be changed. 2owever, you can use theREDEFINITION addition in the METHODS statement to redefine an inherited public or protected instancemethod in a subclass and make its function more speciali7ed. hen you redefine a method, you

    cannot change its interface. The method retains the same name and interface, but has a newimplementation.

    The method declaration and implementation in the superclass is not affected when you redefine themethod in a subclass. The implementation of the redefinition in the subclass obscures the originalimplementation in the superclass.

    ny reference that points to an object of the subclass uses the redefined method, even if the referencewas defined with reference to the superclass. This particularly applies to the self-reference !e-3 . If, fore"ample, a superclass method !& contains a call CALL METHOD 4!e-35!' , and !' is redefined in asubclass, calling !& from an instance of the subclass will cause the original method !' to be called,and calling !& from an instance of the subclass will cause the redefined method !' to be called.

    ithin a redefine method, you can use the pseudoreference su"er-3 to access the obscured method.This enables you to use the e"isting function of the method in the superclass without having to recodeit in the subclass.

    !bstract and )inal "ethods and Classes

    The A$STRACT and FINAL additions to the METHODS and CLASS statements allow you to defineabstract and final methods or classes.

    n abstract method is defined in an abstract class and cannot be implemented in that class. Instead, it

    is implemented in a subclass of the class. bstract classes cannot be instantiated.

    final method cannot be redefined in a subclass. &inal classes cannot have subclasses. Theyconclude an inheritance tree.

    References to Subclasses and Polymorphism

    'eference variables defined with reference to a superclass or an interface defined with reference to itcan also contain references to any of its subclasses. +ince subclasses contain all of the components ofall of their superclasses, and given that the interfaces of methods cannot be changed, a referencevariable defined with reference to a superclass or an interface implemented by a superclass can

  • 8/13/2019 98432616 ABAP Objects

    43/74

    contain references to instances of any of its subclasses. In particular, you can define the target variablewith reference to the generic class O$%ECT .

    hen you create an object using the CREATE O$%ECT statement and a reference variable typed withreference to a subclass, you can use the TYPE addition to create an instance of a subclass, to whichthe reference in the reference variable will then point.

    static user can use a reference variable to address the components visible to it in the superclass towhich the reference variable refers. 2owever, it cannot address any speciali7ation implemented in the

    subclass. If you use a dynamic method call, you can address all components of the class.

    If you redefine an instance method in one or more subclasses, you can use a single reference variableto call different implementations of the method, depending on the position in the inheritance tree atwhich the referenced object occurs. This concept that different classes can have the same interfaceand therefore be addressed using reference variables with a single type is called polymorphism.

    Namespace for Components

    +ubclasses contain all of the components of all of their superclasses within the inheritance tree. !fthese components, only the public and protected ones are visible. ll public and protected componentswithin an inheritance tree belong to the same namespace, and conse/uently must have uni/ue names.

    The names of private components, on the other hand, must only be uni/ue within their class.

    hen you redefine methods, the new implementation of the method obscures the method of thesuperclass with the same name. 2owever, the new definition replaces the previous methodimplementation, so the name is still uni/ue. 5ou can use the pseudoreference su"er-3 to access amethod definition in a superclass that has been obscured by a redefinition in a subclass.

    Inheritance and Static !ttributes

    6ike all components, static attributes only e"ist once in each inheritance tree. subclass can accessthe public and protected static attributes of all of its superclasses. $onversely, a superclass shares its

    public and protected static attributes with all of its subclasses. In terms of inheritance, static attributesare not assigned to a single class, but to a part of the inheritance tree. 5ou can change them fromoutside the class using the class component selector with any class name, or within any class in whichthey are shared. They are visible in all classes in the inheritance tree.

    hen you address a static attribute that belongs to part of an inheritance tree, you always address theclass in which the attribute is declared, irrespective of the class you specify in the class selector. This is

    particularly important when you call the static constructors of classes in inheritance. +tatic constructorsare e"ecuted the first time you address a class. If you address a static attribute declared in asuperclass using the class name of a subclass, only the static constructor of the superclass ise"ecuted.

    Inheritance and ConstructorsThere are special rules governing constructors in inheritance.

    !nstance Constructors

    4very class has an instance constructor called constructor . This is an e"ception to the rule thatstates that component names within an inheritance tree must be uni/ue. 2owever, the instanceconstructors of the various classes in an inheritance tree are fully independent of one another. 5oucannot redefine the instance constructor of a superclass in a subclass, neither can you call one

  • 8/13/2019 98432616 ABAP Objects

    44/74

    specifically using the statement CALL METHOD CONSTR.CTOR . $onse/uently, no naming conflicts canoccur.

    The instance constructor of a class is called by the system when you instantiate the class usingCREATE O$%ECT . +ince a subclass contains all of the visible attributes of its superclasses, which canalso be set by instance constructors, the instance constructor of a subclass has to ensure that theinstance constructors of all of its superclasses are also called. To do this, the instance constructor ofeach subclass must contain a CALL METHOD S.PER-3CONSTR.CTOR statement. The only e"ceptionto this rule are direct subclasses of the root node O$%ECT .

    In superclasses without an e"plicitly-defined instance constructor, the implicit instance constructor iscalled. This automatically ensures that the instance constructor of the immediate superclass is called.

    hen you call an instance constructor, you must supply values for all of its non-optional interface parameters. There are various ways of doing this:

    1sing CREATE OBJECT

    #f the class that 'ou are instantiating has an instance constructor )ith an interface, 'ou must pass %alues toit using E&PORT#"' &

    #f the class that 'ou are instantiating has an instance constructor )ithout an interface, 'ou do not pass an' parameters&

    #f the class 'ou are instantiating does not ha%e an e/plicit instance constructor, 'ou must loo( in theinheritance tree for the ne/t0highest superclass )ith an e/plicit instance constructor& #f this has aninterface, 'ou must suppl' %alues using E&PORT#"' & Other)ise, 'ou do not ha%e to pass an' %alues&

    1sing CALL ET1OD %UPER8 CO"%TRUCTOR

    #f the direct superclass has an instance constructor )ith an interface, 'ou must pass %alues to it using

    E&PORT#"' &

    #f the direct superclass has an instance constructor )ithout an interface, 'ou do not pass an' parameters&

    #f the direct superclass does not ha%e an e/plicit instance constructor, 'ou must loo( in the inheritance treefor the ne/t0highest superclass )ith an e/plicit instance constructor& #f this has an interface, 'ou mustsuppl' %alues using E&PORT#"' & Other)ise, 'ou do not ha%e to pass an' %alues&

    In both CREATE O$%ECT and CALL METHOD S.PER-3CONSTR.CTOR , you must look at the ne"t-available e"plicit instance constructor and, if it has an interface, pass values to it. The same applies toe"ception handling for instance constructors. hen you work with inheritance, you need an precise

    knowledge of the entire inheritance tree. hen you instantiate a class at the bottom of the inheritancetree, you may need to pass parameters to the constructor of a class that is much nearer the root node.

    The instance constructor of a subclass is divided into two parts by the CALL METHOD S.PER-3CONSTR.CTOR statement. In the statements before the call, the constructor behaves like a staticmethod, that is, it cannot access the instance attributes of its class. 5ou cannot address instanceattributes until after the call. 0se the statements before the call to determine the actual parameters forthe interface of the instance constructor of the superclass. 5ou can only use static attributes or localdata to do this.

  • 8/13/2019 98432616 ABAP Objects

    45/74

    hen you instantiate a subclass, the instance constructors are called hierarchically. The first nestinglevel in which you can address instance attributes is the highest-level superclass. hen you return tothe constructors of the lower-level classes, you can also successively address their instance attributes.

    In a constructor method, the methods of the subclasses of the class are not visible. If an instanceconstructor calls an instance method of the same class using the implicit self-reference !e-3 , themethod is called as it is implemented in the class of the instance constructor, and not in any redefinedform that may occur in the subclass you want to instantiate. This is an e"ception to the rule that statesthat when you call instance methods, the system always calls the method as it is implemented in theclass to whose instance the reference is pointing.

    Static Constructors

    4very class has a static constructor called class_constructor . s far as the namespace within aninheritance tree, the same applies to static constructors as to instance constructors.

    The first time you address a subclass in a program, its static constructor is e"ecuted. 2owever, beforeit can be e"ecuted, the static constructors of all of its superclasses must already have been e"ecuted.

    static constructor may only be called once per program. Therefore, when you first address asubclass, the system looks for the ne"t-highest superclass whose static constructor has not yet beene"ecuted. It e"ecutes the static constructor of that class, followed by those of all classes between that

    class and the subclass you addressed.

    See also'

    !verview 1raphics

    Inheritance: Introductory 4"ample

    Inheritance' O er ie4 Gra8hic

    http://help.sap.com/saphelp_nw2004s/helpdata/en/1d/df5f54127111d3b9390000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/1d/df5f54127111d3b9390000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/1d/df5f54127111d3b9390000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    46/74

    Inheritance' O er ie$

    The left-hand part of the graphic shows how you can derive a subclass c' from a superclass c& usingthe INHERTIN, FROM addition in the CLASS statement. The right-hand part of the graphic shows thedistribution of the subclass in the inheritance tree, which stretches back to the default empty classO$%ECT . subclass contains all of the components declared above it in the inheritance tree, and canaddress all of them that are declared public or protected.

    Single Inheritance

  • 8/13/2019 98432616 ABAP Objects

    47/74

  • 8/13/2019 98432616 ABAP Objects

    48/74

    CLA%% counter # PLE E"TAT#O". ET1OD (et. count , (et_)*+ue. E"D ET1OD. ET1OD increment. ADD 0 TO count. E"D ET1OD. ET1OD get. get_)*+ue , count.

    E"D ET1OD.E"DCLA%%.

    CLA%% counter_ten DE!#"#T#O" #"1ER#T#"' !RO counter.PUBL#C %ECT#O". ET1OD% increment REDE!#"#T#O" . DATA count_ten.E"DCLA%%.

    CLA%% counter_ten # PLE E"TAT#O". ET1OD increment.

    DATA modu+o TYPE i. CALL ET1OD (uper8 increment . rite F count. modu+o , count mod 0:. #! modu+o , :. count_ten , count_ten 0. rite count_ten. E"D#!. E"D ET1OD.E"DCLA%%.

    DATA2 count TYPE RE! TO counter5 number TYPE i VALUE .

    %TART8O!8%ELECT#O".

    CREATE OBJECT count TYPE counter_ten .

    CALL ET1OD count8 (et E&PORT#"' (et_)*+ue , number.

    DO ;: T# E%. CALL ET1OD count8 increment. E"DDO.

    The class counter_ten is deri%ed from counter & #t redefines the method increment & To do this,'ou must change the %isibilit' of the count attribute from PR#VATE to PROTECTED& The redefinedmethod calls the obscured method of the superclass using the pseudoreference (uper8 & The redefinedmethod is a speciali3ation of the inherited method&

    The e/ample instantiates the subclass& The reference %ariable pointing to it has the t'pe of the superclass&When the #"CRE E"T method is called using the superclass reference, the s'stem e/ecutes the redefinedmethod from the subclass&

  • 8/13/2019 98432616 ABAP Objects

    49/74

  • 8/13/2019 98432616 ABAP Objects

    50/74

    hen you implement an interface in a class, the components of the interface are added to the othercomponents in the public section. component #co!" of an interface #nt can be addressed asthough it were a member of the class under the name #nt K#co!" .

    The class must implement the methods of all interfaces implemented in it. The implementation part ofthe class must contain a method implementation for each interface method #!et/ :

    34T2! intfKimeth.

    ...

    4; 34T2! .

    Interfaces can be implemented by different classes. 4ach of these classes is e"tended by the same setof components. The methods of the interface can be implemented differently in each class. 2owever,each class should keep the semantics of the interface. method implementation should provide

    precisely that functionality which is re/uired by the interface.

    Interfaces allow you to use different classes in a uniform way using interface references(polymorphism . &or e"ample, interfaces that are implemented in different classes e"tend the publicscope of each class by the same set of components. If a class does not have any class-specific public

    components, the interfaces define the entire public face of the class.

    Interface References

    'eference variables allow you to access objects (refer to orking with !bjects . Instead of creatingreference variables with reference to a class, you can also define them with reference to an interface.This kind of reference variable can contain references to objects of classes that implement thecorresponding interface.

    To define an interface reference, use the addition TYPE REF TO #nt in the TYPES or DATAstatement. #nt must be an interface that has been declared to the program before the actualreference declaration occurs. reference variable with the type interface reference is called aninterface reference variable, or interface reference for short.

    n interface reference #re allows a user to use the form #re -3#co!" to address all visibleinterface components #co!" of the object to which the object reference is pointing. It allows the user toaccess all of the components of the object that were added to its definition by the implementation of theinterface.

    "ddressing Objects Using !nterface References

    If a class class implements an interface #nt , you can use the following assignment between theclass reference variable cre and an interface reference #re to make the interface reference in#re point to the same object as the class reference in cre :

    iref E cref

    If a class class implements an interface #nt , you do not need to create a class reference variablecre with reference to the class first in order to create an object of the class class . Instead, you canuse the TYPE addition in the CREATE O$%ECT statement to create an instance of the class with aninterface reference variable.

    $'4 T4 !*B4$T iref T5 4 class.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    51/74

    This creates an instance of the class class to which the reference in #re points.

    If the interface #nt contains an instance attribute attr and an instance method !et/ , you canaddress the interface components as follows:

    0sing the class reference ariable cre :

    To access an attribute *ttr < cref0Dintf attr

    To call a method met6

  • 8/13/2019 98432616 ABAP Objects

    52/74

    definition of #6 , e"pressions like #&7co!" or #'7co!" cannot be used, with the e"ception of the ALIAS statement.

    There are several ways how you can use the components of component interfaces:

    Using "lias #ames

    5ou can use the ALIAS statement in interface definitions to assign alias names to the components ofcomponent interfaces. This makes these components visible in the interface definition.

    I;T4'& $4 i8. ... I;T4'& $4+ i). 6I +4+ alias8) &!' i)Kcomp). ...4; I;T4'& $4.

    I;T4'& $4 i . ... I;T4'& $4+ i8. 6I +4+ alias ) &!' i8Kalias8). 6I +4+ alias 8 &!' i8Kcomp8. ...4; I;T4'& $4.

    "ssigning !nterface References

    5ou can assign interface references typed with reference to one of the component interfaces tointerface references typed with reference to a nested interface. 5ou can then use the interfacereferences typed with reference to a component interface to address the components of the componentinterfaces.

    I;T4'& $4 i8. ... I;T4'& $4+ i). ...4; I;T4'& $4.

    I;T4'& $4 i . ... I;T4'& $4+ i8. ...4; I;T4'& $4.

    T : iref) T5 4 '4& T! i), iref8 T5 4 '4& T! i8, iref T5 4 '4& T! i .

    iref8 E iref .iref) E iref8.

    ... iref)-Acomp) ...

    ... iref8-Acomp8 ...

    The following e"pressions are not possible:

  • 8/13/2019 98432616 ABAP Objects

    53/74

  • 8/13/2019 98432616 ABAP Objects

    54/74

    The concepts of interfaces and of inheritance are completely orthogonal. In the classes of aninheritance tree you can implement as many interfaces as re/uired. 2owever, you must pay attentionto the fact that each interface can be implemented only once per inheritance tree. This ensures thateach interface component has a uni/ue name #nt 7#co!" across the entire inheritance tree and iscontained in all subclasses starting with the class that implements the interface. Interface referencesthat can point to a class of an inheritance tree can also point to all subclasses. !nce they have beenimplemented, interface methods are fully-fledged components of a class and can be redefined insubclasses. 2owever, you cannot declare interface methods as abstract or final in the definition of theinterface.

    See also

    !verview 1raphics

    Interfaces: Introductory 4"ample

    Interfaces' O er ie4 Gra8hic

    The left0hand side of the diagram sho)s the definition of a local interface #0 and the declaration andimplementation parts of a local class C0 that implements the interface #0 in its 8ublic section & Theinterface method #0K 0 is implemented in the implementation part of the class& .ou cannot implementinterfaces in the other %isibilit' sections&

    The right0hand side illustrates the structure of the class )ith the components in their respecti%e %isibilit'areas, and the implementation of the methods& The interface components e/tend the public scope of theclass& All users can access the public components specific to the class and those of the interface&

    http://help.sap.com/saphelp_nw2004s/helpdata/en/55/e47846b4d611d194f30000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/4aad83b75211d194f50000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/93/83a0a6b4e811d194f30000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/55/e47846b4d611d194f30000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/55/e47846b4d611d194f30000e8353423/content.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/4aad83b75211d194f50000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    55/74

    Overview raphics for Interfaces

    Classes can implement any n!mber f interfaces in their p!blic secti n. The left/han% si%e f the %ia#ram sh ws the%eclarati n an% implementati n part f a l cal class C1& which implements several interfaces in its p!blic secti n.The class m!st implement all f the interface meth %s in its implementati n part.

    The ri#ht/han% si%e ill!strates the str!ct!re f the class with the c mp nents in their respective visibility areas& an%the implementati n f the meth %s. The interface c mp nents e ten% the p!blic secti n f the class in which theyare %eclare%.

    Interfaces . Intro uctor 9@am8le

    The follo)ing simple e/ample sho)s ho) 'ou can use an interface called (t*tu( to output the attributesof an object and illustrates ho) this interface is implemented in t)o different classes&

    REPORT demo_interf*ce.

    #"TER!ACE (t*tu(. ET1OD% rite.E"D#"TER!AC E.

    CLA%% counter DE!#"#T#O". PUBL#C %ECT#O". #"TER!ACE% (t*tu(. ET1OD% increment.

    http://help.sap.com/saphelp_nw2004s/helpdata/en/48/4aad83b75211d194f50000e8353423/frameset.htm
  • 8/13/2019 98432616 ABAP Objects

    56/74

    PR#VATE %ECT#O". DATA count TYPE i.E"DCLA%%.

    CLA%% counter # PLE E"TAT#O". ET1OD (t*tu(K rite . /R#TE2 F $Count in counter i($5 count. E"D ET1OD. ET1OD increment.

    ADD 0 TO count. E"D ET1OD.E"DCLA%%.

    CLA%% bic7c+e DE!#"#T#O". PUBL#C %ECT#O". #"TER!ACE% (t*tu(. ET1OD% dri)e. PR#VATE %ECT#O". DATA (peed TYPE i.E"DCLA%%.

    CLA%% bic7c+e # PLE E"TAT#O". ET1OD (t*tu(K rite . /R#TE2 F $%peed of bic7c+e i($5 (peed. E"D ET1OD. ET1OD dri)e. ADD 0: TO (peed. E"D ET1OD.E"DCLA%%.

    The interface (t*tu( contains one method called rite & The classes counter and

    bic7+e implement the interface in the public section& Both classes must implement the interface methodin their implementation part in accordance )ith the semantics re-uired&

    The follo)ing sections e/plain ho) a user can use interface references to address the objects of bothclasses 604(e) e8 ... T5 4 type C! TI!; 6D..

    To declare static events, use the following statement:

    http://help.sap.com/saphelp_nw2004s/helpdata/en/71/a8a77955bc11d194aa0000e8353423/frameset.htmhttp://help.sap.com/saphelp_nw2004s/helpdata/en/48/4aad89b75211d194f50000e8353423/content.htm
  • 8/13/2019 98432616 ABAP Objects

    60/74

  • 8/13/2019 98432616 ABAP Objects

    61/74

    Registering E$ent %andler &ethods

    To allow an event handler method to react to an event, you must determine at runtime the trigger towhich it is to react. 5ou can do this with the following statement:

    +4T 2 ; 64' h ) h 8 ... C&!'D...

    It links a list of handler methods with corresponding trigger methods. There are four different types ofevent.

    It can be

    An instance e%ent declared in a class

    An instance e%ent declared in an interface

    A static e%ent declared in a class

    A static e%ent declared in an interface

    The synta" and effect of the SET HANDLER depends on which of the four cases listed above applies.

    &or an instance event, you must use the FOR addition to specify the instance for which you want toregister the handler. 5ou can either specify a single instance as the trigger, using a reference variablere :

    +4T 2 ; 64' h ) h 8 ... &!' ref.

    or you can register the handler for all instances that can trigger the event:

    +4T 2 ; 64' h ) h 8 ... &!' 66 I;+T ;$4+.

    The registration then applies even to triggering instances that have not yet been created when youregister the handler.

    5ou cannot use the FOR addition for static events:

    +4T 2 ; 64' h ) h 8 ...

    The registration applies automatically to the whole class, or to all of the classes that implement theinterface containing the static event. In the case of interfaces, the registration also applies to classesthat are not loaded until after the handler has been registered.

    iming of E$ent %andling

    fter the RAISE EVENT statement, all registered handler methods are e"ecuted before the ne"tstatement is processed (synchronous event handling . If a handler method itself triggers events, itshandler methods are e"ecuted before the original handler method continues. To avoid the possibility ofendless recursion, events may currently only be nested L9 deep.

    2andler methods are e"ecuted in the order in which they were registered. +ince event handlers areregistered dynamically, you should not assume that they will be processed in a particular order.Instead, you should program as though all event handlers will be e"ecuted simultaneously.

  • 8/13/2019 98432616 ABAP Objects

    62/74

    See also'

    4vents: !verview 1raphic

    4vents: Introductory 4"ample

    4vents: $omple" 4"ample

    9 ents' O er ie4 Gra8hic

    "uppose )e ha%e t)o classes, C0 and C; class_constructor? user_act#on*

    ENDCLASS*

    :----------------------------------------------------------------

    CLASS c_l#st DEFINITION*

    P.$LIC SECTION*

    METHODS> co0e_/an0ler FOR EVENT 1utton_cl#c8e0 OF status IMPORTIN, co0e? l#st_c/an9e FOR EVENT s"ee0_c/an9e OF #_ve/#cle IMPORTIN, ne #0 TYPE #? re _s/#" TYPE REF TO c_s/#"? re _truc8 TYPE REF TO c_truc8?

    $E,IN OF l#ne? #0 TYPE I? la9;&= TYPE c? #re TYPE REF TO #_ve/#cle? s"ee0 TYPE #? END OF LINE? LIST LI@E SORTED TA$LE OF l#ne ITH .NIB.E @EY #0*

    ENDCLASS*

    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    The following events are declared in the e"ample:

    The instance e%ent (peed_c6*nge in the interface i_)e6ic+e

  • 8/13/2019 98432616 ABAP Objects

    68/74

    The static e%ent button_c+icIed in the class (t*tu( &

    The class c_l#st contains event handler methods for both events.

    ;ote that the class status does not have any attributes, and therefore only works with static methodsand events.

    Implementations

    *elow are the implementations of the methods of the above classes:

    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: : I!"le!entat#ons:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    CLASS c_s/#" IMPLEMENTATION*

    METHOD constructor* !a+ ) 6 * ENDMETHOD*

    METHOD #_ve/#cle70r#ve* CHEC@ s/#"_s"ee0 !a+* s/#"_s"ee0 ) s/#"_s"ee0 & * RAISE EVENT #_ve/#cle7s"ee0_c/an9e E PORTIN, ne

  • 8/13/2019 98432616 ABAP Objects

    69/74

    E PORTIN, ne

  • 8/13/2019 98432616 ABAP Objects

    70/74

    MODIFY TA$LE l#st FROM l#ne* ENDMETHOD*

    METHOD l#st_out"ut* s2-ls#n0 ) * SET TITLE$AR GTITG* LOOP AT l#st INTO l#ne* IF l#ne- la9 ) GCG* RITE #con_ l#st-3 co0e_/an0ler? l#st-3l#st_c/an9e FOR ALL INSTANCES*

    :----------------------------------------------------------------

    AT .SER-COMMAND* CALL METHOD status)3user_act#on*

    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  • 8/13/2019 98432616 ABAP Objects

    71/74

    The program creates an object of the class c_l#st and registers the event handler methodco0e_/an0ler of the object for the class event 1utton_cl#c8e0 , and the event handler methodl#st_c/an9e for the event s"ee0_c/an9e of all instances that implement the interface #_ve/#cle .

    /lass an Interface Pools

    This section discusses the structure and special features of class and interface pools for globalclasses.

    Global Classes and Interfaces

    $lasses and interfaces are object types. 5ou can define them either globally in the 'epository or locallyin an * program. If you define classes and interfaces globally, special * programs called class

    pools or interface pools of type < or B serve as containers for the respective classes and interfaces.4ach class or interface pool contains the definition of a single class or interface. The programs areautomatically generated by the $lass *uilder when you create a class or interface.

    class pool is comparable to a module pool or function group. It contains both declarative ande"ecutable * statements, but cannot be started on its own. The runtime system can create runtimeinstances (objects through a re/uest using the CREATE O$%ECT statement. These objects e"ecute

    the statements of the class pool.

    Interface pools do not contain any e"ecutable statements. Instead, they are used as containers forinterface definitions. hen you implement an interface in a class, the interface definition is implicitlyincluded in the class definition.

    Structure of a Class Pool

    $lass pools are structured as follows:

    http://help.sap.com/saphelp_nw2004s/helpdata/en/ec/d9ab291b0b11d295400000e8353423/frameset.htm
  • 8/13/2019 98432616 ABAP Objects

    72/74

    $lass pools contain a definition part for type declarations, and the declaration and implementation partsof the class.

    (ifferences )rom Other !0!P Programs

    $lass pools are different from other * programs for the following reasons:

    ABAP programs such as e/ecutable programs, module pools, or function modules, usuall' ha%e adeclaration part in )hich the global data for the program is defined& This data is %isible in all of the

    processing bloc(s in the program& Class pools, on the other hand, ha%e a definition part in )hich 'ou candefine data and object t'pes, but no data objects or field s'mbols& The t'pes that 'ou define in a class poolare onl %isible in the implementation part of the global class&

    The onl' processing bloc(s that 'ou can use are the declaration part and implementation part of theglobal class& The implementation part ma' onl' implement the methods declared in the global class& .oucannot use an' of the other ABAP processing bloc(s *dialog modules, e%ent bloc(s, subroutines, functionmodules+&

    The processing bloc(s of class pools are not controlled b' the ABAP runtime en%ironment& =o e%entsoccur, and 'ou cannot call an' dialog modules or procedures& Class pools ser%e e/clusi%el' for class

    programming& .ou can onl' access the data and functions of a class using its interface &

    "ince e%ents and dialog modules are not permitted in classes, 'ou cannot process screens in classes&.ou cannot program lists and selection screens in classes, since the' cannot react to the appropriate

  • 8/13/2019 98432616 ABAP Objects

    73/74

    e%ents& #t is intended to ma(e screens a%ailable in classes& #nstead of dialog modules, it )ill be possible tocall methods of the class from the screen flo) logic&

    Local Classes in Class Pools

    The classes and interfaces that you define in the definition part of a class pool are not visible e"ternally.ithin the class pool, they have a similar function to local classes and interfaces in other *

    programs. 6ocal classes can only be instantiated in the methods of the global class. +ince subroutinesare not allowed in class pools, local classes are the only possible modulari7ation unit in global classes.

    6ocal classes have roughly the same function for global classes as subroutines in function groups, butwith the significant e"ception that they are not visible e"ternally.

    OO Transactions

    #n transaction maintenance *"$I7+, 'ou can specif' a transaction code as an OO transaction&

    .ou either lin( the transaction code to the Transaction "er%ice of the ABAP Object "er%ices for persistent objects or to a public method of a