Chapter 01 Fundamentals of Abap Objects
Chapter 01 Fundamentals of Abap Objects
Objectives
Data and Functions are kept separate. Global variables of program contains data,
while subroutine contains functions.
Usually non-encapsulated access to data. (exception: Global data of Function
Group is only accessible by Function Modules within that group).
Program maintenance as well as management becomes difficult as program size
increases.
Very difficult to model real word entity.
Not possible to create several runtime instances easily.
Data Function
Data Function
Data Function
Function
Data
Object
4 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation
IBM Global Services
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 are instances of a Class.
Example :
Functions of the box? (Methods)
What are the
It can store things
characteristics of the
box? (Attributes) It can occupy space
Function: Function:
Data RETRIEVE_D Data
GET_DETAILS
ETAILS
Object 1 Object 2
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.
A class is a template/blueprint based on which all objects of the class are created. Class does
not occupies any memory space during program execution only the instance of an class
(objects) occupies memory space.
In ABAP, 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 in global declarations section and only accessible to
the programs within the main program.)
A class declaration has two parts. Classes are template for Objects.
Definition
This example declares and defines a
Implementation
local class “test ”.
The class DEFINITION belongs in
CLASS test DEFINITION. the global declarations section at the
PUBLIC SECTION. beginning of the program.
{ Attributes, Methods, Events }
Class definition cannot be nested.
PROTECTED SECTION.
{ Attributes, Methods, Events } Classes cannot be defined inside
PRIVATE SECTION. subroutines or function modules.
{ Attributes, Methods, Events } A class definition declares :
ENDCLASS. Its components :
Attributes, Methods, Events.
CLASS test IMPLEMENTATION.
The visibility of its components :
<class body>
Public, Protected and Private.
{Method implementation is done here}
ENDCLASS.
14 Fundamentals of ABAP Objects Jan-2007 © 2007 IBM Corporation
IBM Global Services
The class components that share a common memory area for all the class
instance are static components.
The class components that have separate memory area for separate instance are
instance components.
Instance Static
Notes:
There is no default visibility section in a class.
You should not make attributes public unless absolutely necessary.
Attributes
Methods
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.
Constructors
Constructors (Contd.)
Notes:
There is no Destructor in ABAP Objects. I.e. there is no instance method
automatically called immediately before the object is deleted.
Self- Reference
Multiple instantiation
Deleting Objects
… oref1
oref1
9999 Object of C1
CLEAR oref2. oref2
Object of C2
8888
Calling Methods
Functional Methods
Pointer tables
DATA: oref1 TYPE REF TO c1, Pointer tables are used to store
oref2 TYPE REF TO c1, multiple instances/objects of
oref3 TYPE REF TO c1. same class. This method
reduces coding and more
DATA: oref TYPE REF TO c1, elegant against creating
oref_tab TYPE TABLE OF REF TO c1. separate, separate object
reference variables for storing
START-OF-SELECTION. every objects of the same class.
… Reference variables are
DO 3 TIMES. handled like any other data
CREATE OBJECT oref. object with an elementary data
APPEND oref TO oref_tab. type.
ENDDO.
…
LOOP AT oref_tab INTO oref.
CALL METHOD oref->meth.
ENDLOOP.
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.
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.
Summary
Questions
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 ?
Hands-on Exercises
Exercise 1:
Exercise 2: