You are on page 1of 37

IBM Global Business Services

Fundamentals of ABAP Objects

Copyright IBM Corporation 2007

IBM Global Services

Objectives
Upon completion of this chapter, the participants will be able to:
Recognize the concept of Object Oriented Programming (OOP) Identify the features of Object Oriented Programming & its Advantages Recall the history of ABAP Object Oriented Programming Identify the need to learn ABAP Objects Analyze the basic building blocks of ABAP Objects Create a local Class with Attributes, Methods, Constructors.

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Features of Procedural Programming Model


Data and Functions are kept separate. Global variables of program contains data, while subroutine contains functions. Usually non-encapsulated access to data. (exception: Global data of Function Group is only accessible by Function Modules within that group). Program maintenance as well as management becomes difficult as program size increases. Very difficult to model real word entity. Not possible to create several runtime instances easily.
Data Data Data Function Function Function

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

What is Object Oriented Programming (OOP) ?


The fundamental idea behind Object Oriented Programming (OOP) is to combine both data and the functions (methods) those operate on that data into a single unit. Such an unit is called Object. Possibility of creating several runtime instances is one of the key characteristics of Object Oriented Programming. This allows you to create a direct abstraction of a real world object.

Function Data

Object
4 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Basic building blocks of OOP


Classes and Objects are the basic building blocks of Object Oriented Programming. When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods. Objects are instances of a Class.
Example : What are the characteristics of the box? (Attributes) Inside color is blue (Private) Outside color is white (Public) What is the status of the box ? (Events) The box is semi open
5 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

Functions of the box? (Methods) It can store things It can occupy space

IBM Global Services

Client/Server Relationship & Delegation


Objects behave like Client/Server systems. When an objects sends a message to another object, telling it to behave in a particular way, the first object can be seen as a Client and the second as a Server In OOP services are distributed among objects to avoid redundancy and each object offers only those services that are within its area of responsibility. If an object needs any other services, it requests these from other objects. This is known as principle of delegation.
Sends Message to Retrieve Details Data Function: GET_DETAILS Function: RETRIEVE_D ETAILS Data

Object 1
6 Fundamentals of ABAP Objects Jan-2007

Object 2
2007 IBM Corporation

IBM Global Services

Features of Object Oriented Programming


Abstraction
Modeling real world entities and processes in a more natural way.

Ecapsulation
Hiding data and its related logic behind well defined interfaces.

Inheritance
Reusing attributes and methods while allowing for specialization.

Polymorphism
Simplifying by hiding varying implementations behind the same interface.

Code Reuse
Same code can be reused multiple times by using inheritance.

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Advantages of Object Oriented Programming


Real world entity can be modeled very well. Provides advance level of data encapsulation that improves the maintainability and stability of ABAP programs. Better programming structure, reduced maintenance effort & less susceptibility to errors. Software extension is simpler & more secure. Stress on data security and access. Data encapsulation and abstraction. Once a base class is written and tested, it need not be touched again. Reusing existing code through Inheritance saves time, money and increase a programs reliability.

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

History of ABAP Object Oriented Programming


Object Oriented Programming (OOP) in general was developed at approximately the same time as procedural programming models. In SAP:
SAP Basis Release 4.5 delivered the first version of ABAP Objects. SAP Basis Release 4.6 delivered complete version of ABAP Objects by introducing Inheritance. SAP Web Application Server 6.10/6.20 enhanced ABAP Objects with Friendship and Object Services.

The object-oriented concept in ABAP is same as other modern object-oriented languages such as C++ or Java.

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

ABAP as Hybrid Language


The ABAP runtime support for both the procedural and ABAP Objects programming models

10

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

ABAP as Hybrid Language (Contd.)


ABAP Objects is not a new language, but a systematic extension of ABAP. Type checks in ABAP Object context is stricter. Cleaner Syntax: Obsolete statements lead to syntax errors. ABAP object statements can be used in procedural ABAP programs. Object (Classes) can also contain procedural ABAP statements. Although a pure OO world is technically possible, most real-world implementations use a mixture of procedural ABAP and ABAP Objects.

11

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Why do we need to learn ABAP Objects ?


