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.

2 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 Function

Data Function

Data Function

3 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 :
Functions of the box? (Methods)
What are the
 It can store things
characteristics of the
box? (Attributes)  It can occupy space

 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


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

Function: Function:
Data RETRIEVE_D Data
GET_DETAILS
ETAILS

Object 1 Object 2

6 Fundamentals of ABAP Objects Jan-2007 © 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.

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

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

9 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 Global Classes Local Classes


Classes
Accessed from ? Any Program Only with in the Program where
it is defined
Where store ? In the class repository Locally in the program where it
is defined
Tools required to create Class builder (SE24) With ABAP editor (SE38)
?
Namespace ? Must begin with ‘Y’ or ‘Z’ Any

13 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Declaring a Class: Local

 A class declaration has two parts.  Classes are template for Objects.
 Definition
 This example declares and defines a
 Implementation
local class “test ”.
 The class DEFINITION belongs in
CLASS test DEFINITION. the global declarations section at the
PUBLIC SECTION. beginning of the program.
{ Attributes, Methods, Events }
 Class definition cannot be nested.
PROTECTED SECTION.
{ Attributes, Methods, Events }  Classes cannot be defined inside
PRIVATE SECTION. subroutines or function modules.
{ Attributes, Methods, Events }  A class definition declares :
ENDCLASS.  Its components :
Attributes, Methods, Events.
CLASS test IMPLEMENTATION.
 The visibility of its components :
<class body>
Public, Protected and Private.
{Method implementation is done here}
ENDCLASS.
14 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation
IBM Global Services

Components of Class ( Instance + Static )

 Instance components:  Instance components exist separately in each


 DATA instance (object) of the class.
 For instance attributes  Static components only exist one per class and
 METHODS are valid for all instances of the class.
 For instance methods  Static attributes of a class are retained
 EVENTS throughout the entire runtime.
 For instance events
 Static components are declared with the
 Static components: CLASS- * keywords.
 CLASS-DATA  To access instance components, instance
 For static attributes component selector (->) is used.
 CLASS-METHODS  To access static components, static component
 For static methods 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 Memory for Memory for Object 1


Object 1 Object 2 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.  Attributes contains data that can be


PUBLIC SECTION. stored in the objects of a class.
METHODS: do_something…  Attributes can be of 3 types:
PRIVATE SECTION. elementary, structured or table-type.
TYPES: ……  In classes you can only use TYPE
CONSTANTS: …… addition to refer to data types and use
DATA: LIKE reference only for local data
var1 TYPE local_type, objects.
var2 TYPE global_type,  The READ ONLY addition can be
var3 LIKE var1, used for data declared in PUBLIC
var4 TYPE built_in_type VALUE val, SECTION, which means the data can
be read from outside, but can be
var5 TYPE local_type READ-ONLY,
changed only by methods of the same
var6 TYPE REF TO class_name. class.
ENDCLASS.

18 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Methods

CLASS c1 DEFINITION.  Methods are the functionality of a


PUBLIC SECTION. class , ABAP code is written within a
METHODS: do_something method to incorporate the
IMPORTING ...i1 TYPE… functionality.
EXPORTING…e1 TYPE…
 Methods are processing blocks with a
RETURNING VALUE (P)
.. R1 TYPE…
parameter interface very similar to
function modules.
CHANGING …c1 TYPE…
EXCEPTIONS …en.  Methods are of two types:
PRIVATE SECTION.  Standard Methods.
DATA: … e.g. METHODS meth.
ENDCLASS.  Event handler methods:
CLASS c1 IMPLEMENTATION. METHODS meth FOR EVENT evt
METHOD do_something. OF class.
This type of methods are written to
… trap events.
ENDMETHOD.
 Methods are called with a CALL
ENDCLASS.
METHOD statement.
19 Fundamentals of ABAP Objects 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  Constructor is a special method that is


