Advanced
Download
1 / 53

Advanced ABAP Objects Programming - PowerPoint PPT Presentation


  • 133 Views
  • Uploaded on

Advanced ABAP Objects Programming. Horst Keller / Stefan Bresch Business Programming Languages, S AP AG. Overview. Interfaces and Inheritance Visibility and Access Method Call Object Services. Interfaces and Inheritance. Abstract and Final Classes and Methods.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Advanced ABAP Objects Programming' - bran


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Advanced ABAP Objects Programming

Horst Keller / Stefan Bresch Business Programming Languages, SAP AG


Overview
Overview

Interfaces and Inheritance

Visibility and Access

Method Call

Object Services


Interfaces and inheritance
Interfaces and Inheritance

Abstract and Final Classes and Methods

Composing Interfaces

Polymorphism

Polymorphic Event Handling


Interfaces and inheritance principles
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 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.


Abstract and final classes and methods1
Abstract and Final Classes and Methods

Final Classes and Methods

CLASS c1 DEFINTION FINAL. METHODS: m1, m2.ENDCLASS.

CLASS c2 DEFINITION. METHODS: m1, m2 FINAL.ENDCLASS.

  • Final classes can‘t have subclasses.

  • Final methods can‘t be redefined.

  • All methods of a final classare implicitly final.

  • Final classes are endnodes of the inheritance tree.


Interfaces implementation

6.10

Interfaces – Implementation

Newadditions for implementation in classes:

CLASS class DEFINITION.

PUBLIC SECTION.INTERFACES intf [ABSTRACT METHODSmeth ...] [FINAL METHODSmeth ...] [ALL METHODS ABSTRACT|FINAL] [DATA VALUESattr = value ...]. ...

CLASS class DEFINITION.

PUBLIC SECTION.INTERFACES intf. ...

  • You can make interface methodsabstract or final.

  • You can assign start values to interface attributes.


Composing interfaces
Composing Interfaces

Composing Interfaces

INTERFACE i1. ...INTERFACES i2, i3, ... ...ENDINTERFACE.

INTERFACE i1. ... ... ...ENDINTERFACE.

  • i1: Compound Interface

  • i2, i3, ... : Component Interfaces


Composing interfaces naming
Composing Interfaces – Naming

INTERFACE i2. INTERFACES i1.ENDINTERFACE.

INTERFACE i3. INTERFACES i1, i2.ENDINTERFACE.

  • i3 contains i1 exactly once

  • The name of i1 in i3 is i3~i1

  • No nesting of names (i3~i2~i1) allowed


Composing interfaces diamond inheritance
Composing Interfaces – Diamond Inheritance

INTERFACE i1. METHODS meth.ENDINTERFACE.

INTERFACE i2. INTERFACES i1. METHODS meth.ENDINTERFACE.

INTERFACE i3. INTERFACES i1. METHODS meth.ENDINTERFACE.

INTERFACE i4. INTERFACES i2, i3.ENDINTERFACE.

Problem?


Composing interfaces implementation
Composing Interfaces – Implementation

CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES i4.ENDCLASS.

No Problem!

  • Each interface is implemented once.

  • All interfaces are implemented at the same level.

  • Each interface component is unique.

CLASS c1 IMPLEMENTATION. METHOD i1~meth. ... ENDMETHOD. METHOD i2~meth. ... ENDMETHOD. METHOD i3~meth. ... ENDMETHOD.ENDCLASS.


Composing interfaces aliases

FOR i2~i1~m1.

iref3->i2~i1~m1( ).

Composing Interfaces – Aliases

INTERFACE i1. METHODS m1.ENDINTERFACE.

  • Inside: Access to deep components of compound interfaces via aliases only.

INTERFACE i2. INTERFACES i1.ALIASESm2 FOR i1~m1.ENDINTERFACE.

  • Outside:Narrowing cast.

INTERFACE i3. INTERFACES i2.ALIASESm3 FOR i2~m2.ENDINTERFACE.

DATA: iref1 TYPE REF TO i1, iref3 TYPE REF TO i3.

iref1 = iref3.iref1->m1( ).iref3->m3( ).


