Вы находитесь на странице: 1из 68

Date

Global network of innovation

ABAP OBJECTS

Copyright Siemens Business Services

Global network of innovation

Agenda
Day 1 Introduction to OOPS OOPS Principles ABAP Objects Inheritance Exercise Programs Day 2 Event Handling Global classes Advanced Concepts Exercise Programs

Global network of innovation

Introduction

Contents:
Procedural programming Object-oriented programming Aims of the ABAP Objects programming language

Global network of innovation

History of Programming Languages


Machine language Assembler

C++

ABAP

Java ABAP Objects

Global network of innovation

Introduction : Overview

Procedural Programming Object-Oriented Programming

Global network of innovation

Procedural Programming
Functions are defined independently of data structures Direct access to data

Data

Data

Data

Data

Data

Function Function

Function Function

Function Function

Function Function

Function Function Function Function Function Function Function Function

Global network of innovation

Structure of ABAP Program


TYPES: ... DATA: ... ... PERFORM f1 ... CALL FUNCTION ... ...

Data declaration

Main program Call subroutines Call function modules Define subroutines

FORM f1 ... ... ENDFORM.

Global network of innovation

Working with Function Groups


ABAP Program Function groups

... CALL FUNCTION A2 ...

Function group A Function module A1 Function module A2 Data

...

Function module A3

CALL FUNCTION B1 ... ...

Function group B Function module B1 Data Function module B2

Global network of innovation

Function Group as Counter : Example


FUNCTION-POOL counter. DATA: count TYPE I. FUNCTION SET_COUNTER. * Local interface IMPORTING VALUE(set_value) count = set_value ENDFUNCTION. FUNCTION INCREMENT_COUNTER. ADD 1 TO count. ENDFUNCTION. FUNCTION GET_COUNTER. * Local interface EXPORTING VALUE(get_value) get_value = count. ENDFUNCTION.

Global network of innovation

Function Group as Counter : Example


DATA: number TYPE I VALUE 3.

CALL FUNCTION SET_COUNTER

EXPORTING set_value = number.

DO 4 TIMES. CALL FUNCTION INCREMENT_COUNTER. ENDDO.

CALL FUNCTION GET_COUNTER

IMPORTING get_value = number.

WRITE: ..., number, ...

Global network of innovation

Several Instances of Function Group?

1 counter

Any number of counters

Function group COUNTER SET_COUNTER INCREMENT_COUNTER GET_COUNTER COUNTER

Global network of innovation

Introduction : Overview

Procedural Programming

Object-Oriented Programming

Date

Global network of innovation

Why OOPs

Copyright Siemens Business Services

Global network of innovation

Overall Aims of Software Development


Correctness Robustness

Quality of a software product

Extensibility

Re-usability

Date

Global network of innovation

OBJECTS

Copyright Siemens Business Services

Global network of innovation

What are Objects?


Model Real world
Tree
Method Method Method Method Method Method

House

Data

Data

Crane
Method Method Method

Data

Boat

Data

Method Method Method

Objects are an abstraction of the real world Objects are units made up of data and of the functions belonging to that data

Global network of innovation

Object-Oriented Programming Model


Classes Classes describe objects. From a technical point of view, objects are runtime instances of a class. In other words Class is nothing but a template which carries DATA and METHODS to access the DATA Objects Objects are instances of classes. They contain data and provides services. The data forms the attributes of the object. The services are known as methods (also known as operations or functions)

Global network of innovation

Strengths of Object-Oriented Approach


The following aims are better supported:

Extensibility through

Polymorphism Inheritance

Extensibility

Re-usability through

Classes Encapsulation Inheritance

Re-usability

Global network of innovation

Strengths of Object-Oriented Approach


Uniform language throughout the development process

All participants In all phases

Reality is reflected in appropriate software concepts


Objects -> objects Their state -> attributes Their functions -> methods

Global network of innovation

Weaknesses of Object-Oriented Approach

Longer development phase before first results ready Paradigm break between object-oriented programs and Procedural Programming

Global network of innovation

Outlook

OO application

OO application BOR

Class library ABAP Objects

Global network of innovation

ABAP Objects : Design Aims


As simple as possible Only object-oriented concepts, that have proved themselves in other object-oriented programming languages More frequent use of type checks

Global network of innovation

ABAP Objects
True, compatible extension of ABAP ABAP Objects statements can be used in conventional ABAP programs ABAP statements can be used in ABAP Objects programs

* ABAP Program CLASS lcl _airplane DEFINITION. ... ENDCLASS. ... TYPES: ... DATA: ... ...

* ABAP Objects Program DATA: counter TYPE i. ... CREATE OBJECT ... counter = counter + 1. ...

Date

Global network of innovation

