Advanced ABAP Objects Programming

Horst Keller / Stefan Bresch
Business Programming Languages, SAP AG

Overview

Interfaces and Inheritance Visibility and Access Method Call Object Services

Interfaces and Inheritance

Abstract and Final Classes and Methods Composing Interfaces Polymorphism Polymorphic Event Handling

Interfaces and Inheritance – Principles

ABAP Objects supports single inheritance
CLASS c1 DEFINITION. ... CLASS c2 DEFINITION INHERITING FROM c1. ... CLASS c3 DEFINITION INHERITING FROM c1. ...

Classes can implement several interfaces
INTERFACE i1. ... INTERFACE i2. ... CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES: i1, i2. ...

Abstract and Final Classes and Methods

Abstract Classes and Methods
CLASS c1 DEFINTION ABSTRACT. METHODS: m1, m2 ABSTRACT. ENDCLASS. CLASS c1 IMPLEMENTATION. METHODS m1. ENDMETHOD. ENDCLASS.

 Abstract classes can‘t be instantiated  Abstract methods can only be implemented in a subclass, using REDEFINITION.  A class containing an abstract method must itself be abstract.  Compared to interfaces, abstract classes can be partly implemented but are restricted by single inheritance.

 Final classes are endnodes of the inheritance tree. CLASS c2 DEFINITION.  All methods of a final class are implicitly final.  Final classes can‘t have subclasses. ENDCLASS. m2 FINAL. METHODS: m1. ENDCLASS. m2. .  Final methods can‘t be redefined. METHODS: m1.Abstract and Final Classes and Methods Final Classes and Methods CLASS c1 DEFINTION FINAL.