Polymorphism
Polymorphism

Polymorphism

Accessing different methods in different objects and with different behavior via the same interface.

  • You access objects via reference variables.

  • You can use one and the same reference variable to access various objects.

  • What is the technical background?


Polymorphism reference variables
Polymorphism – Reference Variables

  • Object:Instance of a class.

  • Reference Variable:Typed with a class or an interface

  •  Reference Variable Types:

CREATE OBJECT oref TYPE class.

DATA oref TYPE REF TO class|interface.

oref

Static Type - from typingDynamic Type - class of object


Polymorphism static and dynamic types
Polymorphism – Static and Dynamic Types

Golden Rule

The static type must be more general than or equal to the dynamic type.

  • If the static type is an interface, the dynamic type must implement the interface.

  • If the static type is a class, the dynamic type must be the same class or one of its subclasses.

  •  Different static and dynamic types:Polymorphism


Polymorphism reference variable assignments
Polymorphism – Reference Variable Assignments

Two cases for assignments:

Golden rule can be tested statically: Narrowing (Up) Cast

Golden rule cannot be tested statically: Widening (Down) Cast

Static type of target is more general than or equal to static type of source.

Static type of target is more special than static type of source.


Polymorphism narrowing cast
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 types of target and source are interfaces:Target interface is component of or the same as source interface.

  • 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.

  • Static type of target is class and static type of source is interface:Target class is the root class “object“.


Polymorphism widening cast
Polymorphism – Widening Cast

No static tests of golden rule possible:

  • All cases that cannot be handled by narrowing cast.

  • No other assignments possible than with casting operator ?= (MOVE ... ?TO ...).

DATA: oref1 TYPE REF TO class,

oref2 TYPE REF TO interface.

...

CREATE OBJECT oref1 TYPE class.

...

TRY.

oref1 ?= oref2.

CATCH cx_sy_move_cast_error.

...

ENDTRY.


Polymorphic event handling

EVENTS evt

class

Polymorphic Event Handling

Principle

The event handler defines the objects that can trigger the event handler method.

Inheritance case:

METHODS handlerFOR EVENTevt OF class.


Polymorphic event handling type check

6.10

Polymorphic Event Handling – Type Check

Strict type check for event handler during registration.

Object type behind FOR EVENT OF in declaration ofhandler must be more general or the same as static type of trigger.

CLASS c1 DEFINITION. PUBLIC SECTION. EVENTS e1.ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1. ...ENDCLASS.

CLASS c3 DEFINITION. PUBLIC SECTION. CLASS-METHODS handlerFOR EVENTe1 OF c2.ENDCLASS.

CLASS c3 DEFINITION. PUBLIC SECTION. CLASS-METHODS handlerFOR EVENTe1 OF c1.ENDCLASS.

DATA trigger TYPE REF TO c1.

SET HANDLER c3=>handler FOR trigger.

DATA trigger TYPE REF TO c2.

SET HANDLER c3=>handler FOR trigger.


Polymorphic event handling type of sender

6.10

Polymorphic Event Handling – Type of Sender

New data type of sender.

Data type of the implicit event parameter sender is the object type class or interface behind FOR EVENT OF.

Before release 6.10 the type was defined by the class or interface where the event was declared.

CLASS c_handler DEFINITION. PUBLIC SECTION. [CLASS-]METHODS handler FOR EVENTevt OF class|interfaceIMPORTING sender ...ENDCLASS.


Visibility and access
Visibility and Access

Read-Only Attributes

Attributes in Internal Tables

Dynamic Access

Restricted Instantiation

Friends


Visibility read only attributes

CLASS c1 DEFINITION. PUBLIC SECTION. DATA a1 TYPE ... READ-ONLY. ...ENDCLASS.

Visibility – Read-Only Attributes

CLASS c1 DEFINITION. PUBLIC SECTION. METHODS get_a1 RETURNING r1 ... PRIVATE SECTION. DATA a1 TYPE ...ENDCLASS.

CLASS c1 IMPLENTATION. METHOD get_a1.r1 = a1. ENDMETHOD. ENDCLASS.

Text book style ...

