0% found this document useful (0 votes)
79 views42 pages

ABAP Introduction Course Training 1

The document provides an overview of object-oriented (OO) programming concepts in ABAP. Key points covered include: 1) Classes are templates that define attributes and methods, while objects are instances created from classes at runtime that can access those attributes and methods. 2) ABAP OO allows creating classes and using objects in reports, functions, and other programs. 3) Classes contain a definition part for attributes and methods and an implementation part for method code. Naming conventions help identify variables, objects, and other elements. 4) Attributes have properties like name, level, visibility, and typing that define how they can be accessed and used. Methods allow interacting with

Uploaded by

Thierry Kemp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views42 pages

ABAP Introduction Course Training 1

The document provides an overview of object-oriented (OO) programming concepts in ABAP. Key points covered include: 1) Classes are templates that define attributes and methods, while objects are instances created from classes at runtime that can access those attributes and methods. 2) ABAP OO allows creating classes and using objects in reports, functions, and other programs. 3) Classes contain a definition part for attributes and methods and an implementation part for method code. Naming conventions help identify variables, objects, and other elements. 4) Attributes have properties like name, level, visibility, and typing that define how they can be accessed and used. Methods allow interacting with

Uploaded by

Thierry Kemp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Workshop ABAP OO

Recapitulation day 1

Presented by Wim Verschueren

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

On the left side a normal procedural process is shown:


•Data is declared at the top of the program and is accessible by the rest of the
program
•The business functionality is created in the programs and functions

On the right side the OO process is shown:


•For every used object in the real world (car, people,...) a technical object is
created. Objects are abstractions of the real world.
•This technical object contains the data and the behaviour of the real world object
•The business functionality is created in the behaviour of the object

3
What is a Object?

Attributes Methods

An object is an abstraction of a real world


object
Objects are units made up of data and the
methods belonging to that data
Objects interact with eachother by using the
methods

The base of OO programming is an object.

An object is an instance that represents an object in the real world (employee,


product, ...). The object contains attributes to contain the data, and methods to
access/modify this data.

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

OO is completely based on 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

An object is an instance of a class


An object is created from a class in runtime
The object will have the attributes and methods
available of the class it was created from

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.

In runtime when an object is needed, an instance is created based on the class.


This object (=instance of the class) is used to do perform the functionality of the
program.

6
Classes and objects - example

A class Vehicle is created in design time

During the run time of the program, 3 objects


(instances) of the type “Vehicle” are created

Vehicle

3 objects are created in the program during run time:


•lr_bus
•lr_sportscar
•lr_familycar

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

An object is created as a reference variable


A reference variable is a variable that doesn’t
contain the data itself, but instead a pointer
(reference) to the memory where the data is
stored

This reference is of the type of the class from


which the object was created

Once an object is created based on a class


The methods defined in the class can be called
The attributes defined in the class can be
accessed

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 is available from release 4.6

ABAP OO classes and objects can be called


almost everywhere in SAP
Reports
Other ABAP OO classes
Module pools
Functions
BADI’s
...

As of SAP release 4.6, ABAP OO is available. Although extra functionalities (ex.


Unicode handling) and improvements to the development environment are added
with every new release.

ABAP OO classes and objects can be called and used in all existing programs.

10
ABAP OO syntax

ABAP is cleaned up while implementing the


OO-part
To make the language more compatible with OO
principles

Added some syntax-elements


Ex. CREATE OBJECT

Removed some syntax-elements:


Internal table with header line
Ranges
...

11
Used transactions

SE80 Object navigator


Create and modify classes
Only methods need to be coded, the definitions
can be configurated using the possibilities of the
tool

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.

Transaction SE24 is specifically designed for creating ABAP OO classes, but


with later releases this tool has become part of SE80. So this transaction is not
needed anymore.

With the ABAP Workbench (transaction SE38) it is not possible to create or


modify a class. But it is possible to define a local class with this tool. However,
local classes are used rarely in reality and therefore we are not going to use this
transaction in this workshop.

12
Naming conventions

Naming conventions for classes


