You are on page 1of 39

Contents

Introduction
Elements in ABAP Objects
Classes
Attributes
Methods
Events
Event Handling
Objects
Interfaces
Inheritance in ABAP Objects


Introduction
Conventional ABAP programming has been structured
in nature, where data is stored in database tables and
function oriented programs access it.

ABAP Objects support Object Oriented Software
Development

Object Orientation is a programming model in which
Data and Functions are combined together into Objects

Classes
It can be regarded as instruction for building an
Object
The type of an object is known as its Class
Abstract description of an object

Classes contain components which describe state
and behavior of an object.
Global classes can be defined using SE24(Class
Builder) and local classes using SE38
Note : Instance of a Class is called Object


Classes
Complete definition includes :
Declaration part
Implementation part (If required)

Class MyClass Definition.
Declaration Part ( For attributes, Methods
EndClass. and Events)

If Declaration part contains Methods, then these methods must be
implemented in the Implementation part using statement

Class MyClass Implementation.

EndClass.



Class Components
The Components of Class can be :
Attributes
Methods
Events

The declaration part can be divided into three sections namely
PUBLIC SECTION, PROTECTED SECTION and PRIVATE SECTION
which controls the visibility.

Each component of class must be explicitly assigned to one of these three
visibility sections


Visibility Sections
PUBLIC SECTION : The components in this section are visible to everybody, i.e.
users, its own methods and its subclasses.
Therefore, public components form the external interface between the class and its
users.

PROTECTED SECTION : The components are visible to the methods of the class
and its subclasses.
Therefore, it forms a special interface between the class and its subclasses.

PRIVATE SECTION : Here, components are only visible to its own method.

A section may or may not be present, but whatever sections are present they
should come in the same above order.
Also, these three sections form the basis for Encapsulation, which is one of the
Buiding Blocks of the OOPS Design
Visibility Sections
In short,
Own
Class
Subclass User
Public Visible Visible Visible
Protected Visible Visible Not Visible
Private Visible Not Visible Not Visible
Components Types
Instance Components :
Instance Dependent
Exist for each Object

Static Components :
Instance Independent
Exist only once for each class
They are retained for the entire Runtime
All objects of a class can access the static attributes of the class
When a static attribute is changed, this change is reflected in all other
objects of the class

Attributes
Attributes are data fields in the Class
Can have any ABAP datatype
The contents of the Attribute determine the status
of the object.
Can be Instance( DATA Statement) or
Static ( CLASS-DATA Statement)
Note : Within the Class, the addition LIKE can only be used for
reference to other attributes of the class. For references to data types
from the ABAP Dictionary, only the addition TYPE can be used
Methods
Internal procedures in the Class
Determine behavior of an Object
Can access all attributes of the Class and can thus change the status of an
Object
Similar to other ABAP procedures (Subroutines and Function Modules)
Can be Instance ( METHODS) or Static (CLASS-METHODS)
They are declared in the Declaration part and implemented in the
Implementation part using statement

Method MyMethod.
.
EndMethod.
Methods
Instance Methods :
They can access all the attributes of a class and can trigger all events of the class.

Static Methods :
Can access only static attributes and can trigger only static events

Methods can be called statically or dynamically.
Dynamic access to methods is called Dynamic Invocation.
The additions of the CALL METOD statement are used to pass and receive actual
parameters to and from the method interface.

To call a method, use the following syntax
Variant 1 : CALL METHOD meth.
where, meth = Method Name

Calling a Method
The additions to the above statement are
1. ... EXPORTING p1 = f1 ... pn = fn ( By Value/ By Reference)
2. ... IMPORTING p1 = f1 ... pn = fn ( By Value/ By Reference)
3. ... CHANGING p1 = f1 ... pn = fn ( By Value/ By Reference)
4. ... RETURNING p = f ( By Value)
5. ... EXCEPTIONS except1 = rc1 ... exceptn = rcn
6. ... PARAMETER-TABLE itab
7. ... EXCEPTION-TABLE itab

Variant 2 : CALL METHOD meth( f ).
If it has only one input (IMPORTING) parameter and no other interface
parameters. Here f is actual parameter.
Variant 3 : CALL METHOD meth( p1 = f1 ... pn = fn ).
If the method has only input parameters(IMPORTING) parameter and no
other
interface parameters. Here f1 . fn are actual parameter.

Constructors Special Methods
Special methods that produce defined initial state of object and classes
Necessary to set the initial state of an object dynamically
They are of two types :
Instance Constructors
Static Constructors

