Professional Documents
Culture Documents
March-2005
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
March-2005
March-2005
March-2005
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.
March-2005
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.
March-2005
March-2005
March-2005
March-2005
10
March-2005
Global Classes
Any Program
Local Classes
Only with in the Program where it is defined In the program where it is defined With ABAP editor (SE38) Any
12
March-2005
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.
March-2005
METHODS
For instance methods
EVENTS
For instance events
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.
Static components:
CLASS-DATA
For static attributes
CLASS-METHODS
For static methods
CLASS-EVENTS
For static events
CONSTANTS
For constants
14 Fundamentals of ABAP Objects | 11.01 March-2005
15
March-2005
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.
e.g. METHODS meth.
ENDCLASS.
CLASS c1 IMPLEMENTATION. METHOD do_something. ENDMETHOD. ENDCLASS.
16 Fundamentals of ABAP Objects | 11.01
Constructors
METHODS constructor
IMPORTING
EXPORTING CREATE OBJECT obj EXPORTING Instance constructor
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). 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.
17
March-2005
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
March-2005
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:
Declare reference variables.
ENDCLASS.
DATA : oref TYPE REF TO c1. START-OF-SELECTION. CREATE OBJECT oref. WRITE / oref-> int. CALL METHOD oref-> display_int.
19 Fundamentals of ABAP Objects | 11.01
March-2005
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.
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.
ENDMETHOD.
ENDCLASS. DATA : oref TYPE REF TO c1. CREATE OBJECT oref. CALL METHOD oref-> display_int.
20 Fundamentals of ABAP Objects | 11.01
March-2005
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, oref1.
21
March-2005
Deleting Objects
oref1 oref2 oref1 Object C1 Object C2
DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c2. ... CREATE OBJECT oref1, oref2. oref1 = oref2. CLEAR oref1.
9999
oref2
8888
oref1
8888
oref2
8888
oref1
oref2
8888
oref1
Object C2
Object C1
CLEAR oref2.
oref2
Object C2
22
March-2005
Functional Methods
METHODS meth
IMPORTING
RETURNING VALUE (r) CALL METHOD oref->meth EXPORTING i1 = a1.in = an RECEIVING r = a. oref->meth() oref->meth(a) Method call specific to Functional method Conventional Method call
Instead of CALL METHOD, functional methods can be performed in expressions. A Functional method can have zero to many IMPORTING parameters and exactly one RETURNING parameter, that must be passed by value. A Functional method can be instance method or it can be static method.
March-2005
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.
24 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
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.
25
March-2005
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
March-2005
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
March-2005
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
March-2005
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?
29
March-2005