Fundamentals of ABAP Objects: IBM Global Business Services
Fundamentals of ABAP Objects: IBM Global Business 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.
July-2007
Function
Function
Data
Function
July-2007
Function
Data
Object
4
July-2007
July-2007
Function:
GET_DETAILS
Function:
RETRIEVE_D
ETAILS
Object 1
6
Data
Object 2
July-2007
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.
July-2007
July-2007
July-2007
10
July-2007
11
July-2007
12
July-2007
13
Global Classes
Local Classes
Accessed from ?
Any Program
Where store ?
Tools required to
create ?
Namespace ?
Any
July-2007
July-2007
Notes:
There is no default visibility section in a class.
You should not make attributes public unless absolutely necessary.
While defining local Classes in you must follow the syntactical sequence of
PUBLIC, PROTECTED & PRIVATE section.
15
July-2007
Demonstration
The example program will demonstrate that external users cannot access the
protected and private components of a class.
Example Code
During Syntax check of the program, an error will be generated which will prove
that protected and private components of a class cannot be accessed by external
users.
Comment out the last two lines to execute the program successfully.
16
July-2007
METHODS
For instance methods
EVENTS
For instance events
Static components:
CLASS-DATA
For static attributes
CLASS-METHODS
For static methods
CLASS-EVENTS
For static events
CONSTANTS
For constants
17
July-2007
Object 2 of Class
Memory for
Object 1
Memory for
Object 2
Object 1 of Class
Static
Instance
18
Object 2 of Class
July-2007
Demonstration
The example program will demonstrate that Static methods of a class can only use
static attributes of that class. It cannot use instance attributes. But, instance
methods can use both.
Example Code
Both the static and instance methods are attempting to display values of the static
and instance attributes: STNUM and INSTNUM .
On syntax check, an error will be generated which will demonstrate that static
method STMETH cannot work with instance attribute, INSTNUM.
Remove the line in bold in the program and compile. It will get successfully
compiled and executed.
19
July-2007
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.
20
July-2007
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:
Standard Methods.
e.g. METHODS meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD do_something.
ENDMETHOD.
ENDCLASS.
21
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.
If there are more than one OPTIONAL import parameters in a method and no
non-optional import parameters without values, one can type in the clause
PREFERRED PARAMETER after the list of import parameters to specify which of
the optional parameters will get more preference
22
July-2007
Demonstration
This program will demonstrate the use of PREFERRED parameter in methods.
Example Code
This program contains a class C1 containing method METH1 which has two
optional parameters, INPUT1 and INPUT2. Out of them, parameter INPUT2 is
declared as PREFERRED parameter. The method simply displays the value of
two import parameters .
Notice the last line of the program and see the output. The output will establish
that the preferred parameter INPUT2 gets the value passed to the method.
23
July-2007
of the class.
Create objects, assigning their
START-OF-SELECTION.
CREATE OBJECT oref.
WRITE / oref-> int.
CALL METHOD oref-> display_int.
24
July-2007
Hands-on Exercise 1
Objectives:
Creating a Local Class with different components in different visibility sections
Define and implement Methods
Showing how to instantiate the class
Access the instance and static components of the Class.
25
July-2007
Constructors
METHODS constructor
IMPORTING im_par TYPE string.
CREATE OBJECT obj
EXPORTING im_par = val_ex.
Instance
constructor
CLASS-METHOD class_constructor
Static
Constructor
July-2007
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.
July-2007
Hands-on Exercise 2
Objectives:
Create a constructor for a class
Create an object using the constructor
28
July-2007
29
July-2007
Demonstration
This program will demonstrate how one can refer to a class without defining the
class before that point. But, the class has to be defined later on.
Example Code
30
July-2007
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.
start-of-selection.
DATA : oref TYPE REF TO c1.
CREATE OBJECT oref.
CALL METHOD oref->display_int.
31
Notes:
ME is equivalent to THIS pointer
in C++.
July-2007
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.
32
July-2007
Deleting Objects
oref1
oref2
...
oref1
oref2
9999
8888
9999
Object of C1
8888
Object of C2
9999
Object of C1
8888
Object of C2
9999
Object of C1
8888
Object of C2
9999
Object of C1
8888
Object of C2
oref1
oref1 = oref2.
8888
oref2
8888
oref1
CLEAR oref1.
oref2
8888
oref1
CLEAR oref2.
33
oref2
July-2007
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)
34
July-2007
Functional Methods
METHODS meth
IMPORTING
RETURNING VALUE (r)
CALL METHOD oref->meth
EXPORTING i1 = a1.in = an
RECEIVING r = a.
Conventional
Method call
Method call
Example:
specific to
Functional
var = oref->meth().
method
or
var = oref->meth(a).
or
var = oref->meth( i1 = a1.in = an).
35
July-2007
Demonstration
This program will demonstrate the use of FUNCTIONAL methods.
Example Code
36
July-2007
Hands-on Exercise 3
Objectives:
Define Functional Methods
Call Functional Methods
37
July-2007
Hands-on Exercise 4
Objectives:
Define & call Private Methods.
Use keyword ME
Using Static Constructor
Raising and handling Exceptions for methods.
38
July-2007
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.
July-2007
Demonstration
This program will demonstrate the use of POINTER tables.
Example Code
40
July-2007
Variants:
- oref->(method)
- me->(method)
- class=>(method)
- (class)=>method
41
- (class)=>(method)
A methods parameters can be passed
dynamically using PARAMETER-TABLE
and EXCEPTION-TABLE additions to the
CALL METHOD statement.
July-2007
Demonstration
This program will demonstrate Dynamic Method calls.
Example Code
The program contains class C1 with static method (STATM) and instance method
(INSTM). The program utilizes syntaxes to call these static/instance methods
dynamically.
42
July-2007
Demonstration
This program will demonstrate PARAMETER-TABLE & EXCEPTION-TABLE.
Example Code
This demonstrates, dynamic call of the static method gui_download of the global
class cl_gui_frontend_services to store the contents in an internal table in a file
on the presentation server. The names of class and method are specified in the
strings class and meth. The interface parameters are passed in the internal table
ptab and to the execptions of the method return values are assigned using table
etab Exceptions that can occur at the method call itself, are handled in a TRY
control structure with the statement CATCH.
43
July-2007
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)
44
July-2007
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 ?
45
July-2007
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 ?
46
July-2007