Fundamentals of ABAP Objects
Embed Size (px)
DESCRIPTION
Fundamentals of ABAP Objects
Citation preview
ABAP Objects - BasicsIBM Global Services
© 2005 IBM Corporation
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
IBM Global Services
© 2005 IBM Corporation
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
The fundamental idea behind Object Oriented Programming 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”.
Whereas in procedural programming programs are typically organized
around code. This approach can be thought of as “Code acting on
data“.
IBM Global Services
© 2005 IBM Corporation
Better Programming Structure
Stress on data security and access
Data encapsulation and abstraction
Reduction in code redundancy
IBM Global Services
© 2005 IBM Corporation
Abstraction
Modeling real world entities and processes in a more natural
way.
Ecapsulation
Hiding data and its related logic behind well defined
interfaces.
Inheritance
Polymorphism
Code Reuse
Same code can be reused multiple times by using inheritance.
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
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
SAP Basis Release 4.5 delivered the first version of ABAP Objects
by extending the ABAP programming language with classes, interfaces
and the ability to create objects from classes i.e. class
‘instances’.
SAP Basis Release 4.6 delivered the complete version of ABAP
Objects by enabling “Inheritance” which is the key feature of
Object-oriented programming for many programmers, and allowing the
creation of a compose interface from several component
interface.
SAP Web Application server 6.10/6.20 , the successor to SAP Basis,
enhanced ABAP Objects with features like “ friendship” between
classes and the introduction of Object Services for storing objects
in the database.
IBM Global Services
© 2005 IBM Corporation
Fundamentals of ABAP Objects | 11.01
The ABAP runtime support for both the procedural and ABAP Objects
programming models enables you to use ABAP Objects classes in any
existing ABAP program i.e. in executable programs, module pools,
function pools. This enables you to use new techniques based on
ABAP Objects, such as new user interfaces, with out the need to
completely rewrite your existing code.
ABAP Objects is an extension of the existing ABAP programming
language.
ABAP Objects is designed to be downward-compatible.
In procedural programming ( see the upper part of the diagram
above), execution always starts in a dialog module or an event
block like START-OF-SELECTION, for example . You work with your
program’s global data inside these processing blocks. You can
modularize your programs internally with Subroutines or externally
by calling Function Modules as external procedures. In addition to
accessing the global data of your program, these procedures can
contain temporary local variables that assist with the procedure’s
internal activities.
IBM Global Services
© 2005 IBM Corporation
ABAP as Hybrid Language (Contd.)
Fundamentals of ABAP Objects | 11.01
In OO programming ( see the bottom part of the diagram above), the
only structuring units are classes. Instead of working with global
data , you work with “objects”, which are instances of classes.
These objects encapsulate the state and behavior of self-contained
parts of the application. The application state is presented by
“attributes” that supersede the global variables used in the
procedural approach. The application behavior is determined by
methods that modify the object’s attributes or call methods of
other objects.
Although a pure OO world is technically possible, most real-world
implementations use a mixture of procedural ABAP and ABAP Objects.
ABAP Objects is used in parallel with procedural techniques, and
non-OO techniques, such as calling classical procedures or screens,
or direct database access. This hybrid programming model of SAP Web
Application Server allows you to take advantage of enhance SAP
technology.
IBM Global Services
© 2005 IBM Corporation
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 object’s 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
ABAP Objects establishes an advanced level of data encapsulation
that improves the maintainability and stability of your programs.
In procedural programming, a program’s global data area can
encompasses a large number of disparate data objects that end up
mixed together in a single memory area, where one function might
change the application state in a way other functions do not
expect. But in OO programming the state is kept in ‘objects’ that
separate externally and internally visible data and
implementations, ensuring that functions (methods in OO context)
work only with the data they should, attributes defining an
object’s state remain intact, and applications using that object
continue to work correctly.
ABAP Objects provides you with the ability to instantiate multiple
instances of a single class each with its own variation of
characteristics defined in the class, and to use an object’s own
functions (methods) to change its state. Together with the ABAP
Objects automatic garbage collector, this means that developers do
not have to deal with manually associating data and functionality
for each and every object, or explicitly controlling the lifetime
of objects. But in Procedural programming, which does not support
multiple instantiation, data and functions are separate. You work
with stateless functions that have to be initialized via their
parameter interface with every call, and then manually cleared from
memory.
ABAP Objects greatly enhances code reuse through “inheritance”
which is a key property of the OO programming paradigm. With
“inheritance” you can derive some or all of the characteristics
from a general procedure (method in OO context) into various
specialized procedures simply by programming the difference between
two, which results in smaller procedures, and in turn improves
maintainability. But in Procedural programming, you must call
existing procedures ( subroutines or function modules) as they are
or implement entirely new ones.
IBM Global Services
© 2005 IBM Corporation
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 object’s 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
ABAP Objects enables you to work with an object’s business logic
through a standalone interface, rather than working with it
directly, which relieves you from having to know exactly how a
certain functionality is implemented. It also enables you to later
alter an implementation with out modifying the coding that uses the
interface, so that modifications of standard code become less
problematic. But Procedural programming does not support the
concept of standalone interfaces as a point of contact to an
object- the interface concept is basically restricted to parameter
interfaces of procedures ( subroutines or function modules )
ABAP Objects makes it easy to incorporate event-driven programming
models. Applications can be loosely coupled through a ‘publish and
subscribe’ model, where a caller (the event) is not statically
bound to the called procedure (the event handler). This provides
greater flexibility than the procedural approach, where the
coupling is much tighter and the flow of program control is more
predetermined.
ABAP Objects is more explicit, and therefore simpler to use.
ABAP Objects offers cleaner syntax and semantic rules. Example
obsolete and error-prone language concepts are explicitly forbidden
inside ABAP Objects classes. In contrast , you can still use them
in Procedural ABAP. In which case the syntax check presents you
with a warning in some critical cases, nothing more.
ABAP Objects is the only way you can use new ABAP technology. For
example all new GUI concepts, such as the SAP Control Framework
(CFW) and Business Server Pages (BSP), are encapsulated in ABAP
Objects classes. With procedural ABAP, you are stuck with classical
screen and list processing.
IBM Global Services
© 2005 IBM Corporation
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 :
Inside color is blue
The box is semi open
Functions of the box? (Methods)
It can store things
It can occupy space
IBM Global Services
© 2005 IBM Corporation
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
Where store ?
Tools required to create ?
Any
Fundamentals of ABAP Objects | 11.01
Global classes are repository objects and accessible to any ABAP
program.
Local classes declared within the global declarations of a program
are only accessible to the programs within the main program.
IBM Global Services
© 2005 IBM Corporation
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 :
Public, Protected and Private.
Definition
Implementation
ENDCLASS.
Classes cannot be defined inside subroutines or function
modules.
A class definition declares :
The visibility of its components: Public, Protected and
Private.
The class DEFINITION belongs in the global declarations section at
the beginning of the program. The class IMPLEMENTATION processing
block need not be at the top of the program.
IBM Global Services
© 2005 IBM Corporation
Instance components:
For constants
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 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.
Fundamentals of ABAP Objects | 11.01
Static components can be used without even creating an instance of
the class ( Example : Class name => static components )
Constants cannot be modified during the compile time or run time
and hence are somewhat like static attributes.
Unlike instance methods, which can access both instance and static
attributes, and trigger both instance and static events, static
methods can only access static attributes.
IBM Global Services
© 2005 IBM Corporation
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.
Fundamentals of ABAP Objects | 11.01
All components of a class must belong to a visibility section.
Components can be Public, Protected or Private.
The declaration part of the class is divided accordingly into up to
three sections:
PUBLIC SECTION
All components defined in this section are visible in the
subsequent protected and private sections. These are also
externally visible.
PROTECTED SECTION
All components in this section can access the components from the
public section. In turn, they can be accessed by the components of
the private section. These components are externally visible only
to the inherited class.
PRIVATE SECTION
All components of this section can access the components of the
public and private sections. They are not externally visible.
There is no default visibility section in a class.
The principle of Encapsulation is that the class should hide all
unnecessary detail from the users of the class. Adhering to this
principle, you should not make attributes public unless absolutely
necessary. Since a public attribute can be changed by any user, use
of public attribute can easily place the object of a class into an
inconsistent state.
IBM Global Services
© 2005 IBM Corporation
ENDMETHOD.
ENDCLASS.
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.
trap events.
Fundamentals of ABAP Objects | 11.01
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.
Additions for standard methods:
3. …RETURNING VALUE (P) : Method that returns values
4. …CHANGING : Method that changes values that is passed
5. ... EXCEPTIONS p1 ... : Methods that raise exceptions
6. ... ABSTRACT : Abstract method
7. ... FINAL : This method cannot be inherited
8. … REDEFINITION : When super class methods is redefine in sub
class
IBM Global Services
© 2005 IBM Corporation
CLASS-METHOD class_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.
Instance constructor
Static Constructor
Fundamentals of ABAP Objects | 11.01
You can define a special method, called a Constructor, which will
execute automatically whenever an object is created. This is useful
when you need to initialize data structures or set default values
that require calculation
Constructors are special methods that produce a defined initial
state of objects and classes. The state of an object is determined
by its instance attributes and static attributes. You can assign
contents to attributes using the VALUE addition in the DATA
statement. Constructors are necessary when you want to set the
initial state of an object dynamically.
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. A constructor cannot be called with
the CALL METHOD statement.
To define a Constructor, simply create a method called
‘Constructor’ (a reserved word in ABAP).
The Instance Constructor method has no EXPORTING parameters because
its sole purpose is to initializing the object.
The Static Constructor method has no interface parameters.
IBM Global Services
© 2005 IBM Corporation
CLASS class_name DEFINITION DEFERRED.
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.
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
METHODS display_int.
START-OF-SELECTION.
CALL METHOD oref-> display_int.
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.
Fundamentals of ABAP Objects | 11.01
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.
Use the object components
The operator ‘->’ is the object component selector.
To access static components of a class use the operator ‘=>’
(class component selector ) with the class name.
Example : (class_name => comp )
IBM Global Services
© 2005 IBM Corporation
METHODS display_int.
WRITE:/ int,
ME->int.
CREATE OBJECT oref.
CALL METHOD oref-> display_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.
Note : “ME” is equivalent to “THIS” pointer in C++.
Fundamentals of ABAP Objects | 11.01
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.
Note: “ME” is equivalent to “THIS” pointer in C++.
IBM Global Services
© 2005 IBM Corporation
oref2 TYPE REF TO c1.
START-OF-SELECTION.
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
oref2 TYPE REF TO c2.
...
Fundamentals of ABAP Objects | 11.01
CREATE statement creates an anonymous object of the class that the
object reference variable refers and assign the address of that
anonymous object to the object reference variable.
The assignment of oref1 to oref2, as we saw leaves oref1 and oref2
pointing to the same object and no reference variable any longer
pointing to the object created with reference to c1. That latter
object is considered deleted and will be removed from memory by the
run time garbage collector.
CLEAR oref1 initializes the contents of oref1, with the similar
affect of deleting the object created with reference to C2
(actually not until you also CLEAR oref2, since oref2 is now also
pointing to the object of type C2).
IBM Global Services
© 2005 IBM Corporation
RECEIVING r = a.
e.g.,
var = oref-> meth( i1 = a1….in = an).
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.
Conventional
Fundamentals of ABAP Objects | 11.01
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.
Example of static functional method:
max = CL_ABAP_EXCEPTIONAL_VALUES=>get_max_value(in = …).
min = CL_ABAP_EXCEPTIONAL_VALUES=>get_min_value(in = …).
Source data objects for the MOVE statement
Logical expressions
WHERE condition in LOOP AT
Note : In ABAP Objects all parameters for methods must be fully
typed.
IBM Global Services
© 2005 IBM Corporation
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.
CALL METHOD oref->meth.
ENDLOOP.
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.
Fundamentals of ABAP Objects | 11.01
Pointer tables are used to store multiple instances of same class.
This method reduces coding and this method is 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.
IBM Global Services
© 2005 IBM Corporation
Dynamic Method calls
CLASS c1 DEFINITION.
…
…
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)
A method’s parameters can be passed dynamically using
PARAMETER-TABLE
and EXCEPTION-TABLE additions to the CALL METHOD statement.
Fundamentals of ABAP Objects | 11.01
Syntax for passing parameters dynamically is as follows:
CALL METHOD meth
PARAMETER-TABLE ptab
EXCEPTION-TABLE etab.
Internal tables ptab and etab have specific row and table types.
See the type group ABAP in the ABAP Dictionary, and the line type
ABAP_PARMBIND and the table type ABAP_PARMBIND_TAB.
When assigning the method name to the variable use uppercase name
rather than lowercase name.
IBM Global Services
© 2005 IBM Corporation
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.
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
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.
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
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)
Fundamentals of ABAP Objects | 11.01
IBM Global Services
© 2005 IBM Corporation
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?
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?
Fundamentals of ABAP Objects | 11.01