To understand the recent concepts of ABAP e.g. BAPI, BAdi, Workflow. ABAP Objects is the only way you can use new ABAP technology. For example all new GUI concepts, such as the SAP Control Framework (CFW) and Business Server Pages (BSP), are encapsulated in ABAP Objects classes. For interfacing ABAP with Microsoft technologies or Java as all these are built on the OOP concept. To take advantage of cleaner syntax and semantic rules. To exploit the object resource that has been provided by SAP.

12

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Classes ( Global + Local )


A class is a template/blueprint based on which all objects of the class are created. Class does not occupies any memory space during program execution only the instance of an class (objects) occupies memory space. In ABAP, classes can be of two types:
Global Class (Created using class builder (SE24) and stored in class repository as Class pool) Local Class (Created in any ABAP program in global declarations section and only accessible to the programs within the main program.)
Global vs. Local Classes Accessed from ? Where store ? Global Classes Any Program In the class repository Local Classes Only with in the Program where it is defined Locally in the program where it is defined With ABAP editor (SE38) Any

Tools required to create ? Class builder (SE24) Namespace ? Must begin with Y or Z

13

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Declaring a Class: Local


A class declaration has two parts.
Definition Implementation CLASS test DEFINITION. PUBLIC SECTION. { Attributes, Methods, Events } PROTECTED SECTION. { Attributes, Methods, Events } PRIVATE SECTION. { Attributes, Methods, Events } ENDCLASS. CLASS test IMPLEMENTATION. <class body> {Method implementation is done here} ENDCLASS.
14 Fundamentals of ABAP Objects

Classes are template for Objects. This example declares and defines a local class test . The class DEFINITION belongs in the global declarations section at the beginning of the program. Class definition cannot be nested. Classes cannot be defined inside subroutines or function modules. A class definition declares :
Its components :

Attributes, Methods, Events.


The visibility of its components :

Public, Protected and Private.

Jan-2007

2007 IBM Corporation

IBM Global Services

Components of Class ( Instance + Static )


Instance components:
DATA
For instance attributes

METHODS
For instance methods

EVENTS
For instance events

Static components:
CLASS-DATA
For static attributes

CLASS-METHODS
For static methods

Instance components exist separately in each instance (object) of the class. Static components only exist one per class and are valid for all instances of the class. Static attributes of a class are retained throughout the entire runtime. Static components are declared with the CLASS- * keywords. To access instance components, instance component selector (->) is used. To access static components, static component selector (=>) is used.

CLASS-EVENTS
For static events

CONSTANTS
For constants
15 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Instance vs. Static components: Elaborated


The class components that share a common memory area for all the class instance are static components. The class components that have separate memory area for separate instance are instance components.
Object 1 of Class Object 2 of Class Object 1 of Class Object 2 of Class

Memory for Object 1

Memory for Object 2

Memory for Object 1 and Object 2

Instance

Static

16

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Visibility sections in a Class


All components of a class must belong to a visibility section. Components can be public, protected or private. Public components form the external interface of the class they are visible to all users of the class as well as to methods within the class and to methods of subclasses. Protected components form the interface of the class to its subclasses they are visible to methods of the heirs of the class as well as to methods within the class. Private components can only be used in the methods of the class itself. Using private visibility section is known as Encapsulation or Information hiding.

Notes: There is no default visibility section in a class. You should not make attributes public unless absolutely necessary.
17 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Attributes
CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: do_something PRIVATE SECTION. TYPES: CONSTANTS: DATA: var1 TYPE local_type, var2 TYPE global_type, var3 LIKE var1, var4 TYPE built_in_type VALUE val, var5 TYPE local_type READ-ONLY, var6 TYPE REF TO class_name. ENDCLASS.

Attributes contains data that can be stored in the objects of a class. Attributes can be of 3 types: elementary, structured or table-type. In classes you can only use TYPE addition to refer to data types and use LIKE reference only for local data objects. The READ ONLY addition can be used for data declared in PUBLIC SECTION, which means the data can be read from outside, but can be changed only by methods of the same class.

18

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Methods
CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: do_something IMPORTING ...i1 TYPE EXPORTINGe1 TYPE RETURNING VALUE (P) .. R1 TYPE CHANGING c1 TYPE EXCEPTIONS en. PRIVATE SECTION. DATA: ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD do_something. ENDMETHOD. ENDCLASS.
19 Fundamentals of ABAP Objects

Methods are the functionality of a class , ABAP code is written within a method to incorporate the functionality. Methods are processing blocks with a parameter interface very similar to function modules. Methods are of two types:
Standard Methods.
e.g. METHODS meth.