PUBLIC SECTION. .] [ALL METHODS ABSTRACT|FINAL] [DATA VALUES attr = value .  You can assign start values to interface attributes.. .] [FINAL METHODS meth ...... [ABSTRACT METHODS meth .10 New additions for implementation in classes: CLASS class DEFINITION.  You can make interface methods abstract or final.]. INTERFACES intf intf.Interfaces – Implementation 6...

. .. ENDINTERFACE.... . : Component Interfaces .. . i1... i3.. ... i3..  i1: Compound Interface  i2..Composing Interfaces Composing Interfaces INTERFACE INTERFACE i1. ENDINTERFACE. .. . . INTERFACES i2.

INTERFACE i3. ENDINTERFACE. INTERFACES i1. i2.Composing Interfaces – Naming INTERFACE i2.  i3 contains i1 exactly once  The name of i1 in i3 is i3~i1  No nesting of names (i3~i2~i1) allowed . INTERFACES i1. ENDINTERFACE.

METHODS meth. INTERFACES i1. METHODS meth. Problem? . INTERFACE i4. ENDINTERFACE. INTERFACES i1. INTERFACE i3. ENDINTERFACE. i3.Composing Interfaces – Diamond Inheritance INTERFACE i1. ENDINTERFACE. INTERFACES i2. ENDINTERFACE. METHODS meth. INTERFACE i2.

 Each interface component is unique. ENDCLASS. .Composing Interfaces – Implementation No Problem!  Each interface is implemented once... INTERFACES i4. . METHOD i3~meth. METHOD i1~meth.. ENDMETHOD. . . ENDMETHOD. CLASS c1 DEFINITION.. ENDCLASS. METHOD i2~meth. ENDMETHOD. PUBLIC SECTION.. CLASS c1 IMPLEMENTATION.  All interfaces are implemented at the same level..

DATA: iref1 TYPE REF TO i1. INTERFACES i2.  Inside: Access to deep components of compound interfaces via aliases only. iref3->m3( ). iref3->i2~i1~m1( ). . iref3 TYPE REF TO i3.  Outside: Narrowing cast. iref1->m1( ). ALIASES m2 FOR i1~m1. ALIASES m3 FOR i2~m2. INTERFACES i1. METHODS m1.Composing Interfaces – Aliases INTERFACE i1. ENDINTERFACE. FOR i2~i1~m1. ENDINTERFACE. INTERFACE i3. ENDINTERFACE. iref1 = iref3. INTERFACE i2.

 You access objects via reference variables.  What is the technical background? .Polymorphism Polymorphism Accessing different methods in different objects and with different behavior via the same interface.  You can use one and the same reference variable to access various objects.

Polymorphism – Reference Variables  Object: CREATE OBJECT oref TYPE class. oref Typed with a class or an interface  Reference Variable Types: Static Type Dynamic Type from typing class of object . Instance of a class.  Reference Variable: DATA oref TYPE REF TO class|interface.

the dynamic type must be the same class or one of its subclasses.  If the static type is a class.  Different static and dynamic types: Polymorphism .  If the static type is an interface. the dynamic type must implement the interface.Polymorphism – Static and Dynamic Types Golden Rule The static type must be more general than or equal to the dynamic type.

Golden rule cannot be tested statically: Static type of target is more special than static type of source.  Narrowing (Up) Cast 2.Polymorphism – Reference Variable Assignments Two cases for assignments: 1.  Widening (Down) Cast . Golden rule can be tested statically: Static type of target is more general than or equal to static type of source.

 Static type of target is interface and static type of source is class: Target interface is implemented in source class or one of its super classes.Polymorphism – Narrowing Cast Static tests of golden rule:  Static types of target and source are classes: Target class is superclass of or the same as source class. .  Static type of target is class and static type of source is interface: Target class is the root class “object“.  Static types of target and source are interfaces: Target interface is component of or the same as source interface.

. oref1 ?= oref2.. oref2 TYPE REF TO interface.. CREATE OBJECT oref1 TYPE class.Polymorphism – Widening Cast No static tests of golden rule possible:  All cases that cannot be handled by narrowing cast. CATCH cx_sy_move_cast_error.). ... TRY. DATA: oref1 TYPE REF TO class... . . . ENDTRY. ?TO ..  No other assignments possible than with casting operator ?= (MOVE ...

class . EVENTS evt Inheritance case: METHODS handler FOR EVENT evt OF class.Polymorphic Event Handling Principle The event handler defines the objects that can trigger the event handler method.

.. ENDCLASS. PUBLIC SECTION. CLASS c3 DEFINITION. c1. DATA trigger TYPE REF TO c2.10 Strict type check for event handler during registration. CLASS c1 DEFINITION. PUBLIC SECTION. CLASS c2 DEFINITION INHERITING FROM c1. EVENTS e1.Polymorphic Event Handling – Type Check 6. ENDCLASS. ENDCLASS. CLASS-METHODS handler c2. Object type behind FOR EVENT OF in declaration of handler must be more general or the same as static type of trigger.. SET HANDLER c3=>handler FOR trigger. FOR EVENT e1 OF c1. .

10 New data type of sender. PUBLIC SECTION. Before release 6. ENDCLASS..10 the type was defined by the class or interface where the event was declared. Data type of the implicit event parameter sender is the object type class or interface behind FOR EVENT OF. CLASS c_handler DEFINITION.. [CLASS-]METHODS handler FOR EVENT evt OF class|interface IMPORTING sender .Polymorphic Event Handling – Type of Sender 6. .

Visibility and Access Read-Only Attributes Attributes in Internal Tables Dynamic Access Restricted Instantiation Friends .

METHODS get_a1 RETURNING r1 . ENDMETHOD. CLASS c1 IMPLENTATION. PRIVATE SECTION.. . Text book style . DATA a1 TYPE .. READ-ONLY.... METHOD get_a1.. ENDCLASS. ENDCLASS.Visibility – Read-Only Attributes CLASS c1 DEFINITION.. ENDCLASS. but performance? CLASS c1 DEFINITION. DATA a1 TYPE . PUBLIC SECTION. r1 = a1. PUBLIC SECTION. ....

DATA itab LIKE TABLE OF itab_line. idx TYPE i.... READ TABLE itab INTO itab_line WITH KEY oref->attr = .....Access – Attributes in Internal Table Operations Did you know? DATA: BEGIN OF itab_line. LOOP AT itab INTO itab_line WHERE oref->attr > 1. . . END OF itab_line.. MODIFY itab FROM itab_line TRANSPORTING oref WHERE oref->attr = . DELETE itab WHERE oref->attr = . SORT itab BY oref->attr . oref TYPE REF TO class|interface... ENDLOOP.

attr = 'ATTR'. . FIELD-SYMBOLS <fs> TYPE ANY. ASSIGN (attr) TO <fs>.10 Principle: Access to all visible components  Static type more general than dynamic type. ASSIGN oref->(attr) TO <fs>.  Static access only to statically known components. create object oref type class.Access – Dynamic Access 6.  Dynamic Access DATA oref TYPE REF TO object. Check by sy-subrc or IS ASSIGNED . DATA attr TYPE string. attr = 'OREF->ATTR'.

CREATE PUBLIC|PROTECTED|PRIVATE. But: Subclasses of superclass with addition CREATE PRIVATE cannot be instantiated (exception: FRIENDS) They always inherit the implicit addition CREATE NONE.Access – Restricted Instantiation Visibility of Instance Constructor CLASS class DEFINITION DEFINITION.. PUBLIC SECTION. .. Classes with addition CREATE PRIVATE should be FINAL.  Declaration of constructor in public section.  ??? METHODS constructor .  Constructor is called by CREATE OBJECT statement  Constructor visibility set implicitly by CREATE addition.  Subclasses inherit constructor visibility or override it.

Access – Friends 6. ... FRIENDS classes interfaces . PROTECTED|PRIVATE SECTION..  Offering friendship is not inherited by subclasses.  Subclasses of friends and all classes/interfaces that implement a friend interface are also friends..  A class offering friendship is not automatically friend of its friends.10 Giving up Protection and Privacy CLASS class DEFINITION CREATE PROTECTED|PRIVATE CLASS class DEFINITION CREATE PROTECTED|PRIVATE.  Friends can allways instantiate a class... PROTECTED|PRIVATE SECTION.  Friends have access to all components of a class. .  A class can offer friendship to other classes or interfaces. .

Method Call Functional Methods Short Form for Static Invoke Dynamic Invoke Invoke via OO-Transaction .

meth( ) > f. METHODS meth IMPORTING .Method Call – Functional Methods Methods with one RETURNING PARAMETER. . meth( a ). f = meth( ). pi .. Short forms can be used in operand positions. . CASE meth( ).. RETURNING VALUE(r) TYPE . WHEN meth( ). [COMPUTE] r = f + meth( )..10 Built-in functions also in above operand positions. ).. meth( pi = ai ... meth( ).... 6..

.. ).... ). pj = aj . * Examples transaction_manager->start( ). When the passing of parameters is done in parenthesis.. CALL METHOD meth( pi = ai . pi = ai .. . meth( a ).. html_viewer->show_url( url ). ). IMPORTING pjIMPORTING = aj . ).6 CALL METHOD optional in Static Invoke CALL METHOD methpiEXPORTING meth( meth( EXPORTING = ai ... meth( pi = ai .10 4. meth( ). the keyword CALL METHOD is not necessary . CALL METHOD meth( )... CALL METHOD meth( a ).Method Call – Short Form for Static Invoke 6.

10 PARAMETER-TABLE also in CALL FUNCTION.Method Call – Dynamic Invoke Dynamic Passing of Parameters TYPE-POOLS abap.. DATA: ptab TYPE abap_parmbind_tab. . ENDTRY. ptab_line-name = 'P'. 6. CATCH cx_sy_dyn_call_error. TRY. GET REFERENCE OF a INTO ptab_line-value.. meth = 'METH'. CALL METHOD oref->(meth) PARAMETER-TABLE ptab. INSERT ptab_line INTO TABLE ptab. . ptab_line LIKE LINE OF ptab.

. ENDCLASS. Program . METHOD instance_method. ENDMETHOD. CLASS demo_class DEFINITION. .10 Transaction Code Object Services Global or Local PROGRAM demo_oo_transaction. PUBLIC SECTION. Calling the transaction loads the program into an own internal mode. METHODS instance_method. ENDCLASS.Method Call – OO-Transaction 6. instantiates the class and executes the method. CLASS demo_class IMPLEMENTATION..

10 Introduction Transparent Object Persistence Handling persistent Objects Additional transaction service .Object Services 6.

Database .Introduction ABAP program 6..10 Persistent objects Object Services System and classspecific agents ABAP runtime environment  Object services are languagerelated services that are not part of the language itself.Object Services .  are realized in the Class Library in classes CL_OS_.  provide a level of abstraction between ABAP programs and the runtime environment..  support object-oriented handling of persistent data and transactions. ... and interfaces IF_OS_.

Object Services .10 Persistence Service Persistent Classes Object-relational Mapping Persistence Representation .Transparent Object Persistence 6.

The persistence of your objects is managed by the persistence service.  The persistence service stores your (changed) objects.  The persistence service tracks the changes made to your objects.Object Services .  The persistence service loads your objects.Persistence Service 6.10 Object Services offers you a transparent object persistence. .

Object Services . you can create a persistent class.10 The persistence service handles instances of persistent classes. With the Class Builder. .Persistent Classes 6.

. Lufthansa .. CARRID = LH CARRNAME = Lufthansa ...Object-Relational Mapping 6. . . SCARR CARRID CARRNAME.... CL_CARRIER CARRID CARRNAME .....10 Mapping classes to tables and objects to table rows is called the object-relational mapping.Object Services . LH ..

 Here.Object Services .10  For a persistent class. persistent attributes can be created. the object-relational mapping can be defined within the Class Builder.  The „persistence representation“ tool can be accessed using the button Persistence .Persistence Representation 6. starting with fields of an existing table. .

Object Services . Object Identity Persistent Object References . .10 Accessor Methods Life Cycle Management Methods Class Agents Loading. Deleting.Handling Persistent Objects 6.. Creating..

 The accessor methods inform the persistence service of attribute access.  The attribute A of a persistent class can only be accessed with the methods GET_A and SET_A. .Object Services .Accessor Methods 6.10  The Class Builder generates accessor methods for each attribute of a persistent class.

Object Services . A persistent instance has one of the following life cycle states: NEW.  Methods for creating persistent instances. you can handle the life cycle of persistent instances.  Methods for deleting persistent instances. LOADED. DELETED. . CHANGED.10 With the life cycle management methods.Life Cycle Management Methods 6. NOT_LOADED.  Methods for loading persistent instances.

 The class agent is generated by the Class Builder. .  The class agent is a singleton and has a class attribute AGENT containing an object reference to this singleton.  The class agent for the persistent class CL_X is named CA_X.Object Services .Class Agents 6.10  The life cycle management methods are provided by the class agent.

Object Services . CARRIER = CARRIER_AGENT->GET_PERSISTENT( I_CARRID = 'LH' ). CATCH CX_OS_OBJECT_NOT_FOUND. . CARRNAME.Loading a Persistent Object 6. CARRNAME TYPE S_CARRNAME. TRY. DATA: CARRIER TYPE REF TO CL_CARRIER. CARRNAME = CARRIER->GET_CARRNAME( ). ENDTRY. WRITE: 'LH: '. CARRIER_AGENT = CA_CARRIER=>AGENT.10 A persistent object can be loaded with the class agent method GET_PERSISTENT. CARRIER_AGENT TYPE REF TO CA_CARRIER.

CARRIER = CARRIER_AGENT->CREATE_PERSISTENT( I_CARRID = 'LH' ).Creating a Persistent Object 6. TRY. CARRIER->SET_CARRNAME('Lufthansa' ). DATA: CARRIER TYPE REF TO CL_CARRIER. ENDTRY. CARRIER_AGENT = CA_CARRIER=>AGENT. COMMIT WORK. .10 A persistent object can be created with the class agent method CREATE_PERSISTENT. CATCH CX_OS_OBJECT_EXISTING.Object Services . CARRIER_AGENT TYPE REF TO CA_CARRIER.

ENDTRY. CATCH CX_OS_OBJECT_NOT_EXISTING. CARRIER_AGENT->DELETE_PERSISTENT( I_CARRID = 'LH' ).Object Services .Deleting a Persistent Object 6. COMMIT WORK. TRY.10 A persistent object can be deleted with the class agent method DELETE_PERSISTENT. DATA: CARRIER_AGENT TYPE REF TO CA_CARRIER. CARRIER_AGENT = CA_CARRIER=>AGENT. .

Object Services – Object Identity 6. (There‘s a GUID related to each persistent object.) . but the primary key field of the table mapped to this persistent class. (The business keys are the attributes mapped to the primary key fields of the table mapped to this persistent class.10 Object identity business key: The object identity is controlled by the application. The GUID is not an attribute of the persistent class.) Object identity GUID: The object Identity is controlled by the persistence service.

The persistent service supports transparent navigation.Object Services – Persistent Object References 6. A referenced object is not loaded until it is accessed. Runtime references are converted to persistent references during database access (and vice versa). .10 A persistent attribute can be a reference to another persistent object with object identity GUID.

Object Services – Transaction Service 6.10 Transaction service Transactions Nested transactions Transactions and the SAP-LUW .

Inside an OO-transaction (with OO transaction model checked) you must use the transaction service with the specified update mode.10 Object Services offers you an additional transaction service. .Object Services – Transaction Service 6.

10 Transactions are managed by the transaction manager (singleton created by the runtime system).Object Services – Transactions 6. DATA: TM TYPE IF_OS_TRANSACTION_MANAGER. * Inside the transaction T->END( ). T = TM->CREATE_TRANSACTION( ). A transaction is represented by a transaction object. DATA: T TYPE IF_OS_TRANSACTION. T->START( ). A transaction is started by the START and completed by the END method (UNDO rolls the transaction back). . TM = CL_OS_SYSTEM=>GET_TRANSACTION_MANAGER( ).

 If UNDO is called for a subtransaction.Object Services – Nested Transactions 6.  If END is called for a subtransaction . the before-image of the changed instances is discarded.  Within a transaction.  The top-level transaction is the transaction that is active and not a subtransaction. the state of the instances is restored in memory. a subtransaction can be started. .10  Object Services support nested transactions.

 . the transaction services executes COMMIT WORK implicitly.  A legacy application (standard ABAP application) calls object services components (components using object services persistence service). At top-level transaction completion (using the method END). The legacy application executes COMMIT WORK. There are two scenarios for this coupling.Object Services – Transactions and the SAP-LUW 6.10 The object services transactions are tightly coupled with the SAP-LUW concept. An object services application (application using object services persistence and transaction service) calls legacy components (standard ABAP components).

Sign up to vote on this title
UsefulNot useful