Questions ?

Copyright Siemens Business Services

Global network of innovation

OOPS Principles

Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Principles : Overview-1

Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Object-1
Example: airplane

Attributes Attributes Plane ID: ID0057231 Weight: 30,000 kg Length: 70 m Methods Events Events landed Name: LH Berlin Methods land fly

Private access Encapsulation As a rule, attributes

Public access Interface As a rule,methods, events

Global network of innovation

Principles : Overview-2
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Class : Blueprint of Object


CLASS < classname > DEFINITION. ENDCLASS.

Definition part The class components (for example, attributes and methods) are defined in this part. Implementation part This part only contains the method implementations.

CLASS < classname > IMPLEMENTATION. ENDCLASS.

Global network of innovation

Important Components in a Class


1. Attributes or Data Determine the state of the object 2. Methods Determine the Behavior of the Object Used to access the Attribute 3. Events Determines the Actions you can perform

Global network of innovation

Principles : Overview-3
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Attributes
Attribute types can have any kind of data type:

Elementary types:

C, I, P, STRING TYPE REF TO (References to objects/interfaces)

Define your own types

lcl_airplane name: LH weight: 30 000 Berlin kg tank:

lcl _tank

Global network of innovation

Attributes : Syntax
CLASS CLASS < <classname classname> > DEFINITION. DEFINITION. ... ... TYPES: TYPES: < < normale normale Typdefinition Typdefinition >. >. CONSTANTS: CONSTANTS: constant constant TYPE TYPE <type> <type> VALUE VALUE <value>. <value>. DATA: DATA: variable1 variable1 variable2 variable2 variable3 variable3 variable4 variable4 variable5 variable5 variable6 variable6 variable7 variable7 CLASS-DATA: CLASS-DATA: ... ... ENDCLASS. ENDCLASS. TYPE TYPE <type>, <type>, TYPE TYPE < <ddic ddic_type>, _type>, LIKE LIKE variable1, variable1, TYPE TYPE <type> <type> VALUE VALUE <value>, <value>, TYPE TYPE <type> <type> READ-ONLY READ-ONLY, , TYPE TYPE REF REF TO TO < <classname classname>, >, TYPE TYPE REF REF TO TO <interface>. <interface>.

Global network of innovation

Attributes : Visibility
CLASS CLASS lcl lcl_airplane _airplane DEFINITION. DEFINITION. PUBLIC PUBLIC SECTION. SECTION. DATA DATA: : name name TYPE TYPE string. string. PRIVATE PRIVATE SECTION. SECTION. DATA: DATA: weight weight TYPE TYPE saplane saplane-weight. -weight. ENDCLASS. ENDCLASS. CLASS CLASS lcl lcl_airplane _airplane DEFINITION. DEFINITION.

Public attributes

Can be viewed and changed by all users and in all methods Direct access

Private attributes

Can only be viewed and changed from within the class No direct access from outside the class
better

PUBLIC PUBLIC SECTION. SECTION. ... ... PRIVATE PRIVATE DATA DATA: : SECTION. SECTION. weight TYPE weight TYPE saplane saplane-weight, -weight, name TYPE string. name TYPE string.

ENDCLASS. ENDCLASS.

Global network of innovation

Attributes : Instance and Static-1


Instance attributes

CLASS lcl _airplane DEFINITION. PUBLIC SECTION. PRIVATE SECTION. DATA : weight TYPE saplane -weight, name TYPE string.

One per instance Statement: DATA

Static attributes

Only one per class Statement: CLASS-DATA

CLASS-DATA : count TYPE I. ENDCLASS.

Also known as class attributes

Global network of innovation

Attributes : Instance and Static-2

count: 2

name: LH Berlin weight: 30,000 kg

name: AA Boston weight: 45,000 kg

Main memory
Static attributes for class LCL_AIRPLANE

Global network of innovation

Principles : Overview-4

Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Methods

Contain coding Have an interface


lcl _airplane ...

fly land

Global network of innovation

Methods : Syntax
CLASS < classname > DEFINITION . ... METHODS : <method_name> [ IMPORTING < im _var > TYPE <type> EXPORTING <ex_ var > TYPE <type> CHANGING < ch _var > TYPE <type> RETURNING VALUE( <re_ var >) TYPE <type> EXCEPTIONS <exception> ]. ENDCLASS.

CLASS < classname > IMPLEMENTATION . METHOD <method_name>. ... ENDMETHOD . ENDCLASS.

Global network of innovation

Methods : Visibility
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS : set_name importing im _name TYPE string. PRIVATE SECTION. METHODS : init _name. DATA: name TYPE string. ENDCLASS. CLASS lcl _airplane IMPLEMENTATION. METHOD init _name. name = No Name. ENDMETHOD . METHOD set_name. IF im _name IS INITIAL. * Calling init _name ... ELSE. name = im _name. ENDIF. ENDMETHOD . ENDCLASS.

