Você está na página 1de 29

IBM Global Services

Fundamentals of ABAP Objects

Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Objectives

 The participants will be able to:


 Recognize the concept of Object Oriented Programming (OOP)
 Identify the features of Object Oriented Programming
 Recall the history of ABAP Object Oriented Programming
 Advantages of ABAP OOP over conventional ABAP Procedural Programming
 Analyze the basic building blocks of ABAP Objects

2 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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, i.e. key principle of OOP is “Data controlling
access to code”.

3 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Advantages of Object Oriented Programming

 Better Programming Structure


 Real world entity can be modeled very well
 Stress on data security and access
 Data encapsulation and abstraction
 Reduction in code redundancy

4 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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.

5 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

History of ABAP Object Oriented Programming

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

6 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

ABAP as Hybrid Language

7 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

ABAP as Hybrid Language (Contd.)

8 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Advantages of ABAP OOP over conventional ABAP


Procedural Programming
 ABAP Objects provides advance level of data encapsulation that improves the
maintainability and stability of ABAP programs.
 ABAP Objects provides instantiation of multiple instances of a single class.
 ABAP Objects enhances code reuse through “Inheritance”.
 ABAP Objects helps us to work with an object’s business logic through a
standalone interface.
 ABAP Objects makes it easy to incorporate event driven programming models.
 ABAP Objects are more explicit, and therefore simpler to use.
 ABAP Objects offers cleaner syntax and semantic rules.
 ABAP Objects offers the only way to use new ABAP technology.

9 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Advantages of ABAP OOP over conventional ABAP


Procedural Programming (Contd.)
 ABAP Objects provides advance level of data encapsulation that improves the
maintainability and stability of ABAP programs.
 ABAP Objects provides instantiation of multiple instances of a single class.
 ABAP Objects enhances code reuse through “Inheritance”.
 ABAP Objects helps us to work with an object’s business logic through a
standalone interface.
 ABAP Objects makes it easy to incorporate event driven programming models.
 ABAP Objects are more explicit, and therefore simpler to use.
 ABAP Objects offers cleaner syntax and semantic rules.
 ABAP Objects offers the only way to use new ABAP technology.

10 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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 is an instance 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

11 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Classes ( Global + Local )

 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)

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 In the program where
it is defined
Tools required to Class builder (SE24) With ABAP editor
create ? (SE38)
Namespace ? Must begin with ‘Y’ or Any
‘Z’

12 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Declaring a Class (Local)

 A class declaration has two  Classes are template for Objects.


parts.
 This declares and defines a local
 Definition
 Implementation class “test ”.
 In ABAP program this belongs to
CLASS test DEFINITION.
Global Section.
PUBLIC SECTION.
 Class definition cannot be nested.
{ Attributes, Methods, Events }
PROTECTED SECTION.  Classes cannot be defined inside
{ Attributes, Methods, Events } subroutines or function modules.
PRIVATE SECTION.  A class definition declares :
{ Attributes, Methods, Events }  Its components :
ENDCLASS.
Attributes, Methods, Events.
 The visibility of its components :
CLASS test IMPLEMENTATION.
<class body> Public, Protected and Private.
{Method implementation is done here}
ENDCLASS.
13 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation
IBM Global Services

Components of Class ( Instance + Static )

 Instance components:  Instance components exist


 DATA separately in each instance (object)
 For instance attributes of the class.
 METHODS  Static components only exist one
 For instance methods per class and are valid for all
 EVENTS instances of the class.
 For instance events  Static components are declared with
 Static components: the CLASS- * keywords.
 CLASS-DATA  To access instance components,
 For static attributes instance component selector (->) is
 CLASS-METHODS used.
 For static methods  To access static components, static
 CLASS-EVENTS component selector (=>) is used.
 For static events
 CONSTANTS
 For constants

14 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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.

15 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Methods
 Methods are the functionality of a
CLASS c1 DEFINITION. class , ABAP codes are written within
a method to incorporate the
PUBLIC SECTION.
functionality.
METHODS: do_something
 Methods are processing blocks with a
