OOABAP Theory
OOABAP Theory
In the past, information systems used to be defined primarily by their functionality: Data
and functions were kept separate and linked together by means of input and output
relations.
ABAP Objects is a complete set of object-oriented statements that has been introduced
into the ABAP language. This object-oriented extension of ABAP builds on the existing
language, and is fully compatible with it. You can use ABAP Objects in existing
programs, and can also use "conventional" ABAP in new ABAP Objects programs.
Kalyan trainerkalyan@gmail.com
Comparison between Procedural and Object Oriented Programming
Kalyan trainerkalyan@gmail.com
The main difference between real object orientation and function groups is that although
a program can work with the instances of several function groups at the same time, it
cannot work with several instances of a single function group. Suppose a program wanted
to use several independent counters, or process several orders at the same time. In this
case, you would have to adapt the function group to include instance administration, for
example, by using numbers to differentiate between the instances.
Classes: Class is a prototype that defines a real life entity which has both data and the
behavior common to all the objects of certain kind. Classes describe the real time objects.
Classes in ABAP Objects can be declared either globally or locally. You define global
classes and interfaces in the Class Builder (Transaction SE24) in the ABAP
Workbench.There are, however, a significant difference in the way that local and global
classes are designed. If you are defining a local class that is only used in a single
program, it is usually sufficient to define the outwardly visible components so that it fits
into that program. Global classes, on the other hand, must be able to be used anywhere.
Objects: objects are runtime instances of a class. In theory, you can create any number of
objects based on a single class. Each instance (object) of a class has a unique identity and
its own set of values for its attributes. They form a capsule which combines the character
to the respective behavior. Objects should enable programmers to map a real problem and
its proposed software solution on a one-to-one basis.
Object References: In a program, you identify and address objects using unique object
references. Object references allow you to access the attributes and methods of an object.
Kalyan trainerkalyan@gmail.com
Local and Global Classes
Global Class: Global classes and interfaces are defined in the Class Builder (Transaction
SE24) in the ABAP Workbench. They are stored centrally in class pools in the class
library in the R/3 Repository. All of the ABAP programs in an R/3 System can access the
global classes
Local Class: Local classes are define in an ABAP program (Transaction SE38) and can
only be used in the program in which they are defined.
Local Classes
Definition: This section is used to declare the components of the classes such as
attributes, methods, events .They are enclosed in the ABAP statements CLASS ...
ENDCLASS.
Implementation: This section of a class contains the implementation of all methods of the
class. The implementation part of a local class is a processing block.
Kalyan trainerkalyan@gmail.com
Structure of a Class
• Attributes: Attributes are internal data fields within a class that can have any ABAP
data type.
Instance Attributes: The contents of instance attributes define the instance-specific state
of an object. You declare them using the DATA statement.
Static Attributes: The contents of static attributes define the state of the class that is valid
for all instances of the class. Static attributes exist once for each class. You declare them
using the CLASS-DATA statement. They are accessible for the entire runtime of the
class.
• Methods:- Methods are internal procedures in a class that define the behavior of an
object. They can access all of the attributes of a class. This allows them to change the
data content of an object. They also have a parameter interface, with which users can
supply them with values when calling them, and receive values back from them.
METHOD <meth>.
...
ENDMETHOD.
You can declare local data types and objects in methods in the same way as in other
ABAP procedures (subroutines and function modules). You call methods using the
CALL METHOD statement.
Instance Methods
You declare instance methods using the METHODS statement. They can access all of the
attributes of a class, and can trigger all of the events of the class.
Static Methods
You declare static methods using the CLASS-METHODS statement. They can only
access static attributes and trigger static events.
Special Methods
Kalyan trainerkalyan@gmail.com
As well as normal methods, which you call using CALL METHOD, there are two special
methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are
automatically called when you create an object (CONSTRUCTOR) or when you first
access the components of a class (CLASS_CONSTRUCTOR).
Declaring Methods
You can declare methods in the declaration part of a class or in an interface. To declare
instance methods, use the following statement:
CLASS-METHODS <meth>...
When you declare a method, you also define its parameter interface using the additions
IMPORTING, EXPORTING, CHANGING, and RETURNING. The additions define the
input, output, and input/output parameters, and the return code. They also define the
attributes of the interface parameters, namely whether a parameter is to be passed by
reference or value (VALUE), its type (TYPE), and whether it is optional (OPTIONAL,
DEFAULT). Unlike in function modules, the default way of passing a parameter in a
method is by reference. To pass a parameter by value, you must do so explicitly using the
VALUE addition. The return value (RETURNING parameter) must always be passed
explicitly as a value. This is suitable for methods that return a single output value. If you
use it, you cannot use EXPORTING or CHANGING parameters.
• Events:- A mechanism set within a class which can help a class to trigger methods
of other class.
• Interfaces:- Interfaces are independent structures that you can implement in a class to
extend the scope of that class.
Instance components exist separately in each instance (object) of the class and
are referred using instance component selector using ‘’.
Kalyan trainerkalyan@gmail.com
Static components only exist once per class and are valid for all instances of the
class. They are declared with the CLASS- keywords
Static components can be used without even creating an instance of the class and
are referred to using static component selector ‘ =>’ .
Visibility of Components
Each class component has a visibility. In ABAP Objects the whole class definition is
separated into three visibility sections: PUBLIC, PROTECTED, and PRIVATE.
• Data declared in public section can be accessed by the class itself, by its
subclasses as well as by other users outside the class.
• Data declared in the protected section can be accessed by the class itself, and
also by its subclasses but not by external users outside the class.
• Data declared in the private section can be accessed by the class only, but not by
its subclasses and by external users outside the class.
Object References
To access an object from an ABAP program, you use object references. Object references
are pointers to objects. In ABAP, they are always contained in reference variables.
addition in the TYPES or DATA statement, where <class> refers to a class. A reference
variable with the type class reference is called a class reference variable, or class
reference for short.
A class reference <cref> allows a user to create an instance (object) of the corresponding
class, and to address a visible component <comp> within it using the form.
Kalyan trainerkalyan@gmail.com
Creating Objects
Before you can create an object for a class, you need to declare a reference variable with
reference to that class. Once you have declared a class reference variable <obj> for a
class <class>, you can create an object using the statement
Programs can only access the instance components of an object using references in
reference variables. The syntax is as follows (where <ref> is a reference variable):
You can access static components using the class name as well as the reference variable.
It is also possible to address the static components of a class before an object has been
created.
Within a class, you can use the self-reference ME to access the individual components:
Self references allow an object to give other objects a reference to it. You can also access
attributes in methods from within an object even if they are obscured by local attributes
of the method.
Object Lifetime
An object exists for as long as it is being used in the program. An object is in use by a
program for as long as at least one reference points to it, or at least one method of the
object is registered as an event handler.
As soon as there are no more references to an object, and so long as none of its methods
are registered as event handlers, it is deleted by the automatic memory management
(garbage collection). The ID of the object then becomes free, and can be used by a new
object.
Kalyan trainerkalyan@gmail.com
Example on Visibility of Components
Class Definition
Class Implementation
Object Creation
Kalyan trainerkalyan@gmail.com
Attributes of Object Oriented Programming:
• Inheritance.
• Abstraction.
• Encapsulation.
• Polymorphism
Inheritance is the concept of adopting the features from the parent and reusing them . It
involves passing the behavior of a class to another class. You can use an existing class to
derive a new class. Derived classes inherit the data and methods of the super class.
However, they can overwrite existing methods, and also add new ones.
Single Inheriting: Acquiring the properties from a single parent. (Children can be more).
Multiple inheritance: Acquiring the properties from more than one parent.
Example
Tomato1
(Best color)
Tomato2
(Best Size)
Kalyan trainerkalyan@gmail.com
Tomato3
(Best Taste)
Let us see a very simple example for creating subclass(child) from a superclass(parent)
Kalyan trainerkalyan@gmail.com
Multiple Inheritance is not supported by ABAP.
Output is as follows :
Kalyan trainerkalyan@gmail.com
Abstraction: Everything is visualized in terms of classes and objects.
Encapsulation The wrapping up of data and methods into a single unit (called class) is
known as Encapsulation. The data is not accessible to the outside world only those
methods, which are wrapped in the class, can access it.
Kalyan trainerkalyan@gmail.com