Anda di halaman 1dari 29

Fundamentals of

ABAP Objects

Fundamentals of ABAP Objects | 11.01

March-2005

Objectives
OThe participants will be able to:
ORecognize the concept of Object Oriented

Programming (OOP)
OIdentify the features of Object Oriented
Programming
ORecall the history of ABAP Object Oriented
Programming
OAdvantages of ABAP OOP over conventional
ABAP Procedural Programming
OAnalyze the basic building blocks of ABAP
Objects
Fundamentals of ABAP Objects | 11.01

March-2005

What is Object Oriented


Programming (OOP) ?
OThe 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.

Fundamentals of ABAP Objects | 11.01

March-2005

Advantages of Object
Oriented Programming
OBetter Programming Structure
OReal world entity can be modeled

very well
OStress on data security and access
OData encapsulation and abstraction
OReduction in code redundancy

Fundamentals of ABAP Objects | 11.01

March-2005

Features of Object
Oriented Programming
OAbstraction
O Modeling real world entities and processes in a more natural way.
OEcapsulation
O Hiding data and its related logic behind well defined interfaces.
OInheritance
O Reusing attributes and methods while allowing for specialization.
OPolymorphism
O Simplifying by hiding varying implementations behind the same
interface.

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

Fundamentals of ABAP Objects | 11.01

March-2005

History of ABAP Object


Oriented Programming
OSAP Basis Release 4.5 delivered the

first version of ABAP Objects.


OSAP Basis Release 4.6 delivered
complete version of ABAP Objects by
introducing Inheritance.
OSAP Web Application Server 6.10/6.20
enhanced ABAP Objects with
Friendship and Object Services.
Fundamentals of ABAP Objects | 11.01

March-2005

ABAP as Hybrid
Language

Fundamentals of ABAP Objects | 11.01

March-2005

ABAP as Hybrid Language


(Contd.)

Fundamentals of ABAP Objects | 11.01

March-2005

Advantages of ABAP OOP over


conventional ABAP Procedural
Programming
O ABAP Objects provides advance level of data

encapsulation that improves the maintainability and


stability of ABAP programs.
O ABAP Objects provides instantiation of multiple instances
of a single class.
O ABAP Objects enhances code reuse through Inheritance.
O ABAP Objects helps us to work with an objects business
logic through a standalone interface.
O ABAP Objects makes it easy to incorporate event driven
programming models.
O ABAP Objects are more explicit, and therefore simpler to
use.
O ABAP Objects offers cleaner syntax and semantic rules.
O ABAP Objects offers the only way to use new ABAP
technology.
Fundamentals of ABAP Objects | 11.01

March-2005

Advantages of ABAP OOP over


conventional ABAP Procedural
Programming (Contd.)
O ABAP Objects provides advance level of data encapsulation

that improves the maintainability and stability of ABAP


programs.
O ABAP Objects provides instantiation of multiple instances of a
single class.
O ABAP Objects enhances code reuse through Inheritance.
O ABAP Objects helps us to work with an objects business logic
through a standalone interface.
O ABAP Objects makes it easy to incorporate event driven
programming models.
O ABAP Objects are more explicit, and therefore simpler to use.
O ABAP Objects offers cleaner syntax and semantic rules.
O ABAP Objects offers the only way to use new ABAP technology.

Fundamentals of ABAP Objects | 11.01

March-2005

10

Basic building blocks of OOP


OClasses 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.
OObjects is an instance of a Class.
Example :
Functions of the box?
(Methods)
It can store things

What are the


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

Fundamentals of ABAP Objects | 11.01


11

March-2005

Classes ( Global + Local )


OClasses can be of two types:
OGlobal Class (Created using class builder (SE24)

and stored in class repository as Class pool)


OLocal Class (Created in any ABAP program)
Global vs. Local
Classes

Global Classes

Local 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
create ?

Class builder (SE24)

With ABAP editor


(SE38)

Namespace ?

Must begin with Y or


Z

Any

12

Fundamentals of ABAP Objects | 11.01

March-2005

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}
Fundamentals
of ABAP Objects
| 11.01
ENDCLASS.

Classes are template for Objects.


This declares and defines a local
class test .
In ABAP program this belongs to
Global Section.
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.


March-2005

13

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

CLASS-EVENTS
For static events

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

CONSTANTS
For constants
Fundamentals of ABAP Objects | 11.01

March-2005

14

Visibility sections in a
Class
O All components of a class must belong to a

visibility section. Components can be


public, protected or private.
O 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.
O 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.
O Private components can only be used in the
methods of the class itself.
Fundamentals of ABAP Objects | 11.01

March-2005

15

Methods
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS: do_something
IMPORTING ...i1 TYPE
EXPORTINGe1 TYPE
CHANGING c1 TYPE
EXCEPTIONS en.
PRIVATE SECTION.
DATA:

Methods are the functionality of a class ,


ABAP codes are 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.

ENDCLASS.

e.g. METHODS meth.