Instance Constructors
It is predefined, public instance method of the Class, with name CONSTRUCTOR
Each class has one Instance Constructor
They are executed once for each instance
They are called automatically directly after you have created an instance of the object
with the CREATE OBJECT statement.
They can have interface with IMPORTING parameters and EXCEPTIONS
There is no EXPORTING parameters, and it shows that Constructors only exist to
define the state of an Object.


Constructors - Special Methods
Static Constructors
It is predefined, public, static method of the class with name
CLASS_CONSTRUCTOR
Each class has a single static constructor
If require, declare it using CLASS-METHODS statement and implement it
in the Implementation part
It has no interface parameters and cannot trigger exceptions
It is executed once in each program.
It is called automatically for the class before the class is accessed for the first
time
Events
Events enable objects or classes to trigger Event Handler Methods in
other objects or Classes
Events are of two types : Instance and Static

Instance Events
Declared using the statement EVENTS.
Instance events can only be triggered in Instance Methods.

Static Events
Declared using statement CLASS-EVENTS
Static events can be triggered both by Instance Methods and Static Methods
Events
Events can be triggered from the Objects/Class of the same class where the events have been
defined.
Event handler methods can be defined in the same class or other classes.
The EVENTS statement has addition EXPORTING parameter to pass attributes if required
The following statements are used to declare event e1 in class c1. Event e1 exports integer
variable var. Here object o1 is created of class type c1. Event handler method handle_e1 has
been declared. Also, when raising e1 the integer temp_var is exported.

EVENTS e1 EXPORTING value(var) TYPE i.
The syntax for declaration of Event Handler Methods is as follows :
METHODS handle_e1 FOR EVENT e1 OF c1
IMPORTING var.
Set the Event Handler methods, before triggering the events, using the following statement
SET HANDLER o1->handle_e1 FOR ALL INSTANCES.
To trigger the event, use the statement RAISE EVENT.
RAISE EVENT e1 EXPORTING var = temp_var.
SET HANDLER
Registers and deregisters Event Handler Methods dynamically at Runtime

This statement can be used in three ways

SET HANDLER h1 .hn FOR ref
This form is used for instance events and registers the Event Handler Methods for only one instance (i.e
one object )
ref stands for Class Reference Variable or Interface Reference Variable

SET HANDLER h1..hn FOR ALL INSTANCES
This form is used for instance events and registers the Event Handler Methods for all Instances
ref stands for Class Reference Variable or Interface Reference Variable

SET HANDLER h1..hn.
This form is used for static events.
In case of Class Reference : It registers Event Handler Methods for triggering from class where the event
has been declared.
In case of Interface Reference : It registers the event handler methods for all triggering classes that
implement the interface intf.


RAISE EVENT
It triggers the Event Handler Methods that have been registered for
that event with the SET HANDLER statement

The event must be the component of the class from where it is triggered
After the RAISE EVENT statement, all of the registered handler methods
are executed before the next statement is processed (Synchronous Event
Handling)
Event handler methods can in turn trigger events
It is currently only possible to nest RAISE EVENT statements to a depth of
64 calls.
Objects and References
Objects are nothing but instance of a class.
An object remains intact for as long as it is used in a program
Objects are deleted by the automatic memory management (garbage collection) when no
more references to them exist, or when they cease to be registered as an event handler.

Class References : Declared using statement
DATA : <cref> TYPE REF TO <class name>

Interface References : Declared using statement
DATA : <iref> TYPE REF TO <Interface name>

Special References : ME and SUPER
Note : TYPES statement can be used to define data types
CONSTANTS keyword can be used to declare constants.
Constants are static Attributes whose values are defined in declaration and cannot be
changed.
TYPES and CONSTANTS, both are instance-independent and existence for all objects
of a
class.
Creating Object
To create objects we have to use the CREATE OBJECT statement.
CREATE OBJECT cref.
where, cref = Class Reference Variable
{ data : cref type ref to MyClass. }

Can have interface parameters like EXPORTING or EXCEPTIONS.
If the Class has instance constructor (CONSTRUCTOR), the CREATE
OBJECT statement calls it after the object has been created completely
If the Class has static constructor (CLASS_CONSTRUCTOR), and it has not
yet been executed, the CREATE OBJECT statement calls it before creating
the Object

CREATE OBJECT
Addition to the Statement are
EXPORTING p1 = f1 pn = fn.
Pass all the non-optional IMPORTING parameters of Instance Constructor.

EXCEPTIONS except1 = rc1 exceptn = rcn.
It allows to handle exceptions which occur in the instance constructor of the Class.
If the exception is triggered, the system stops processing the constructor, deletes the
instance
it just created, and assigns the return value rc to the system variable SY-SUBRC.
Exceptions can be raised by two ways :
RAISE except statement : If the exception raised is not handled the program
terminates