Classes start with CL_
Local classes start with LCL_
Customer specific classes start with ZCL_
Interfaces start with IF_
Customer specific interfaces start with ZIF_

Naming conventions for variables and


objects
Objects start with R_ (LR_ and R_)
Variables start with V_ (LV_, IV_ and EV_)
Structures start with S_ (LS_, IS_ and ES_)
Tables start with T_ (LT_, IT_ and ET_)

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

Definition part contains:


the attributes and their properties
the properties and parameters of the methods

Implementation part contains:


the implementation of the method (actual code)

15
Attribute

An attribute in a class has the following


properties:
Name
Level (Instance, Static or Constant)
Visibility (Public, Private or Protected)
Read only
Typing (Like, Type,Type Ref To)
Associated type
Description
Initial value

Name: the name of the attribute, the naming conventions are not applied here

Level: see details on one of the next slides

Visibility: see details on one of the next slides

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

The possible levels:


Instance: attribute exists separately for each
object. This is the most common choise
Static: attribute exists independently from the
objects. Can always be accessed without creating
a object. A static attribute is also called a “class
attribute”.
Constant: is the same as static, but not
changeable. This can be used for application wide
constants

An example for a static attribute:


•If you want a counter for the number of objects that are created for a certain
class, you can define a counter as static attribute of the class
•Everytime an object of the class is created, you have to increase this counter

17
Attribute - Visibility

The possible visibilities:


Public: the attribute is accessible from all classes
and programs.
Private: the attribute is only accessible for
methods of the own class and will not be inherited
to the subclasses.
Protected: the attribute is only accessible for
methods of the own class but will also be
inherited to the subclasses.

18
Attribute - Typing

The possible typings:


Type: the same as type statement in normal ABAP
Like: the same as like statement in normal ABAP
Type Ref To: when the attribute is an object the
type ref to (reference variable) typing must be
used

The possible associated types:


For type and like: any possible data type (C, N,
string, structures, tables, dictionary types,...)
For type ref to: only classes

Ex. Class cl_car can have an attribute “engine type ref to cl_engine”

19
Method

A method exists out of a definition and a


implementation

A method in a class has the following


properties:
Name
Level (Instance and static)
Visibility (Public, private and protected)

A method can have parameters

Can raise exceptions

Name: the name of the method, the naming conventions are not applied here

Parameters: import, export, changing and returning parameters.

20
Method - Level

The possible levels:


Instance:
are methods that can only be called for an
object.
They can use the static attributes of the class
as well as the instance attributes of the object
Static:
are methods that can only be called using the
class, and not using an object
They can only use the static attributes of the
class
A static method is also called “class method”

Instance: An object should be created of the class before this method is


accessible. An

21
Method - Visibility

The possible visibilities:


Public: the method can be called from all classes
and programs.
Private: the method can only be called from
methods of the own class. The method will neither
be inherited to the subclasses.
Protected: the method can only be called from
methods of the own class. But it can be inherited
to the subclasses.

22
Method - Parameters

A method has a parameter interface, called


“signature” that can contain multiple
parameters

A parameter has the properties:


Name
Type (Importing, exporting, changing and
returning)
Pass by (value and reference)
Optional
Typing
Default value

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

A method also has Exception parameters

You can choose between 2 exception


alternatives:
Classic exceptions
Like function modules
Sy-subrc
Simple but less powerfull
Exception classes
More work but more powerfull

The exception raising will be handled further on in this work shop.

24
Encapsulation

Encapsulation is a principle that is highly


recommended to be used

It is a way of extra data protection that is


generally used

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.

How does it work:


•The attribute is not visible by other objects
•Other objects can’t change the attribute in any way they want
•A set- and get-method is defined for every attribute. The set-method accepts a
value and puts it in the attribute and the get-method takes the value from the
attribute and returns it
•To protect the data, extra data checks can be build in the get- and set-methods

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

To create an object of a class in a method or


a program
Declare the reference variable
DATA: r_car1 TYPE REF TO zcl_vehicle.

Create the object


CREATE OBJECT r_car1.

To create an instance (=object) of a class in run time, the following statements