Event handler methods:


METHODS meth FOR EVENT evt OF class.
This type of methods are written to trap events.

Methods are called with a CALL METHOD statement.


Jan-2007 2007 IBM Corporation

IBM Global Services

Methods (Contd.)
Methods have a signature. With function modules, you should type each parameter but are not forced to do so; with methods, you must type each parameter. Standard methods are declared, with their parameter interface, in the class definition part and implemented in the class implementation part. All input parameters (IMPORTING & CHANGING) can have OPTIONAL or DEFAULT addition and then these parameters need not be transferred when the method is called.

20

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Constructors
METHODS constructor IMPORTING im_par TYPE string. CREATE OBJECT obj EXPORTING im_par = val_ex. Instance constructor CLASS-METHOD class_constructor Static Constructor

Constructor is a special method that is called by the runtime system as soon as the object is created with the CREATE OBJECT statement. Useful for:
Initializing data structures or setting default values to attributes dynamically. Sending message about object creation.

Each class has one constructor. It is a predefined with the name CONSTRUCTOR (or CLASS_CONSTRUCTOR for static constructor).

Constructors should be defined in the public area.


21 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Constructors (Contd.)
Instance constructor can only have IMPORTING PARAMETERS & EXCEPTIONS. It has no EXPORTING parameters because its sole purpose is to initializing the object. The Static constructor method has no interface parameters.
The Static constructor is called automatically when the class is first accessed: Creating an instance of the class (CREATE OBJECT) Accessing a static attribute or Method of the class. Registering an event handler method for an event in this class.

A static constructor cant be called explicitly When exception is raised in an instance constructor, instances are not created, thus no memory space is occupied. Notes: There is no Destructor in ABAP Objects. I.e. there is no instance method automatically called immediately before the object is deleted.
22 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Some more features of Class


CLASS class_name DEFINITION DEFERRED.
This is used in forward referencing.

CLASS class_name DEFINITION LOAD.


If the first access to a global class in a program is to its static components then explicit loading of the class definition is necessary. In release 6.40 this statement is not required.

CLASS class_name DEFINITION CREATE PUBLIC| PROTECTED | PRIVATE. CREATE PUBLIC addition is provided automatically by compiler if no create addition is used. The additions CREATE PROTECTED and CREATE PRIVATE allow you to control the instantiation of your class.

23

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Objects and Object references


CLASS c1 DEFINITION. PUBLIC SECTION. DATA: int TYPE I VALUE 10. METHODS display_int. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD display_int. WRITE / int. ENDMETHOD. ENDCLASS. DATA : oref TYPE REF TO c1. START-OF-SELECTION. CREATE OBJECT oref. WRITE / oref-> int. CALL METHOD oref-> display_int.
24 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

Objects are real runtime replica of classes. Objects can only be created and addressed using reference variables. To use objects:
Declare reference variables with type

of the class.
Create objects, assigning their

references. (This is called Instantiation) Use the object components.

IBM Global Services

Self- Reference
CLASS c1 DEFINITION. If an objects internally needs to PUBLIC SECTION. provide its own reference, it can use DATA: int TYPE I VALUE 10. the local reference variable ME. METHODS display_int. ME is predefined and always ENDCLASS. contains a reference to the address CLASS c1 IMPLEMENTATION. of its own object. METHOD display_int. DATA : int TYPE I VALUE 20. WRITE:/ int, Local variable of the Method ME->int. Variable of the Class ENDMETHOD. ENDCLASS. Notes: DATA : oref TYPE REF TO c1. ME is equivalent to THIS pointer CREATE OBJECT oref. in C++. CALL METHOD oref-> display_int.
25 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Multiple instantiation
CLASS c1 DEFINITION. PUBLIC SECTION. METHODS meth. ENDCLASS. CLASS c1 IMPLEMENTATION. ENDCLASS. DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c1. START-OF-SELECTION. CREATE OBJECT: oref1, oref2.

Programs can instantiate multiple objects of the same class.

26

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Deleting Objects
oref1 oref2 oref1

DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c2. ... CREATE OBJECT: oref1, oref2. oref1 = oref2.

9999
oref2

9999 8888 9999 8888 9999 8888 9999 8888

Object of C1 Object of C2

8888
oref1

8888
oref2

Object of C1 Object of C2