Public methods

Can be called from outside the class

Private methods

Can only be called within the class

Global network of innovation

Methods : Instance and Static


Instance methods

Can use both static and instance components in the implementation part Can be called using the instance name

Static methods

Can only use static components in the implementation part Can be called using the class name

Global network of innovation

Methods Instance and Static : Example


CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS : set_name IMPORTING im _name TYPE string. CLASS-METHODS : get_count RETURNING VALUE(re_count) TYPE I. PRIVATE SECTION. DATA: name TYPE string. CLASS-DATA: count TYPE I. ENDCLASS. CLASS lcl _airplane IMPLEMENTATION. ... METHOD get_count. re_count = count . ENDMETHOD ENDCLASS.

Global network of innovation

Principles : Overview-5
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Creating Objects

Objects can only be created and addressed using reference variables

lcl _airplane name weight ... name: LH Berlin weight: 30,000 kg

CREATE OBJECT

Global network of innovation

Reference Variables
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. ... PRIVATE SECTION. ... ENDCLASS.

CLASS lcl _airplane IMPLEMENTATION. ... ENDCLASS. DATA: airplane1 TYPE REF TO cl _airplane, airplane2 TYPE REF TO cl _airplane.

airplane1

airplane2 Main memory

Global network of innovation

Creating Objects : Syntax


CREATE OBJECT <reference>. DATA: airplane1 TYPE REF TO lcl _airplane, airplane2 TYPE REF TO lcl _airplane. CREATE OBJECT airplane1. CREATE OBJECT airplane2.

airplane1

name: weight: 0

airplane2

name: weight: 0 Main memory

Global network of innovation

Assigning References
... DATA: airplane1 TYPE REF TO airplane2 TYPE REF TO CREATE OBJECT airplane1. CREATE OBJECT airplane2. airplane1 = airplane2. lcl _airplane, lcl _airplane.

airplane1

name: weight: 0

airplane2

name: weight: 0 Main memory

Global network of innovation

Garbage Collector
... DATA: airplane1 TYPE REF TO lcl _airplane, airplane2 TYPE REF TO lcl _airplane. CREATE OBJECT airplane1 EXPORTING ... . CREATE OBJECT airplane2 EXPORTING ... . airplane1 = airplane2.

airplane1

name: LH B weight: 30,000 kg

airplane2

name: AA Bost weight: 45,000 kg Main memory

Global network of innovation

Principles : Overview-6

Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

References : Comparison
DATA: airplane1 TYPE REF TO airplane2 TYPE REF TO lcl _airplane, lcl _airplane.

CREATE OBJECT airplane1 EXPORTING im _name = LH Berlin ... CREATE OBJECT airplane2 EXPORTING im _name = LH Berlin ... IF airplane1 = airplane2 . ... ENDIF. not equal to

airplane1 n: LH Berlin w: 30,000 kg airplane2 n: LH Berlin w: 30,000 kg Main memory

Global network of innovation

Buffering References -1
DATA: airplane airplane_table TYPE REF TO cl _airplane, TYPE TABLE OF REF TO cl_airplane.

CREATE OBJECT airplane. APPEND airplane TO airplane_table.

airplane airplane_table

Main memory

airplane CREATE OBJECT airplane. APPEND airplane TO airplane_table. airplane_table

Main memory

Global network of innovation

Buffering References -2
LOOP AT TO airplane_table INTO airplane. * work with the current instance

ENDLOOP.

airplane airplane_table

Main memory

Global network of innovation

Object Reference as Attributes

lcl_airplane

lcl_wings

orientat .: left length: 15 m name: LH Berlin weight: 30,000 kg left_wing: right_wing: orientat .: right length: 15 m

Global network of innovation

External Access to Public Attributes


Instance attribute: <reference> -> <instance_attribute> Class attribute: <classname> => <class_attribute> CLASS lcl _airplane DEFINITION. PUBLIC SECTION . DATA : name TYPE string READ-ONLY. CLASS-DATA : count TYPE I READ-ONLY. ... ENDCLASS. ... DATA: airplane1 TYPE REF TO lcl _airplane. DATA: airplane_name TYPE STRING, n_o_airplanes TYPE i. ... airplane_name = airplane1->name . n_o_airplanes = lcl _airplane=>count .

name: LH Berlin

Global network of innovation

Calling Methods

Run Data

call method O2->Do_it

Do_it Data

O1

O2

Global network of innovation

Calling Methods : Syntax