MESSAGE RAISING statement : If the exception raised is not handled, the
system
displays the specified message and then carries on the processing according to the
type of
message
OPERATORS in ABAP Objects
- - Structure Component Selector.

- -> Object Component Selector
- To access instance components from outside the class using obj->comp.
- ME->comp can be used to point to the instance components and static components
of this class.

=> Class Component Selector
You can access the static components from outside the class with the expression
class=>comp

~ Interface Component Selector
When an interface intf is implemented in a class, an interface component comp in
the class is assigned the name intf~comp.
Accessing Class Components
Static Access
The following syntax applies (ref is a reference variable):
Access to an instance attribute attr: ref->attr
Calling an instance method meth: CALL METHOD ref->meth
In addition to reference variables, the class name can be used for access:
Accessing a static attribute attr: class=>attr
Calling a static method meth: CALL METHOD class=>meth

Dynamic Access
The following syntax applies (ref is a reference variable):
Calling an instance method meth: CALL METHOD ref->(f)
Calling a static method meth: CALL METHOD class=>(f)
CALL METHOD (c)=>meth
CALL METHOD (c)=>(f)
Calling own method meth: CALL METHOD (f)
CALL METHOD ME->(f)

Interfaces
Interfaces are independent structures that allow you to enhance the class-specific
public points of contact by implementing them in classes.

Interfaces can be defined globally in the R/3 repository or locally in ABAP program
Can define exactly same components in Interfaces as in Classes
Unlike classes, Interfaces do not have instances, instead they are implemented by
classes
Implemented using INTERFACES statement in the declaration part of the class
INTERFACES statement must be included in the PUBLIC SECTION of the class
Different classes that implement the same interface can all be addressed in the same
way.
Interface references allow users to address different classes in the same manner.
Interfaces can also be nested.

Interfaces are the basis for polymorphism in classes, because they allow a
single interface method to behave differently in different classes.

Interfaces
If an interface is implemented in the class, the interface components
are added in the public section of the class.
A component comp of an interface intf, implemented in a class,
becomes a fully-fledged member of that class, with the name
intf~comp.
Classes that implement interfaces must implement all of their
methods.
METHOD intf~meth.

ENDMETHOD.

Interfaces allow you to use different classes in a
uniform way (polymorphism).
Interface References
Creating Interface Reference Variables
DATA obj TYPE REF TO intf.
Using this reference variable, you can access any of the components defined in the interface

Nested Interfaces
Interface can include one or more interfaces as components, which can contain interfaces
themselves.
Compound Interface : It includes other interface as its component.
Elementary Interface : It does not include any interface as a component.
All interface components of a compound interface have the same level
A component interface exists only once even if it is used once more as a component of another
component interface.

Aliases : It can be used to assign alias names to the components of component interfaces, thus
making them visible within the interface definition.
ALIASES <alias name> FOR intf~comp.
Where, intf = interface name and comp = Component name
Accessing Objects using
Interface References
It is also possible to directly generate an object to which the interface
reference
variable points initially. In this case, the TYPE addition of the statement
CREATE OBJECT must be used to specify a class that implements the
interface.
CREATE OBJECT iref TYPE class.

If the interface intf contains an attribute attr and an instance method meth,
you can address them as follows:
Using the class reference variable:
Accessing an attribute attr: cref->intf~attr
Calling a method meth: CALL METHOD cref->intf~meth

Using the interface reference variable:
Accessing an attribute attr: iref->attr
Calling a method meth: CALL METHOD iref->meth
Accessing Static Components of
Interfaces
Use the name of the interface to access constants within an
interface.

Accessing a constant const: intf=>const.

To access the other static components of an interface, use an
object reference or the class class that is implementing the
interface.

Accessing a static attribute attr: class=>intf~attr.

Calling a static method meth: CALL METHOD
class=>intf~meth.
Inheritance
Inheritance allows to define classes by deriving them from existing
classes.
Use INHERITING FROM clause as follows
CLASS MyClass DEFINITION INHERITING FROM SuperClass.
ENDCLASS.
SUPERCLASS


Subclass1 Subclass2 Subclass3 Inheritance
Tree