8888
oref1

CLEAR oref1.

Object of C1 Object of C2 Object of C1 Object of C2

oref2 oref1

8888

CLEAR oref2.
27 Fundamentals of ABAP Objects

oref2

Jan-2007

2007 IBM Corporation

IBM Global Services

Calling Methods
CALL METHOD oref->meth EXPORTING im_par1 = val_ex1. im_par(n)= val_ex(n) IMPORTING ex_par = val_im CHANGING ch_par = val_chg RECEIVING ret_par = val_res... EXCEPTIONS exception = val_rc Shorter Syntax available from SAP Web AS 6.10 oref->meth( EXPORTING im_par1 = val_ex1. im_par(n)= val_ex(n) IMPORTING ex_par = val_im CHANGING ch_par = val_chg RECEIVING ret_par = val_res... EXCEPTIONS exception = val_rc)

28

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Functional Methods
METHODS meth IMPORTING RETURNING VALUE (r) CALL METHOD oref->meth EXPORTING i1 = a1.in = an RECEIVING r = a.
Conventional Method call

Example: var = oref->meth(). or var = oref->meth(a). or var = oref->meth( i1 = a1.in = an).

Method call specific to Functional method

Instead of CALL METHOD, functional methods can be performed in expressions. A Functional method can have zero to many IMPORTING parameters, EXCEPTIONS and exactly one RETURNING parameter, that must be passed by value. A Functional method can be instance or static method. Operands can be replaced by functional methods

29

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Pointer tables
DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c1, oref3 TYPE REF TO c1. DATA: oref TYPE REF TO c1, oref_tab TYPE TABLE OF REF TO c1. START-OF-SELECTION. DO 3 TIMES. CREATE OBJECT oref. APPEND oref TO oref_tab. ENDDO. LOOP AT oref_tab INTO oref. CALL METHOD oref->meth. ENDLOOP.
30 Fundamentals of ABAP Objects

Pointer tables are used to store multiple instances/objects of same class. This method reduces coding and more elegant against creating separate, separate object reference variables for storing every objects of the same class. Reference variables are handled like any other data object with an elementary data type.

Jan-2007

2007 IBM Corporation

IBM Global Services

Dynamic Method calls


CLASS c1 DEFINITION. PUBLIC SECTION. METHODS: meth1, meth2. DATA fld TYPE DATA oref TYPE REF TO c1. CREATE OBJECT oref. Do something to assign meth1 or meth2 to fld at runtime. fld = METH1 or METH2. CALL METHOD oref->(fld).

Instance, self-reference, and static method can all be called dynamically; the class name for static methods can also be determined dynamically: Variants: - oref->(method) - me->(method) - class=>(method) - (class)=>method - (class)=>(method) A methods parameters can be passed dynamically using PARAMETER-TABLE and EXCEPTION-TABLE additions to the CALL METHOD statement.

31

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Demonstration
Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.

32

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Practice
Creating a local class with different components in different visibility sections and showing how to instantiate the class as well as how to access the instance and static components.

33

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Summary
Features of Object oriented programming are:
Abstraction Ecapsulation Inheritance Polymorphism Code Reuse

Classes and Objects are the basic building blocks of Object Oriented Programming When a real world entity is modeled into OOP world then it is known as Class, characteristics as attributes and functionality as methods. Objects is an instance of a Class. Classes can be of two types:
Global Class (Created using class builder (SE24) and stored in class repository as Class pool) Local Class (Created in any ABAP program)

34

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Questions
What kind of Programming language is ABAP ? What version of SAP first released ABAP Objects ? Can class definition be nested ? Can you define a local class within a subroutine or function module ? What is the default visibility section in a class ? Can you call a constructor with the CALL METHOD statement ? What Interface parameters does a Static Constructor have ? When is CLASS class_name DEFINITION DEFERRED statement required ? What parameters does a Functional method have ? Which transaction we use to maintain global class? What are the various visibility sections present in a ABAP class? What is the basic difference between static component and instance component ?
35 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation

IBM Global Services

Questions
Can we access the static component of a class by the object name instead of the class name? What are the main advantages of Object Oriented Programming over Procedural Programming ?

36

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

IBM Global Services

Hands-on Exercises
Exercise 1: Exercise 2:

37

Fundamentals of ABAP Objects

Jan-2007

2007 IBM Corporation

You might also like