ABAP Objects
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
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
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.
March-2005
March-2005
ABAP as Hybrid
Language
March-2005
March-2005
March-2005
March-2005
10
March-2005
Global Classes
Local Classes
Accessed from ?
Any Program
Where store ?
Tools required to
create ?
Namespace ?
Any
12
March-2005
13
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
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
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:
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD do_something.
ENDMETHOD.
ENDCLASS.
March-2005
16
Constructors
METHODS constructor
IMPORTING
EXPORTING
CREATE OBJECT obj
EXPORTING
Instance
constructor
Static
Constructor
CLASS-METHOD
class_constructor
March-2005
17
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
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.
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
March-2005
20
Multiple instantiation
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
ENDCLASS.
March-2005
21
Deleting Objects
oref1
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
Object C2
March-2005
Functional Methods
METHODS meth
IMPORTING
RETURNING VALUE (r)
e.g.,
var = oref-> meth( i1 =
a1.in = an).
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.
24
Do something to assign
meth1 or meth2 to fld at
runtime.
fld = METH1 or
METH2.
CALL METHOD oref->(fld).
March-2005
25
Demonstration
OCreating a local class with different
March-2005
26
Practice
OCreating a local class with different
March-2005
27
Summary
O Features of Object oriented programming are:
O
O
O
O
O
Abstraction
Ecapsulation
Inheritance
Polymorphism
Code Reuse
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
March-2005
28
Questions
O What is Object Oriented Programming ?
O What are the main advantages of Object Oriented
March-2005
29