Instance methods: CALL METHOD <instance> -><instance_method> EXPORTING < im_var > = <variable> IMPORTING <ex_ var > = <variable> CHANGING < ch_var > = <variable> RECEIVING <re_ var > = <variable> EXCEPTIONS <exception> = < nr>. CALL METHOD <classname >=><class_method> EXPORTING ... .

Static methods:

DATA: airplane TYPE REF TO lcl _airplane. DATA: name TYPE string. DATA: count_planes TYPE I. CREATE OBJECT airplane. CALL METHOD airplane->set_name EXPORTING im _name = name. CALL METHOD l cl_airplane=>get_count RECEIVING re_count = count_planes.

Global network of innovation

Constructor
Special method for creating objects with defined initial state Only has IMPORTING parameters and EXCEPTIONS Exactly one constructor is defined per class (explicitly or implicitly) Is executed exactly once per instance
METHODS CONSTRUCTOR IMPORTING <im _parameter> EXCEPTIONS <exception>. name: LH Berlin weight: 30,000 kg lcl _airplane name weight count constructor

CREATE OBJECT

Global network of innovation

Constructor : Example
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS CONSTRUCTOR IMPORTING im _name TYPE string im _weight TYPE I.

PRIVATE SECTION. DATA: name TYPE string, weight TYPE I. CLASS-DATA count TYPE I. ENDCLASS. CLASS lcl _airplane IMPLEMENTATION. METHOD CONSTRUCTOR. name = im _name. weight = im _weight. count = count + 1. ENDMETHOD. ENDCLASS. DATA airplane TYPE REF TO lcl _airplane. name: LH Berlin ... weight: 30,000 kg CREATE OBJECT airplane EXPORTING im _name = `LH Berlin` im _weight = 30000.

Global network of innovation

Static Constructor
CLASS <classname > DEFINITION. PUBLIC SECTION. CLASS-METHODS CLASS_CONSTRUCTOR . ENDCLASS. CLASS lcl_airplane DEFINITION. PUBLIC SECTION. CLASS-METHODS: CLASS_CONSTRUCTOR, get_count RETURNING VALUE(re_count) TYPE I. CLASS-DATA: count TYPE I. ENDCLASS. CLASS lcl_airplane IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR . ... ENDMETHOD. ... ENDCLASS.

CLASS <classname > IMPLEMENTATION. METHOD CLASS_CONSTRUCTOR . ... ENDMETHOD. ENDCLASS.

Global network of innovation

Call To Static Constructor


Special static method Automatically called before the class is first accessed Only executed once per program
* Example 1: DATA airplane TYPE REF TOcl_airplane. CREATE OBJECT airplane . * Example 2: DATA class_id TYPE string. class_id = lcl_airplane=>count . * Example 3: DATA count_airplane TYPE I. CALL METHOD lcl_airplane=>get_count RECEIVING re_count = count_airplane.

Global network of innovation

Principles Overview -7
Objects Classes Attributes Methods Instantiation, Garbage Collector Working with Objects Further Principles

Global network of innovation

Encapsulation
Class as capsule for functions Defined responsibilities within a capsule (class) Defined interfaces using

Public components of class (PUBLIC SECTION) Interfaces

Implementation of component remains hidden through limited visibility (PRIVATE SECTION)

Global network of innovation

Client Server Behavior


Classes behave toward each other as client/server systems. Classes normally play both roles. Responsibilities between the classes must be established.

Data

Run

CALL METHOD server->Do_it Do_it Data

Client

Server

Global network of innovation

The Delegation Principle

lcl _airplane lcl _client tank : lcl _tank get_fuel_level () : re_level fuel : i

lcl _tank fuel_max : i get_fuel_level () : re_level

re_level = tank->get_fuel_level ( ).

re_level = fuel / fuel_max * 100.

Global network of innovation

Delegation : Sequence Diagram


pilot : lcl _client airbus : lcl _airplane 1: get_fuel_level ( ) 2: get_fuel_level ( ) re_level re_level tank : lcl _tank

Global network of innovation

Namespaces within a Class


The same namespace for

Attribute names Method names Event names Type names Constant names ALIAS names

There is a local namespace within methods

Global network of innovation

Namespace : Example
CLASS lcl _airplane DEFINITION. PUBLIC SECTION. METHODS CONSTRUCTOR IMPORTING im _name TYPE string im _weight TYPE I. PRIVATE SECTION. DATA name TYPE string. DATA weight TYPE I. ENDCLASS. CLASS cl _airplane IMPLEMENTATION. METHOD CONSTRUCTOR. DATA name TYPE string VALUE `-airplane`. CONCATENATE weight = ENDMETHOD. ENDCLASS. im _name name INTO ME->name .

im _weight.

Date

Global network of innovation

Questions ?

Copyright Siemens Business Services