Professional Documents
Culture Documents
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.
Jan-2007
Jan-2007
Function Data
Object
4 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
Functions of the box? (Methods) It can store things It can occupy space
Object 1
6 Fundamentals of ABAP Objects Jan-2007
Object 2
2007 IBM Corporation
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.
Jan-2007
Jan-2007
The object-oriented concept in ABAP is same as other modern object-oriented languages such as C++ or Java.
Jan-2007
10
Jan-2007
11
Jan-2007
12
Jan-2007
Tools required to create ? Class builder (SE24) Namespace ? Must begin with Y or Z
13
Jan-2007
Classes are template for Objects. This example declares and defines a local class test . The class DEFINITION belongs in the global declarations section at the beginning of the program. Class definition cannot be nested. Classes cannot be defined inside subroutines or function modules. A class definition declares :
Its components :
Jan-2007
METHODS
For instance methods
EVENTS
For instance events
Static components:
CLASS-DATA
For static attributes
CLASS-METHODS
For static methods
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 attributes of a class are retained throughout the entire runtime. 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.
CLASS-EVENTS
For static events
CONSTANTS
For constants
15 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
Instance
Static
16
Jan-2007
Notes: There is no default visibility section in a class. You should not make attributes public unless absolutely necessary.
17 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
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.
Attributes contains data that can be stored in the objects of a class. Attributes can be of 3 types: elementary, structured or table-type. In classes you can only use TYPE addition to refer to data types and use LIKE reference only for local data objects. The READ ONLY addition can be used for data declared in PUBLIC SECTION, which means the data can be read from outside, but can be changed only by methods of the same class.
18
Jan-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: ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD do_something. ENDMETHOD. ENDCLASS.
19 Fundamentals of ABAP Objects
Methods are the functionality of a class , ABAP code is 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.
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.
20
Jan-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
Constructor is a special method that is called by the runtime system as soon as the object is created with the CREATE OBJECT statement. Useful for:
Initializing data structures or setting default values to attributes dynamically. Sending message about object creation.
Each class has one constructor. It is a predefined with the name CONSTRUCTOR (or CLASS_CONSTRUCTOR for static constructor).
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.
A static constructor cant be called explicitly When exception is raised in an instance constructor, instances are not created, thus no memory space is occupied. Notes: There is no Destructor in ABAP Objects. I.e. there is no instance method automatically called immediately before the object is deleted.
22 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
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.
23
Jan-2007
Objects are real runtime replica of classes. Objects can only be created and addressed using reference variables. To use objects:
Declare reference variables with type
of the class.
Create objects, assigning their
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. Notes: DATA : oref TYPE REF TO c1. ME is equivalent to THIS pointer CREATE OBJECT oref. in C++. CALL METHOD oref-> display_int.
25 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
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.
26
Jan-2007
Deleting Objects
oref1 oref2 oref1
DATA: oref1 TYPE REF TO c1, oref2 TYPE REF TO c2. ... CREATE OBJECT: oref1, oref2. oref1 = oref2.
9999
oref2
Object of C1 Object of C2
8888
oref1
8888
oref2
Object of C1 Object of C2
8888
oref1
CLEAR oref1.
oref2 oref1
8888
CLEAR oref2.
27 Fundamentals of ABAP Objects
oref2
Jan-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)
28
Jan-2007
Functional Methods
METHODS meth IMPORTING RETURNING VALUE (r) CALL METHOD oref->meth EXPORTING i1 = a1.in = an RECEIVING r = a.
Conventional Method call
Instead of CALL METHOD, functional methods can be performed in expressions. A Functional method can have zero to many IMPORTING parameters, EXCEPTIONS and exactly one RETURNING parameter, that must be passed by value. A Functional method can be instance or static method. Operands can be replaced by functional methods
29
Jan-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. LOOP AT oref_tab INTO oref. CALL METHOD oref->meth. ENDLOOP.
30 Fundamentals of ABAP Objects
Pointer tables are used to store multiple instances/objects 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.
Jan-2007
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.
31
Jan-2007
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.
32
Jan-2007
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.
33
Jan-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)
34
Jan-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 ?
35 Fundamentals of ABAP Objects Jan-2007 2007 IBM Corporation
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 ?
36
Jan-2007
Hands-on Exercises
Exercise 1: Exercise 2:
37
Jan-2007