ABAP Introduction Course Training 1
ABAP Introduction Course Training 1
Recapitulation day 1
1
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
2
Procedural vs. OO programming
3
What is a Object?
Attributes Methods
4
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
5
Classes and Objects
A class is a template
The class is a blueprint that is described in design
time
The class contains the definition of the attributes
The class contains the definition and
implementation of the methods
A class is a template (blueprint) where all attributes and methods are defined in.
A class is created by a developer in design time. During run time, a class is only
used to create an object from.
6
Classes and objects - example
Vehicle
They are all 3 created as an instance of the class Vehicle, so they have all the
same attributes and methods.
7
The creation of an object
When a normal variable is used, this variable is in fact preserved space in the
memory. The variable contains the data itself.
A reference variable is also a space in the memory, but in this variable there is no
data present, only a memory pointer to another memory space where the actual
data is stored.
The reference variable is of the type of the class. This means that the program
can use the methods, attributes of the class because they are defined in the
reference variable.
8
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
9
Usability of ABAP OO
ABAP OO classes and objects can be called and used in all existing programs.
10
ABAP OO syntax
11
Used transactions
Other possibilities
SE24 Class builder (already integrated into SE80)
SE38 ABAP Workbench (only usable for local
classes)
Transaction SE80 is already used for all kinds of development: reports, module
pools, function groups,... But now it is also possible to create classes in it using a
user friendly graphical interface. This is the tool that we will use further in this
workshop.
12
Naming conventions
Naming conventions are very handy to spot with one quick look that a variable is
an object, a normal variable, a structure or a table.
It is useful to use the following extra naming conventions for variables and
objects, to spot that it is a parameter or local defined:
•When the variable or object is created within the method self, add the local sign
“L” before the name (ex. LR_, LV_, ...)
•When the variable or object is received in the method as import parameter, add
the import sign “I” before the name (ex. IR_, IV_, ...)
•When the variable or object is return from the method as export parameter, add
the export sign “E” before the name (ex. ER_, EV_, ...)
13
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
14
Components of a class
Created in SE80
15
Attribute
Name: the name of the attribute, the naming conventions are not applied here
Read only: the attribute can only be read and not be changed from outside the
object
Typing & associated type: see details on one of the next slides
Initial value: the attribute will be automatically filled with the given value when the
object is created
16
Attribute - Level
17
Attribute - Visibility
18
Attribute - Typing
Ex. Class cl_car can have an attribute “engine type ref to cl_engine”
19
Method
Name: the name of the method, the naming conventions are not applied here
20
Method - Level
21
Method - Visibility
22
Method - Parameters
The property name: when looking at the naming convention a import parameter will normally start
with I..., a export parameter with E...
Pass by:
•When a parameter is passed by value, it means that if the value of the parameter changes within
the method, that the parameter in the calling object will not change
•When a parameter is passed by reference, it means that changes done to the value of the
parameter within the method are reflected to the parameter in the calling object
Type:
•A method can have maximum 1 parameter with the type returning. This parameter is used as
return code of the method. Returning parameters can only be passed by value.
•An importing parameter that is passed by reference can not be changed within the method.
Therefore you need to use the type changing.
Optional: when this property is checked it is not obligated to pass the parameter when calling the
method
Default value: when the parameter is not passed, the given value will be automatically filled in
23
Method - Exceptions
24
Encapsulation
What is it:
Put the visibility of all attributes to protected or
private
Give access to these attributes with methods
In the above picture the attributes speed and category are both protected with
encapsulation, the attribute color not.
25
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
26
Creation of an object
27
Check reference variable
28
Reference attributes to 1 object
After creating the 2 objects r_vehicle1 and r_vehicle2, both reference variables
point to a different object.
But of course the reference variables must be of the same type to let this work.
29
Garbage collector
The garbage collector is a mechanism that automatically scans the memory from
time to time. When it encounters an object that is unreferenced, it clears the
object from the memory.
30
Constructor
31
Constructor with parameters
32
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
33
Accessing object-attributes
In this example an object r_vehicle of the class vehicle is created, this object has
a public attribute “make”. The “object->attribute” points to this attribute.
If you want to access an attribute from the same object as the method, it is not
needed to use “object->”.
34
Accessing static attributes
In this example the class lcl_vehicle has a static attribute (= class attribute)
“n_o_vehicles”. The “class=>attribute” points to this attribute.
If you want to access an attribute from the same class as the method, it is not
needed to use “class=>”.
35
Calling object methods
If you want to call a method from the same object as the method, it is not needed
to use “object->”.
This notation is available since release 4.6C. Before this release a method could
be called using the statement “CALL METHOD object->methodname”.
36
Calling static methods
If you want to call a method from the same class as the method, it is not needed
to use “class=>”.
This notation is available since release 4.6C. Before this release a method could
be called using the statement “CALL METHOD class=>methodname”.
37
Method call with parameters
38
Functional methods
Requirements:
Exactly 1 returning parameter
Only importing and exception parameters are
possible
39
Topics
Introduction to OO
Classes and objects
Introduction to ABAP OO
Classes in ABAP OO
Objects in ABAP OO
Addressing attributes and methods
Internal table with objects
40
Objects in an internal table
41
Table as method parameter
You can create a table type and use this type for
your parameter (2 alternatives)
Local types are only usable within the class itself, so this may be an option when
you are defining a parameter in a private/protected method.
42