You are on page 1of 27

ABAP OBJECTS

02-Sep-09 Kaavian Systems


ABAP Objects

 General

 Classes

 Object handling

 Declaring & calling methods

 Interfaces

 Triggering & handling events

02-Sep-09 Kaavian Systems 2


General

 Classes

Components of a Class : Attributes, Methods and Events


 Objects
The Source code that contains data and provides services

 Properties of the objects


Encapsulation

Inheritance

Polymorphism
ABAP Objects

 ABAP run-time environment

 Why function groups to objects

 OO-extension of ABAP language

02-Sep-09 Kaavian Systems 4


Classes

 Global Classes

Defined in Class Builder ( Transaction SE 24).


All ABAP Program can access global classes.

 Local Classes

Defined with in an ABAP program.


Used in the program in which they are defined.

02-Sep-09 Kaavian Systems 5


Defining Local Class

Complete definition of class contains declaration part if required an


implementation part
CLASS <class> DEFINITION
……………….
ENDCLASS

CLASS<class> IMPLEMENTATION
…………………….
ENDCLASS

02-Sep-09 Kaavian Systems 6


Class Components

 Two types of Components


Instance and Static Component.
 Attributes
Instance Attribute, Declaration by DATA
Static Attribute, Declaration by CLASS-DATA
 Methods
Instance Method, Declaration by METHODS
Static Method, Declaration by CLASS-METHODS
 Events
Instance Events, Declaration by EVENTS
Static Events, Declaration by CLASS-EVENTS

02-Sep-09 Kaavian Systems 7


Encapsulation

 Public Section

 Private Section

 Protected Section

02-Sep-09 Kaavian Systems 8


Overview

02-Sep-09 Kaavian Systems 9


Object Handling
 Object :
INSTANCE OF CLASS
 Types of object :
TRANSIENT OBJECT
PERSISTENT OBJECT
 Object reference :
DATA <objr> TYPE REF TO <class>

 Creating objects :
CREATE OBJECT<objr>.

 Addressing components of objects :


TO ACCESS AN ATTRIBUTE : <ref>------><attr>
TO CALL A METHOD : CALL METHOD <ref>----><meth>
<class name>===><attr>
CALL METHOD <class>===><meth>
02-Sep-09 Kaavian Systems 10
Object Life Time

 Assigning reference :
<cref1> = <cref2>
 Object life time :
Object exists as long as it is being used in the program.
An object is in use in the program as long as,

− Atleast one reference points to it

− One method of the object is registered as an event


handler

02-Sep-09 Kaavian Systems 11


Overview

02-Sep-09 Kaavian Systems 12


Methods
Methods describe the behavior of objects within a class. With the help
of methods, the system provides operations, services and functions.
Declaring methods :
 Instance method :
METHODS <meth> IMPORTING
EXPORTING
CHANGING
RETURNING VALUE
EXCEPTIONS
 Static method :
CLASS-METHODS <meth>

02-Sep-09 Kaavian Systems 13


Calling Methods
 Implementing methods :
METHOD <meth>
………………..
ENDMETHOD
 Calling methods :
CALL METHOD <meth> EXPORTING
IMPORTING
CHANGING
RECEIVING
EXCEPTIONS
 Outside the class :
INSTANCE METHOD : CALL METHOD <ref> ------> <meth>
STATIC METHOD : CALL METHOD <class>===><meth>
02-Sep-09 Kaavian Systems 14
Special Methods

 Special methods
 Event handler methods :
METHODS <meth> FOR EVENT <evt> OF <class>

 Constructors :
Instance constructors : CONSTRUCTOR

Static constructors : CLASS_CONSTRUCTOR

02-Sep-09 Kaavian Systems 15


Inheritance
 Inheritance :
CLASS <subclass> DEFINITION INHERITING FROM <superclass>

 Redefining methods :
Redefine an inherited public/protected instance method in a
subclass & make its function more specialized
METHODS <method name> REDEFINITION.

 Abstract & final methods & classes :


ABSTRACT CLASS cannot be instantiated
ABSTRACT Method can be defined in an abstract class but cannot
be implemented in that class. But implemented in subclass.
FINAL CLASSES cannot have subclass
FINAL METHOD cannot be redefined.

02-Sep-09 Kaavian Systems 16


Interfaces

 To address different classes thro’ a universal point of contact

 They allow a single method within an interface to behave


differently in different classes

 Interface along with inheritance forms the pillar for


polymorphism

02-Sep-09 Kaavian Systems 17


Defining Interface

DEFINING INTERFACE

– GLOBAL INTERFACE (SE24)

– LOCAL INTERFACE
INTERFACE <INTF>
……
ENDINTERFACE.

02-Sep-09 Kaavian Systems 18


Implementing Interface

 Implementing interface :
IMPLEMENTING IN A CLASS USING
INTERFACES <intf>
METHODS IN A INTERFACE implemented in the
implementation part of a class
METHOD <intf~meth>

ENDMETHOD.
 Aliases
ALIASES <name> FOR <intf~meth>
An alias name is a short form of the full name of a COMPONENT
that you add to a class by implementing the interface to which it
belongs

02-Sep-09 Kaavian Systems 19


How to Address

 Interface reference :
DATA <iref> TYPE REF TO <INTF>
CAN ADDRESS INTF’ COMPONENTS BY
<iref>-><icomp>

 Addressing objects using int reference.


<iref> = <cref>
USING CLASS REFERENCE VARIABLE <cref>
To access an attribute <cref>-><intf~attr>
CALL METHOD <cref>-><intf~meth>
Using class reference variable
CALL METHOD <iref>-><meth>

02-Sep-09 Kaavian Systems 20


Assigning Reference

Assignment using interface reference


<iref1> = <iref2>

both has 2 refer 2 same interface.

<iref> = <cref>

Class of class reference <cref> must implement the interface of


interface reference <iref>

02-Sep-09 Kaavian Systems 21


Overview

02-Sep-09 Kaavian Systems 22


Data Objects

 Data objects contain the data with which ABAP programs work at
runtime. They are not persistent, but only exist for the duration of the
program.

 All of the data objects that are defined in the declaration part of a
program using statements such as DATA are created statically .

 To create a data object dynamically during a program, you need a data


reference variable and the following statement:

CREATE DATA <dref> TYPE <type>|LIKE <obj>.

02-Sep-09 Kaavian Systems 23


Triggering and Handling Events

Certain method acts as triggers & trigger events to which other


methods(the HANDLERS) react .Handler methods are executed when
the event occurs.

 Declaring events :
Instance events : EVENTS <evt> EXPORTING

Static events : CLASS-EVENTS <evt>

 How do u trigger events :


RAISE EVENT <evt> EXPORTING <e> = <F>

02-Sep-09 Kaavian Systems 24


Declaration of Event Handler Methods

 Instance method :

METHODS <meth>for EVENT <evt> OF <class> .

 Static method :

CLASS-METHODS <meth> for EVENT <evt> OF <class>.

 Registering event handler methods :

SET HANDLER <hi> [FOR]


SET HANDLER <hi> FOR <ref>
SET HANDLER <hi> FOR ALL INSTANCES
SET HANDLER <hi>
02-Sep-09 Kaavian Systems 25
Overview

02-Sep-09 Kaavian Systems 26


THANK YOU

02-Sep-09 Kaavian Systems 27

You might also like