Subclass11 Subclass21
Inheritance
It inherits all the components from the superclass.
Only PUBLIC and PROTECTED components are visible, PRIVATE components are
present but not visible.
Can have attributes with same name as that of the private attributes of the superclass.
The methods of the respective classes will access its own set of private attributes.
A class can have any number of subclasses, but only one superclass, as ABAP Objects
use Single Inheritance.
The root node of the Inheritance tree in ABAP Objects is the predefined empty class
OBJECT.
It is always present implicitly, when we define a new class
Each subclass contains all the components of all the classes that lie between this class
and the root node in the Inheritance tree
Visibility of the component is always the same and cannot be changed

Redefining Methods
It is possible to redefine public and protected instance methods of all
preceding superclasses using the addition REDEFINITION of the statement
Methods
The interface of the Redefined method cannot be changed. The method is
merely re-implemented under the same name.
The obscured method declaration and implementation remains with the
superclass.
Static methods cannot be redefined.
Each reference that points to the subclass object uses the redefined method
even if it was typed with reference to the superclass.
Self Reference ( ME-> ) can also be used to access own methods.
Within the Redefined Method the pseudo reference SUPER-> can be used to
access the obscured method, for example, to take over and supplement its
functionality.
Abstract and Final Methods and
Classes
Abstract Methods are the Methods that can not be implemented in the same class but only
in the subclass of the Inheritance tree.
If the class contains Abstract Method, then the class must be declared as Abstract Class
It is not possible to instantiate Abstract Classes
To declare Abstract Methods and Classes, use the ABSTRACT addition to the
METHODS and CLASS statements

If a Method is declared as Final Method, then it cannot be redefined in the subclasses.
Also, Final classes cannot have other subclasses and close an inheritance tree finally.

To declare Final Methods and Classes, use the FINAL addition to the METHODS and
CLASS statements

References to Subclasses and
Polymorphism
The content of a reference variable typed with reference to a subclass can always be
assigned to reference variables typed with reference to one of its superclasses or the
interfaces of these superclasses.
In particular, the target variable can always be typed with reference to the class
OBJECT.
A static user can use a reference variable to address the components visible to it, (To
which the reference variable refers)
With dynamic accesses, all the components can be addressed.
When an instance method is redefined in one or more subclasses, different
implementations of the method can be executed after a method call using the same
reference variable, depending on to which class the reference variable is pointing in the
inheritance tree.
The feature that different classes have the same interface and can therefore be
addressed using reference variables of one type is called polymorphism.
Inheritance and Constructors
When you call an instance constructor, you must assign values to all of its
non-optional interface parameters.
There are various ways of doing this:
In the CREATE OBJECT statement :
If instance constructor with interface present, then pass values using
EXPORTING
If instance constructor without interface, no parameters are passed
If class of the object created does not have explicit instance constructor, you
must look at next highest superclass with an explicit instance constructor. If it
has interface, supply values using EXPORTING parameter, else dont supply.

Similarly, interface parameters should be taken care of when using the
statement SUPER->CONSTRUCTOR.
Inheritance and Interfaces
Interfaces are standalone constructions within ABAP Objects that allow
Polymorphism.
As each class that implements an Interface can implement its methods differently
Interface references can point to objects in any class that implements the corresponding
interface.
Interfaces and Inheritance are fully compatible.
Can implement any number of interfaces within the given Inheritance tree, but a given
interface may only be implemented once in an inheritance tree. This ensures that the
interface components have unique names throughout the Inheritance tree (intf~icomp).
Interface references that can point to a class can also point to all of its subclasses.
Interface methods, once they have been implemented are full members of class, and
can be redefined in its subclasses.
However, you cannot define methods as Abstract and Final within the Interface
Definition
Inheritance and Static Attributes
Like all components, static attributes exist only once per
inheritance tree
When a static attribute is addressed that belongs to a section of
an inheritance tree, the class where the attribute is declared is
always addressed irrespective of the class name used in the class
component selector.
A static constructor is executed when a class is addressed for the
first time.
When a static attribute is addressed via the class name of a
subclass but is declared in a superclass, only the static
constructor of the superclass is executed.
Inheritance and Constructors
Each class has an instance constructor called CONSTRUCTOR. This is an
exception to the rule that states that components must be unique within an
Inheritance Tree.

Cannot redefine Constructors.
Cannot call it explicitly using CALL METHOD CONSTRUCTOR.

Since subclasses contain all of the public and protected attributes of their
superclasses, whose values are set by the instance constructor of the
corresponding class, the instance constructor of a subclass must ensure that the
instance constructors of all of its superclasses are also executed.

The instance constructor of every subclass must contain the method call
CALL METHOD SUPER->CONSTRUCTOR, which calls the instance
constructor of its direct superclass. The only exception to this rule are the
subclasses of the root node of the inheritance tree OBJECT.


Questions?

Thank You

You might also like