should be executed:
•Declaration of a reference variable that will be used to point to the object. This
reference variable must be declared with the “TYPE REF TO” and with the type
of the class of which the object will be created.
from that moment on there is a reference variable
(ex. r_car1) in the memory, but this reference variable is still empty
•The second step is the creation of the object itself. With the statement CREATE
OBJECT an object is created of the type of the reference variable
from that moment on the reference variable will point to
the actual object

27
Check reference variable

To check if a reference variable points to a


valid object
Is bound-check:
IF r_car1 IS BOUND.

This check can prevent shortdumps

28
Reference attributes to 1 object

It is possible to create multiple reference


variables who point to the same object

After creating the 2 objects r_vehicle1 and r_vehicle2, both reference variables
point to a different object.

When the value of reference variable 1 is copied to the reference variable 2, it is


in fact the pointer that is copied. So the pointer that points to object 1 is put in
reference variable 2, with the result that both reference variables point to object 1
and no reference variables point to object 2.

But of course the reference variables must be of the same type to let this work.

In this example, the second CREATE OBJECT statement is unneeded.

29
Garbage collector

The garbage collector is the mechanism that


removes all unneeded objects from the
memory

A object is seen as unneeded when no


reference variables point to it (the object is
not referenced anymore)

You can push this situation by using the


clear- or free-statement
CLEAR r_vehicle1.
FREE r_vehicle1.

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.

When a CLEAR-statement is executed on a reference variable, not the object is


deleted but the pointer to the object is cleared. So the object becomes
unreferenced, and will be cleared from memory the next time the garbage
collector is running.

30
Constructor

When creating an object, all attributes of the


object will be empty or will contain the
default value

The constructor is a method that


automatically is executed when an object is
created
In this method you can define actions,
initializations,... that have to be executed when
the object is created

You can define a constructor by creating a method with the name


“CONSTRUCTOR” in the class. Only 1 constructor is allowed in a class.

This method will be executed when the CREATE OBJECT-statement is


performed for the object.

31
Constructor with parameters

A constructor can have Importing- and


exception-parameters

When an exception is raised within the


constructor, the object is not created

In the example of the picture:


•A constructor method is available in the class. This constructor method will put
the values from the input parameters im_make and im_model into the attributes
make and model of the object.
•The static attribute n_o_vehicles of the class will also be incremented by 1

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

It is possible to access the public attributes


of an object from a method or program

Content of attributes can be read/changed

They can be accessed using the “->” arrow


sign (object component selector)

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

It is possible to access the public static


attributes of a class from a method or
program

They can be accessed using the “=>” double


arrow sign

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

It is possible to call the public methods of an


object from a method or program

They can be called using the “->” arrow sign

The object->methodname( parameters ) statement can be used to call a method


of an object. In this example the method motor_on of the object r_vehicle is
called with no parameters.

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

It is possible to call the public static methods


of a class from a method or program

They can be called using the “=>” double


arrow sign

The class=>methodname( parameters ) statement can be used to call a static


method (=class method) of the class. In this example the method get_count of
the class lcl_vehicle is called with one parameter.

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

The parameters are put between the brackets


after the method name

When the signature of the method has only


parameters of the same type (importing, ...),
you don’t need to mention the type

When the signature of the method has just 1


parameters, you don’t need to mention the
atrtibute name and type

An example with multiple parameters:

r_vehicle->get_info( IMPORTING ex_make = make_name


ex_name = name
EXPORTING ... ).

38
Functional methods

Functional methods are methods that


generate 1 result which can be used directly
in a statement

Requirements:
Exactly 1 returning parameter
Only importing and exception parameters are
possible

Functional methods can be used in:


•Logical expressions (if, elseif, while, check, wait)
•Case statement (case, when)
•Loop statement
•Arithmetic and bit expressions (compute, ...)
•Move statement

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

Declaration of the table

Adding an object to the table

Using the table

41
Table as method parameter

In a method you can’t directly select a parameter to


be a table

You can create a table type and use this type for
your parameter (2 alternatives)

Define the table type locally in the class

Define the table type in data dictionary

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.

When you want to define a table-parameter for a public method, it is highly


recommended to create a table type in the data dictionary.

42

You might also like