IMPORTING ...i1 TYPE…
parameter interface very similar to
EXPORTING…e1 TYPE…
function modules.
CHANGING …c1 TYPE…
 Methods are of two types:
EXCEPTIONS …en.
 Standard Methods.
PRIVATE SECTION.
e.g. METHODS meth.
DATA: …  Event handler methods:
ENDCLASS. METHODS meth FOR EVENT evt
CLASS c1 IMPLEMENTATION. OF class.
METHOD do_something. This type of methods are written to
trap events.

 Methods are called with a CALL
ENDMETHOD.
METHOD statement.
ENDCLASS.

16 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Constructors

METHODS constructor  Each class has one constructor. It is a


IMPORTING … predefined, public instance method of
EXPORTING … the class, with the name
CONSTRUCTOR (or
CREATE OBJECT obj EXPORTING …
CLASS_CONSTRUCTOR for static
Instance constructor).
constructor
 Constructors are special methods that
produce a defined initial state of
CLASS-METHOD class_constructor objects and classes.
Static  Constructors are executed once for
Constructor each instance. They are called
automatically after you have created
an instance of the class with the
CREATE OBJECT statement.

17 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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.

18 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Objects and Object references

CLASS c1 DEFINITION.
 Classes are the templates of objects;
PUBLIC SECTION.
actual objects must be created and
DATA: int TYPE I VALUE ’10’. referenced to be of use (except for
METHODS display_int. the static components of a class)
ENDCLASS.  Reference variables (TYPE REF TO)
CLASS c1 IMPLEMENTATION. contain references to objects- Users
METHOD display_int. can only access instance objects
WRITE / int. through reference variables.
ENDMETHOD.
ENDCLASS.  To use objects:
DATA : oref TYPE REF TO c1.  Declare reference variables.
START-OF-SELECTION.  Create objects, assigning their
CREATE OBJECT oref. references.
WRITE / oref-> int.  Use the object components

CALL METHOD oref-> display_int.

19 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Self- Reference

CLASS c1 DEFINITION.
 If an objects internally needs to
PUBLIC SECTION. provide its own reference. For
DATA: int TYPE I VALUE ’10’. example to another object, it can use
METHODS display_int. the local reference variable “ME”.
ENDCLASS.  “ME” is predefined and always
CLASS c1 IMPLEMENTATION. contains a reference to the address
METHOD display_int. of its own object.
DATA : int TYPE I VALUE ’20’.
WRITE:/ int,
ME->int.
ENDMETHOD.
ENDCLASS.
DATA : oref TYPE REF TO c1.
 Note : “ME” is equivalent to “THIS”
CREATE OBJECT oref.
pointer in C++.
CALL METHOD oref-> display_int.

20 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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, oref1.

21 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Deleting Objects
… oref1

DATA: oref1 TYPE REF TO c1, oref2


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

oref2 Object C2
8888

oref1 Object C1
CLEAR oref2. oref2 Object C2

22 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Functional Methods

METHODS meth  Instead of CALL METHOD,


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

e.g.,
var = oref-> meth( i1 = a1….in =
an).

23 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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 of same
oref3 TYPE REF TO c1. class. This method reduces
coding and more elegant
DATA: oref TYPE REF TO c1, against creating separate,
oref_tab TYPE TABLE OF REF TO c1. separate object reference
variables for storing every
START-OF-SELECTION. 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.

24 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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 name
for static methods can also be determined
METHODS: meth1,meth2.
dynamically:

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

25 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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.

26 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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.

27 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 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)

28 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation


IBM Global Services

Questions

 What is Object Oriented Programming ?


 What are the main advantages of Object Oriented Programming over Procedural
Programming ?
 What is a Class?
 What is an Object?
 Which transaction we use to maintain global class?
 What is a constructor?
 What are the various visibility sections present in a ABAP class?
 What is the basic difference between static component and instance component?
 Can we access the static component of a class by the object name instead of the
class name?

29 Fundamentals of ABAP Objects | 11.01 March-2005 © 2005 IBM Corporation