Text book style ...but performance?


Access attributes in internal table operations
Access – Attributes in Internal Table Operations

Did you know?

DATA: BEGIN OF itab_line, idx TYPE i, orefTYPE REF TOclass|interface, END OF itab_line.

DATA itab LIKE TABLE OF itab_line.

SORT itab BY oref->attr ...

DELETE itab WHERE oref->attr = ...

READ TABLE itab INTO itab_line WITH KEY oref->attr = ...

MODIFY itab FROM itab_line TRANSPORTING oref WHERE oref->attr = ...

LOOP AT itab INTO itab_line WHERE oref->attr > 1. ...ENDLOOP.


Access dynamic access

6.10

Access – Dynamic Access

  • Principle: Access to all visible components

  • Static type more general than dynamic type.

  • Static access only to statically known components.

  • Dynamic Access

DATA oref TYPE REF TO object.FIELD-SYMBOLS <fs> TYPE ANY.DATA attr TYPE string.

create object oref type class.

attr = 'ATTR'.ASSIGN oref->(attr) TO <fs>.

attr = 'OREF->ATTR'.ASSIGN (attr) TO <fs>.

Check by sy-subrcor IS ASSIGNED .


Access restricted instantiation
Access – Restricted Instantiation

Visibility of Instance Constructor

CLASS class DEFINITION. PUBLIC SECTION.  ??? METHODS constructor ...

CLASS class DEFINITION CREATE PUBLIC|PROTECTED|PRIVATE. PUBLIC SECTION. METHODS constructor ...

  • Declaration of constructor in public section.

  • Constructor is called by CREATE OBJECT statement

  • Constructor visibility set implicitly by CREATE addition.

  • Subclasses inherit constructor visibility or override it.

  • But: Subclasses of superclass with addition CREATE PRIVATE cannot be instantiated (exception: FRIENDS)They always inherit the implicit addition CREATE NONE.Classes with addition CREATE PRIVATE should be FINAL.


Access friends

6.10

Access – Friends

Giving upProtection and Privacy

CLASS class DEFINITION CREATE PROTECTED|PRIVATEFRIENDS classes interfaces ... ...PROTECTED|PRIVATE SECTION.

CLASS class DEFINITION CREATE PROTECTED|PRIVATE. ...PROTECTED|PRIVATE SECTION.

  • A class can offer friendship to other classes or interfaces.

  • Subclasses of friends and all classes/interfaces that implement a friend interface are also friends.

  • Friends have access to all components of a class.

  • Friends can allways instantiate a class.

  • A class offering friendship is not automatically friend of its friends.

  • Offering friendship is not inherited by subclasses.


Method call
Method Call

Functional Methods

Short Form for Static Invoke

Dynamic Invoke

Invoke via OO-Transaction


Method call functional methods
Method Call – Functional Methods

Methods with one RETURNING PARAMETER.

METHODS meth IMPORTING ... pi ...RETURNING VALUE(r) TYPE ...

Short forms can be used in operand positions.

meth( ).

meth( a ).

meth( pi = ai ... ).

f =meth( ).

[COMPUTE] r = f + meth( ).

... meth( ) > f.

CASEmeth( ).WHENmeth( ).

6.10

Built-in functions also in above operand positions.


Method call short form for static invoke

4.6

6.10

Method Call – Short Form for Static Invoke

CALL METHODoptional inStatic Invoke

CALL METHOD meth EXPORTING pi = ai ... IMPORTING pj = aj ...

CALL METHOD meth().

CALL METHOD meth( a ).

CALL METHOD meth( pi = ai ... ).

CALL METHOD meth( EXPORTING pi = ai ... IMPORTING pj = aj ... ).

CALL METHOD meth().

CALL METHOD meth( a ).

CALL METHOD meth( pi = ai ... ).

meth( EXPORTING pi = ai ... IMPORTING pj = aj ... ).

meth( ).

meth( a ).

meth( pi = ai ... ).

When the passing of parameters is done in parenthesis, the keyword CALL METHOD is not necessary .

* Examples

transaction_manager->start( ).

html_viewer->show_url( url ).


