ClassBook-Lessons-ABAP Part II Lesson3
ClassBook-Lessons-ABAP Part II Lesson3
Multiple Instances
▪ The ability to create multiple instances of a "class", such as a vehicle, is one of the
central attributes of object-oriented languages.
Encapsulation
▪ Encapsulation means that the implementation of an object is hidden from other
components in the system, so that they cannot make assumptions about the
internal status of the object and therefore dependencies on specific
implementations do not arise
Polymorphism
▪ Polymorphism (ability to have multiple forms) in the context of object technology
signifies that objects in different classes react differently to the same messages.
Inheritance
▪ Inheritance defines the implementation relationship between classes, in which one
class (the subclass) shares the structure and the behavior defined in one or more
other classes (super classes).
▪ Note: ABAP Objects only allows single inheritance.
Compatibility
▪ ABAP object is true extension of ABAP language. ABAP OOPS statements can be
used in procedural ABAP programs. Object themselves can contain classic ABAP
statements, Only OOPS concepts that have been proved useful have been included.
It has been kept the simplest. There is increased use of type checks.
A class is a set of objects that have the same structure and the same
behavior. A class is therefore like a blueprint, in accordance with which all
objects in that class are created.
The components of the class are defined in the definition part. The
components are attributes, methods, events, constants, types, and
implemented interfaces. Only methods are implemented in the
implementation part.
The CLASS statement cannot be nested, that is, you cannot define a class
within a class.
Attributes describe the data that can be stored in the objects of a class.
Attributes can have any kind of data type:
▪ C, N, I, P, ..., STRING
▪ Dictionary types
▪ User-defined types
▪ TYPE REF TO defines a reference to an object, in this case “r_car”
Public attributes
▪ Can be viewed and changed by all users and in all methods
▪ Direct access
Private attributes
▪ Can only be viewed and changed from within the class
▪ No direct access from outside the class
Public methods
▪ Can be called from anywhere
Private methods
▪ Can only be called within the class
Instance attributes
▪ One per instance
▪ Statement: DATA
Static attributes
▪ Only one per class
▪ Statement: CLASS-DATA
▪ Also known as class attributes
Instance methods
▪ Can use both static and instance components in their implementation part
▪ Can be called using an instance
Static methods
▪ Can only use static components in their implementation part
▪ Can be called using the class
You can address the object itself within instance methods using the
implicitly available reference variable me.
Description of example: In the constructor, the instance attribute make is
covered by the locally defined variable make. In order to still be able to
address the instance attribute, you need to use me.
The static constructor is a special static method in a class with the name
class_constructor. It is executed precisely once per program.
The static constructor of a class <classname> is called automatically when the
class is first accessed, but before any of the following actions are executed:
Creating an instance in the class using CREATE OBJECT <obj>, where <obj> has
the data type REF TO <classname>
Addressing a static attribute using <classname>=><attribute>
Calling a static attribute using CALL METHOD <classname>=><classmethod>
Registering a static event handler method using SET HANDLER
<classname>=><handler_method> FOR <obj>
Registering an event handler method for a static event in class <classname>.
The static constructor cannot be called explicitly.
Single Inheritance
▪ ABAP Objects only has single inheritance.
▪ A class may only have one direct superclass, but it can have more than one
direct subclass. The empty class OBJECT is the root node of every inheritance
tree in ABAP Objects.
Normally the only other entry required for subclasses is what has
changed in relation to the direct superclass. Only additions are permitted
in ABAP Objects, that is, in a subclass you can "never take something
away from a superclass". All components from the superclass are
automatically present in the subclass.
With this kind of cast, a check is carried out at runtime to ensure that
the current content of the source variable corresponds to the type
requirements of the target variables. If it is, the assignment is carried
out. Otherwise, an exception of the error class
CX_SY_MOVE_CAST_ERROR is raised.
The root class CX_ROOT contains two predefined methods that are
inherited by the other classes.
The GET_SOURCE_POSITION method returns the program name, include
name (if relevant), and line number in the source code where the
exception occurred.
The GET_TEXT method returns an exception text of a class in the form of
a string.
You can assign several texts to each class. You can then specify which
text is to used when an exception is raised by passing an identifier to the
IMPORTING parameter TEXTID of the instance constructor.
All exception classes inherit the KERNEL_ERRID attribute from CX_ROOT.
This attribute contains the name of the appropriate runtime error if the
exception was raised by the runtime environment - such as
COMPUTE_INT_ZERODIVIDE if the program tries to divide by zero
The TRY block contains the application code that is to handle the
exceptions.
If an exception occurs in the TRY block the system searches first for a
CATCH statement (which will handle the exception) in the same TRY-
ENDTRY structure and then step by step outwards in all the enclosing
TRY-ENDTRY structures.
A CATCH block contains the exception handler that is executed if a
specified exception has occurred in the TRY block in the same TRY-
ENDTRY structure.
In some cases, the system cannot find a handler for an exception within
a specific TRY-ENDTRY structure but the exception is handled in a
surrounding TRY-ENDTRY structure or passed along to a calling program.
If this occurs, a CLEANUP block is executed before leaving the TRY-
ENDTRY structure.