You are on page 1of 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

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.

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

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.

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.

Fundamentals of ABAP Objects | 11.01

March-2005

2005 IBM Corporation

IBM Global Services

ABAP as Hybrid Language

Fundamentals of ABAP Objects | 11.01

March-2005

2005 IBM Corporation

IBM Global Services

ABAP as Hybrid Language (Contd.)

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

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 objects 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 characteristics of the box? (Attributes) 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

It can store things It can occupy space

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 Classes


Accessed from ?

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

Where store ? Tools required to create ? Namespace ?

In the class repository Class builder (SE24) Must begin with Y or 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 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} ENDCLASS.
13 Fundamentals of ABAP Objects | 11.01

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

2005 IBM Corporation

IBM Global Services

Components of Class ( Instance + Static )


Instance components:
DATA
For instance attributes

Instance components exist separately in each instance (object) of the class.

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

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

Event handler methods:


METHODS meth FOR EVENT evt OF class.

ENDCLASS.
CLASS c1 IMPLEMENTATION. METHOD do_something. ENDMETHOD. ENDCLASS.
16 Fundamentals of ABAP Objects | 11.01

This type of methods are written to trap events.

Methods are called with a CALL METHOD statement.


March-2005

2005 IBM Corporation

IBM Global Services

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.

CLASS-METHOD class_constructor Static Constructor

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. PUBLIC SECTION. DATA: int TYPE I VALUE 10. METHODS display_int. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD display_int. WRITE / int. ENDMETHOD.

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

Create objects, assigning their references.

Use the object components

March-2005

2005 IBM Corporation

IBM Global Services

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

Note : ME is equivalent to THIS pointer in C++.

March-2005

2005 IBM Corporation

IBM Global Services

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.

Programs can instantiate multiple objects of the same class.

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

Object C1 Object C2 Object C1

8888
oref1

oref2

8888
oref1

Object C2
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

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.

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

2005 IBM Corporation

IBM Global Services

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.

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

You might also like