Method call dynamic invoke
Method Call – Dynamic Invoke

Dynamic Passing of Parameters

TYPE-POOLS abap.

DATA: ptab TYPE abap_parmbind_tab, ptab_line LIKE LINE OF ptab.

ptab_line-name = 'P'.GET REFERENCE OF a INTO ptab_line-value.INSERT ptab_line INTO TABLE ptab.

TRY. meth = 'METH'. CALL METHOD oref->(meth) PARAMETER-TABLE ptab. CATCH cx_sy_dyn_call_error. ...ENDTRY.

6.10

PARAMETER-TABLE also in CALL FUNCTION.


Method call oo transaction

6.10

Object Services

Global or Local

Method Call – OO-Transaction

Transaction Code

PROGRAM demo_oo_transaction.

CLASS demo_class DEFINITION. PUBLIC SECTION. METHODS instance_method.ENDCLASS.

CLASS demo_class IMPLEMENTATION. METHOD instance_method. ... ENDMETHOD.ENDCLASS.

Calling the transactionloads the program into an own internal mode, instantiates the class and executes the method.

Program


Object services

6.10

Object Services

Introduction

Transparent Object Persistence

Handling persistent Objects

Additional transaction service


Object services introduction

6.10

ABAP program

Persistent

objects

Object Services

System

and class-

specificagents

ABAP runtime environment

Database

Object Services - Introduction

  • Object services are language-related services that are not part of the language itself,

  • provide a level of abstraction between ABAP programs and the runtime environment,

  • support object-oriented handling of persistent data and transactions,

  • are realized in the Class Library in classes CL_OS_... and interfaces IF_OS_... .


Object services transparent object persistence
Object Services - Transparent Object Persistence

6.10

Persistence Service

Persistent Classes

Object-relational Mapping

Persistence Representation


Object services persistence service

6.10

Object Services - Persistence Service

  • Object Services offers you a transparent object persistence.

  • The persistence of your objects is managed by the persistence service.

    • The persistence service loads your objects.

    • The persistence service tracks the changes made to your objects.

    • The persistence service stores your (changed) objects.


Object services persistent classes

6.10

Object Services - Persistent Classes

The persistence service handles instances of persistent classes. With the Class Builder, you can create a persistent class.


Object services object relational mapping

6.10

CL_CARRIER

CARRID

CARRNAME

...

SCARR

CARRID CARRNAME...

...

LH Lufthansa ...

...

CARRID = LH

CARRNAME = Lufthansa

...

Object Services - Object-Relational Mapping

Mapping classes to tables and objects to table rows is called the object-relational mapping.


Object services persistence representation

6.10

Persistence

Object Services - Persistence Representation

  • For a persistent class, the object-relational mapping can be defined within the Class Builder.

  • The „persistence representation“ tool can be accessed using the button .

  • Here, starting with fields of an existing table, persistent attributes can be created.


Object services handling persistent objects

6.10

Object Services - Handling Persistent Objects

Accessor Methods

Life Cycle Management Methods

Class Agents

Loading, Creating, Deleting, ...

Object Identity

Persistent Object References


Object services accessor methods

6.10

Object Services - Accessor Methods

  • The Class Builder generates accessor methods for each attribute of a persistent class.

  • The attribute Aof a persistent class can only be accessed with the methods GET_A and SET_A.

  • The accessor methods inform the persistence service of attribute access.


Object services life cycle management methods
Object Services - Life Cycle Management Methods

6.10

  • With the life cycle management methods, you can handle the life cycle of persistent instances.

    • Methods for creating persistent instances.

    • Methods for loading persistent instances.

    • Methods for deleting persistent instances.

  • A persistent instance has one of the following life cycle states: NEW, LOADED, CHANGED, DELETED, NOT_LOADED.


Object services class agents

6.10

Object Services - Class Agents

  • The life cycle management methods are provided by the class agent.

  • The class agent is generated by the Class Builder.

  • The class agent for the persistent class CL_X is named CA_X.

  • The class agent is a singleton and has a class attribute AGENT containing an object reference to this singleton.


Object services loading a persistent object

6.10