CLASS c1 IMPLEMENTATION.
METHOD do_something.

Event handler methods:

METHODS meth FOR EVENT evt OF class.

ENDMETHOD.

This type of methods are written to


trap events.

ENDCLASS.

Methods are called with a CALL METHOD


statement.
Fundamentals of ABAP Objects | 11.01

March-2005

16

Constructors

Each class has one constructor. It is


a predefined, public instance method
of the class, with the name
CONSTRUCTOR (or
CLASS_CONSTRUCTOR for static
constructor).

METHODS constructor
IMPORTING
EXPORTING
CREATE OBJECT obj
EXPORTING

Instance
constructor

Static
Constructor
CLASS-METHOD
class_constructor

Constructors are special methods


that produce a defined initial state of
objects and classes.
Constructors are executed once for
each instance. They are called
automatically after you have created
an instance of the class with the
CREATE OBJECT statement.

Fundamentals of ABAP Objects | 11.01

March-2005

17

Some more features of


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

O CLASS class_name DEFINITION LOAD.


O If the first access to a global class in a program is to

its static components then explicit


O Loading of the class definition is necessary. In release
6.40 this statement is not required.
O CLASS class_name DEFINITION CREATE PUBLIC|

PROTECTED | PRIVATE.
O CREATE PUBLIC addition is provided automatically
by compiler if no create addition is used.
O The additions CREATE PROTECTED and CREATE
PRIVATE allow you to control the instantiation of
your class.
Fundamentals of ABAP Objects | 11.01

March-2005

18

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.

Classes are the templates of objects; actual


objects must be created and referenced to
be of use (except for the static components
of a class)
Reference variables (TYPE REF TO)
contain references to objects- Users can
only access instance objects through
reference variables.
To use objects:

Fundamentals of ABAP Objects | 11.01

Declare reference variables.

Create objects, assigning their

references.
Use the object components
March-2005

19

Self- Reference
CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA: int TYPE I VALUE 10.
METHODS display_int.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD display_int.
DATA : int TYPE I VALUE 20.
WRITE:/ int,
ME->int.
ENDMETHOD.
ENDCLASS.
DATA : oref TYPE REF TO c1.
CREATE OBJECT oref.
CALL METHOD oref->
display_int.
Fundamentals of ABAP Objects | 11.01

If an objects internally needs to


provide its own reference. For
example to another object, it can
use the local reference variable
ME.
ME is predefined and always
contains a reference to the address
of its own object.
Note : ME is equivalent to THIS
pointer in C++.

March-2005

20

Multiple instantiation
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION.

ENDCLASS.

Programs can instantiate


multiple objects of the same
class.

DATA: oref1 TYPE REF TO c1,


oref2 TYPE REF TO c1.
START-OF-SELECTION.
CREATE OBJECT oref1, oref1.

Fundamentals of ABAP Objects | 11.01

March-2005

21

Deleting Objects
oref1

DATA: oref1 TYPE REF TO c1,


oref2 TYPE REF TO c2.
...
CREATE OBJECT oref1, oref2.

oref2
oref1

Object C1

9999
oref2

8888
oref1

8888

Object C2

Object C1

oref1 = oref2.

oref2

CLEAR oref1.

oref1

Object C1

oref2

Object C2

oref1

Object C1

oref2

Object C2

8888

8888

CLEAR oref2.

22

Fundamentals of ABAP Objects | 11.01

Object C2

March-2005

Functional Methods
METHODS meth
IMPORTING
RETURNING VALUE (r)

Instead of CALL METHOD,


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

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

Fundamentals of ABAP Objects | 11.01

March-2005

23

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.
Fundamentals of ABAP Objects | 11.01

Pointer tables are used to


store multiple instances 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.
March-2005

24

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.

Fundamentals of ABAP Objects | 11.01

March-2005

25

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

Fundamentals of ABAP Objects | 11.01

March-2005

26

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

Fundamentals of ABAP Objects | 11.01

March-2005

27

Summary
O Features of Object oriented programming are:
O
O
O
O
O

Abstraction
Ecapsulation
Inheritance
Polymorphism
Code Reuse

O Classes and Objects are the basic building blocks of Object

Oriented Programming
O When a real world entity is modeled into OOP world then it is
known as Class, characteristics as attributes and
functionality as methods.
O Objects is an instance of a Class.
O Classes can be of two types:
O Global Class (Created using class builder (SE24) and stored

in class repository as Class pool)

O Local Class (Created in any ABAP program)

Fundamentals of ABAP Objects | 11.01

March-2005

28

Questions
O What is Object Oriented Programming ?
O What are the main advantages of Object Oriented

Programming over Procedural Programming ?


O What is a Class?
O What is an Object?
O Which transaction we use to maintain global class?
O What is a constructor?
O What are the various visibility sections present in a ABAP
class?
O What is the basic difference between static component and
instance component?
O Can we access the static component of a class by the object
name instead of the class name?
Fundamentals of ABAP Objects | 11.01

March-2005

29

Anda mungkin juga menyukai