IMPORTING im_par TYPE string. called by the runtime system as soon as
CREATE OBJECT obj the object is created with the CREATE
OBJECT statement.
EXPORTING im_par = val_ex.
 Useful for:
Instance
 Initializing data structures or setting default
constructor
values to attributes dynamically.

CLASS-METHOD class_constructor  Sending message about object creation.


 Each class has one constructor. It is a
Static predefined with the name CONSTRUCTOR
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 can’t 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.  Objects are real runtime replica of


PUBLIC SECTION. classes.
DATA: int TYPE I VALUE ’10’.  Objects can only be created and
METHODS display_int. addressed using reference variables.
ENDCLASS.  To use objects:
CLASS c1 IMPLEMENTATION.  Declare reference variables with type
METHOD display_int. of the class.
WRITE / int.  Create objects, assigning their
references. (This is called
ENDMETHOD.
Instantiation)
ENDCLASS.
 Use the object components.
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


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.
DATA : oref TYPE REF TO c1. Notes:
CREATE OBJECT oref.  “ME” is equivalent to “THIS” pointer
CALL METHOD oref-> display_int. in C++.

25 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Multiple instantiation

CLASS c1 DEFINITION.  Programs can instantiate multiple


PUBLIC SECTION. objects of the same class.
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.

26 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Deleting Objects
… oref1

DATA: oref1 TYPE REF TO c1,


oref2
oref2 TYPE REF TO c2.
... oref1
9999 9999 Object of C1
CREATE OBJECT: oref1, oref2. oref2
8888 Object of C2
… 8888
oref1
8888 9999 Object of C1
oref1 = oref2. oref2
8888 Object of C2
8888
oref1
9999 Object of C1
CLEAR oref1. oref2
8888 8888 Object of C2

oref1
9999 Object of C1
CLEAR oref2. oref2
Object of C2
8888

27 Fundamentals of ABAP Objects 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  Instead of CALL METHOD,


IMPORTING… functional methods can be
RETURNING VALUE (r)… performed in expressions.
 A Functional method can have
Conventional
zero to many IMPORTING
CALL METHOD oref->meth Method call
parameters, EXCEPTIONS
EXPORTING i1 = a1….in = an and exactly one RETURNING
RECEIVING r = a. parameter, that must be
Method call passed by value.
Example: specific to  A Functional method can be
var = oref->meth(). Functional instance or static method.
or method
 Operands can be replaced by
var = oref->meth(a). functional methods
or
var = oref->meth( i1 = a1….in = an).

29 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Pointer tables

DATA: oref1 TYPE REF TO c1,  Pointer tables are used to store
oref2 TYPE REF TO c1, multiple instances/objects of
oref3 TYPE REF TO c1. same class. This method
reduces coding and more
DATA: oref TYPE REF TO c1, elegant against creating
oref_tab TYPE TABLE OF REF TO c1. separate, separate object
reference variables for storing
START-OF-SELECTION. every objects of the same class.
…  Reference variables are
DO 3 TIMES. handled like any other data
CREATE OBJECT oref. object with an elementary data
APPEND oref TO oref_tab. type.
ENDDO.

LOOP AT oref_tab INTO oref.
CALL METHOD oref->meth.
ENDLOOP.

30 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation


IBM Global Services

Dynamic Method calls

CLASS c1 DEFINITION.  Instance, self-reference, and static method


PUBLIC SECTION. can all be called dynamically; the class
METHODS: meth1, meth2. name for static methods can also be

determined dynamically:
DATA fld TYPE … Variants:
DATA oref TYPE REF TO c1. - oref->(method)
… - me->(method)
CREATE OBJECT oref. - class=>(method)
… - (class)=>method
Do something to assign meth1
or meth2 to fld at runtime.
- (class)=>(method)
fld = ‘METH1’ or ‘METH2’.  A method’s parameters can be passed
dynamically using PARAMETER-TABLE
and EXCEPTION-TABLE additions to the
CALL METHOD oref->(fld).
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