Object Services - Loading a Persistent Object

A persistent object can be loaded with the class agent method GET_PERSISTENT.

DATA: CARRIER TYPE REF TO CL_CARRIER,

CARRIER_AGENT TYPE REF TO CA_CARRIER,

CARRNAME TYPE S_CARRNAME.

CARRIER_AGENT = CA_CARRIER=>AGENT.

TRY.

CARRIER = CARRIER_AGENT->GET_PERSISTENT(

I_CARRID = 'LH' ).

CARRNAME = CARRIER->GET_CARRNAME( ).

WRITE: 'LH: ', CARRNAME.

CATCH CX_OS_OBJECT_NOT_FOUND.

ENDTRY.


Object services creating a persistent object

6.10

Object Services - Creating a Persistent Object

A persistent object can be created with the class agent method CREATE_PERSISTENT.

DATA: CARRIER TYPE REF TO CL_CARRIER,

CARRIER_AGENT TYPE REF TO CA_CARRIER.

CARRIER_AGENT = CA_CARRIER=>AGENT.

TRY.

CARRIER = CARRIER_AGENT->CREATE_PERSISTENT(

I_CARRID = 'LH' ).

CARRIER->SET_CARRNAME('Lufthansa' ).

CATCH CX_OS_OBJECT_EXISTING.

ENDTRY.

COMMIT WORK.


Object services deleting a persistent object

6.10

Object Services - Deleting a Persistent Object

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.

TRY.

CARRIER_AGENT->DELETE_PERSISTENT(

I_CARRID = 'LH' ).

CATCH CX_OS_OBJECT_NOT_EXISTING.

ENDTRY.

COMMIT WORK.


Object services object identity

6.10

Object Services – Object Identity

Object identity business key:The object identity is controlled by the application. (The business keys are the attributes mapped to the primary key fields of the table mapped to this persistent class.)

Object identity GUID:The object Identity is controlled by the persistence service. (There‘s a GUID related to each persistent object. The GUID is not an attribute of the persistent class, but the primary key field of the table mapped to this persistent class.)


Object services persistent object references

6.10

Object Services – Persistent Object References

A persistent attribute can be a reference to another persistent object with object identity GUID.

Runtime references are converted to persistent references during database access (and vice versa).

The persistent service supports transparent navigation.A referenced object is not loaded until it is accessed.


Object services transaction service

6.10

Object Services – Transaction Service

Transaction service

Transactions

Nested transactions

Transactions and the SAP-LUW


Object services transaction service1

6.10

Object Services – Transaction Service

Object Services offers you an additional transaction service.

Inside an OO-transaction (with OO transaction model checked) you must use the transaction service with the specified update mode.


Object services transactions

6.10

Object Services – Transactions

Transactions are managed by the transaction manager (singleton created by the runtime system).

A transaction is represented by a transaction object.

A transaction is started by the START and completed by the END method (UNDO rolls the transaction back).

DATA: TM TYPE IF_OS_TRANSACTION_MANAGER.

DATA: T TYPE IF_OS_TRANSACTION.

TM = CL_OS_SYSTEM=>GET_TRANSACTION_MANAGER( ).

T = TM->CREATE_TRANSACTION( ).

T->START( ).

* Inside the transaction

T->END( ).


Object services nested transactions

6.10

Object Services – Nested Transactions

  • Object Services support nested transactions.

  • Within a transaction, a subtransaction can be started.

  • The top-level transaction is the transaction that is active and not a subtransaction.

  • If UNDO is called for a subtransaction, the state of the instances is restored in memory.

  • If END is called for a subtransaction , the before-image of the changed instances is discarded.


Object services transactions and the sap luw

6.10

Object Services – Transactions and the SAP-LUW

  • The object services transactions are tightly coupled with the SAP-LUW concept. There are two scenarios for this coupling.

    • A legacy application (standard ABAP application) calls object services components (components using object services persistence service). The legacy application executes COMMIT WORK.

    • An object services application (application using object services persistence and transaction service) calls legacy components (standard ABAP components). At top-level transaction completion (using the method END), the transaction services executes COMMIT WORK implicitly.


ad