ABAP Objects 1
Transcript of ABAP Objects 1
-
7/30/2019 ABAP Objects 1
1/148
2005 Intelligroup, Inc. Confidential and Proprietary
ABAP Objects
October,2005
Jayanth.C
-
7/30/2019 ABAP Objects 1
2/148
2
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Topics to cover
Why Object oriented ABAP?
Class, objects, Methods & Constructor
Inheritance, Polymorphism
Events & Exceptions
Real time usage of ABAP Objects
Limitations
-
7/30/2019 ABAP Objects 1
3/148
3
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Evolutions in ABAP as a programming
language
In seventies ABAP stood for AllgemeirBerichicts-Aufbereitungs Prozessor( General Report
Preparation Processor ).
In mid eighties ABAP had developed into an
interpreter language which was a main component of
the R/2 system and which could cope up with
business application programs.
In early nineties, ABAP evolved as a 4th generation
programming language.
-
7/30/2019 ABAP Objects 1
4/148
4
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Different approaches of Programming
Unstructured Programming.
Procedural Programming.
Object Oriented Programming.
-
7/30/2019 ABAP Objects 1
5/148
5
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Unstructured Programming
Consists of only one main program.
The program stands for a sequence of
commands which modify data that isglobal throughout the whole program.
Characteristics
Difficult to manage once the programbecomes large.
Same sequence of statements arerepeated at multiple places, if they areneeded at multiple locations.
Disadvantages
report ysubdel.DATA : sal type p decimals 2,
itax type p decimals 2,net_sal type p decimals 2 .
sal = 12000.IF sal lt 5000 .itax = 0.
ELSE.itax = sal * '0.01'.
ENDIF.net_sal = sal - itax.
write:/5 sal , itax , net_sal.
sal = 3500.IF sal lt 5000 .
itax = 0.ELSE.
itax = sal * '0.01'.ENDIF.
net_sal = sal - itax.write:/5 sal , itax , net_sal.
-
7/30/2019 ABAP Objects 1
6/148
-
7/30/2019 ABAP Objects 1
7/148
7
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Evolutions in ABAP as a programming
language
At the turn of the new century, ABAP completed anew stage in its evolution by ABAP/4 superseded by
ABAP objects.
Questions still unanswered:
What are the implications by introducing ABAP
objects? How did it superseded ABAP which is still
catering customer needs anyways???? Did ABAP objects madeABAP/4 obsolete?
-
7/30/2019 ABAP Objects 1
8/148
8
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Implications of introduction of ABAP
Objects
ABAP objects and ABAP are inextricably linked. Drift from a structural programming approach towards
Object oriented approach
But to keep you comfortable,ABAP is still supported
and allows you to use object-oriented elements.
Questions Unanswered:
Then why should I learn ABAP objects?
Why dont I use the classical approach?
-
7/30/2019 ABAP Objects 1
9/148
9
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Are ABAP Objects mandatory?
Continue our Technical consultancy career in SAP without bottlenecks
Should be able to provide solutions to heterogeneouskinds of client technical requirements related to
Program involving in
multiple functionalrequirements
Reuse available
Standard/Customclasses in ABAP
Business Server Pages GUI Control Framework
Office Integration XML Transformations
BADI, Workflow Email, Shared Objects,Persistent objects
Generic Programming New ABAP Editor,Code Inspector, CATT
-
7/30/2019 ABAP Objects 1
10/148
-
7/30/2019 ABAP Objects 1
11/148
11
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object OrientationWhat are Objects?
You interact with objectseveryday
A customer
An order
All objectscontains state and behavior
What they can do and what changes when they do
Software objectsrepresent these as:
Data ( like 4GL variables )
Methods ( like 4GL procedures)
Your car
The telephone
-
7/30/2019 ABAP Objects 1
12/148
12
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
What's an Object and Class?
. Booch's object definition:An object has state, behavior, and identity; the
structure and behavior of similar objects are defined
in their common class; the terms instance and object
are interchangeable.Ex: ICICI S.Acc#1111, ICICI S.Acc#1112, ICICI
S.Acc#9999,
. Booch's class definition:
A class is a set of objects that share a common
structure and a common behavior.
Ex: ICICI S.Acc.
-
7/30/2019 ABAP Objects 1
13/148
13
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Some Classes & Their ObjectsMaruthi 800 Rajas Maruthi,
Prasads Maruthi,
Ramanis Maruthi
Customer Steelcase, Coke, GE, BMS,Exxon, Hitachi, Hospira
SalesOrder OR2643789, OR2643799OR2643776, OR9999999
Cricket Team IndianTeam, Australian Team,
SrilankanTeam
Your Desktop PC IGA51097 etc.
-
7/30/2019 ABAP Objects 1
14/148
14
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object-oriented Programming
Object-oriented programming is a method of
implementation in which programs are organized
as cooperativecollections of objects, each of
which represents aninstance of some class...
Grady Booch
-
7/30/2019 ABAP Objects 1
15/148
15
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Behavior, State & Identity
Behavior:{Methods}
behavior is how an object acts and reacts, in terms of
its state changes and message passing.
State:{Attributes}
... encompasses all of the (usually static) properties of
the object plus the (usually dynamic) values of each of
these properties. .
Identity:{Key}
... that property which distinguishes it from all other
objects.
-
7/30/2019 ABAP Objects 1
16/148
16
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object-oriented
Application Development
A way to design and build applications
Objects bundle together data (state) and
methods (behavior)
Objects facilitate separating definition from
implementation
Much more than just syntax
You might have already done object-oriented
programming in the 4GL
-
7/30/2019 ABAP Objects 1
17/148
17
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Sample Attributes & Methods
Class Attributes MethodsCricketTeam Captain,
VC, WC,
FB1,FB2,FB3
SP1,SP2,SUB
DoSingle,DoDouble
DoBowl,DoCatch
DoRunout,HitSix,
Doplay,HitBowndaryICICI S.A. Account
Number,
Balance,
CreditLimit
CheckBooks
ATM_Transfer,
E_transfer, Withdraw,
Check_Credit_limit,Issue_check_book,
Track_transactions
Production PO#,SSD,SED,ASD,AED,Comp,CoOptn,Oper,Workcenter
Start_Production,End_production,Start_opr,Send_to_WS Etc
-
7/30/2019 ABAP Objects 1
18/148
18
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Basic Object-oriented Principles
Abstraction
Encapsulation
Hierarchies
-
7/30/2019 ABAP Objects 1
19/148
19
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Abstraction
Abstraction is used to manage complexity
Focus on the essential characteristics
Eliminate the details Find commonalities among objects
Defines the public contract
Public definition for users of the object
The Outside view Independent of implementation
Public View of an Object
-
7/30/2019 ABAP Objects 1
20/148
20
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Abstraction - Example
Object: Automobile
StartStopDrivePumpFuel
What should anAutomobileobject do?
-
7/30/2019 ABAP Objects 1
21/148
21
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Encapsulation
Encapsulationhides implementation
Promotes modular software design data andmethods together
Data access always done through methods Often called information hiding
Provides two kinds of protection:
State cannot be changed directly from outside Implementation can change without
affecting users of the object
Hide Implementation Details
-
7/30/2019 ABAP Objects 1
22/148
22
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Encapsulation - Example
Publicmethods of
Splendor class
Implementation OutsideView
Start(),Stop()PumpFuel(),Change_gear()
Object: Splendor
EngineNum,Gear#,Fuel_MrtRd,SpeedometrRd
StartEngine(),StopEngine(),CosumePetrol(),Move_Wheel()
Start(),Stop()PumpFuel(),Change_gear()
-
7/30/2019 ABAP Objects 1
23/148
23
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Encapsulation - Example continued
Object: Splendor
EngineNum,Gear#,Fuel_MrtRd,
SpeedometrRdStartEngine(),StopEngine(),Consume_Petrol(),Move_Wheel()
Start(),Stop()Pump_Fuel(),Change_gear()
Hmm...Id like to change
Consume_petrol toConsume_diesel
Consume_fuel() callsConsume_Petrol( )
-
7/30/2019 ABAP Objects 1
24/148
24
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Encapsulation - Example continued
This change was easybecause users of the
object will not beaffected.
Object: Splendor
EngineNum,Gear#,Fuel_MrtRd,
SpeedometrRdStartEngine(),StopEngine(),Consume_Diesel(),Move_Wheel())
Start(),Stop()Pump_Fuel(),Change_gear()
Consume_fuel() callsConsume_Diesel( )
-
7/30/2019 ABAP Objects 1
25/148
-
7/30/2019 ABAP Objects 1
26/148
26
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Hierarchies - Example
Automobile
2-Wheerler,3-Wheeler and
4-Wheerlerinherit fromAutomobile(Inheritance)
2-Wheeler 3-Wheeler
is a is a
4-Wheeler
is a
-
7/30/2019 ABAP Objects 1
27/148
27
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Hierarchies - Example
Automobile
Engine
references
Automobile
uses Engine(Aggregation)
-
7/30/2019 ABAP Objects 1
28/148
28
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Summary : Object-oriented Principles
Abstraction Break up complex problem
Focus on public view, commonalities
Encapsulation Hide implementation details
Package data and methods together
Hierarchies
Build new objects by combining or extendingother objects
-
7/30/2019 ABAP Objects 1
29/148
29
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ABAP Object Oriented Programming
Class defined andimplemented
Classes and objects areused to model real worldentity.
Methods inside the classesperform the functions.
Data used by the classesare protected between them.
-
7/30/2019 ABAP Objects 1
30/148
30
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ABAP Object Oriented Programming
Method implementationCalling a Method
Creating an Object
Defining a Referrence
-
7/30/2019 ABAP Objects 1
31/148
-
7/30/2019 ABAP Objects 1
32/148
32
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Oriented Approach - key features
1. Better Programming Structure
2. Real world entity can be modeled very well
3.Stress on data security and access
4. Data encapsulation and abstraction
5. Reduction in code redundancy
-
7/30/2019 ABAP Objects 1
33/148
33
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Summary-- Components of a Class
A Class basically contains the following:-
Attributes:-Any data,constants,types declared within a class form theattribute of the class.
Methods:-Block of code, providing some functionality offered by the class.Can be compared to function modules.
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 ina class to extend the scope of that class.
-
7/30/2019 ABAP Objects 1
34/148
34
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Oriented Design(OOD)
Five Major Steps
1. Identify the objects and their attributes
2. Identify the operations suffered by and required of eachobject
3. Establish the visibility of each object in relation to otherobjects
4. Establish the interface of each object
5. Implement each object
-
7/30/2019 ABAP Objects 1
35/148
-
7/30/2019 ABAP Objects 1
36/148
2005 Intelligroup, Inc. Confidential and Proprietary
DAY 2
-
7/30/2019 ABAP Objects 1
37/148
37
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
Classes are templates for objects. Conversely,you can say that the type of an object is the sameas its class.
components of the class describe the state andbehavior of objects.
Local and Global Classes: Classes in ABAPObjects can be declared either globally or locally.You define global classes and interfaces in theClass Builder (Transaction SE24) in the ABAP
Workbench. They are stored centrally in classpools in the class library in the R/3 Repository.
-
7/30/2019 ABAP Objects 1
38/148
38
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
Local classes are defined within an ABAP program. Local
classes and interfaces can only be used in the program inwhich they are defined.
When you use a class in an ABAP program, the system
first searches for a local class with the specified name. If it
does not find one, it then looks for a global class. Apart from the visibility question, there is no difference
between using a global class and using a local class.
Certain restrictions apply when you define the interface ofa global class, since the system must be able to guarantee
that any program using an object of a global class can
recognize the data type of each interface parameter.
-
7/30/2019 ABAP Objects 1
39/148
39
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
Defining Local Classes:
A complete class definition consists of adeclaration part and, if required, animplementation part.
The declaration part of a class
CLASS DEFINITION....ENDCLASS.
It contains the declaration for all components
(attributes, methods, events) of the class. The declaration part belongs to the global
program data.
-
7/30/2019 ABAP Objects 1
40/148
40
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
If you declare methods in the declaration part of a
class, you must also write an implementation part
for it. This consists of a further statement block:
CLASS IMPLEMENTATION.
...ENDCLASS
The implementation part of a local class is a
processing block. Subsequent coding that is not
itself part of a processing block is therefore notaccessible.
-
7/30/2019 ABAP Objects 1
41/148
41
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Defining Local Classes
REPORT YSUBOOPS17 .
CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value 5.
methods : m1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD M1.
WRITE:/5 'I am M1 in C1'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : oref1 TYPE REF TO c1 .
CREATE OBJECT : oref1.
write:/5 oref1->w_num.
CALL METHOD : oref1->m1 .
Defined in the global areaof a local program :-
CLASS DEFINITION.
..
ENDCLASS.
All the attributes ,methods, events andinterfaces are declared here.
Cannot be declared inside asubroutine/function module.
Class definition cannot benested.
-
7/30/2019 ABAP Objects 1
42/148
42
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Implementing Local Classes
Local class in a program is implemented asfollows:-
CLASS IMPLEMENTATION.
..
ENDCLASS.
Methods used by the class are described here.
A class can be implemented
At the end of the program( like subroutines).
After the class definition.
If the latter is adopted, one must then assignsubsequent non-declarative statements explicitlyto a processing block, such asSTART-OF-SELECTION, so that they can be accessed.
REPORT YSUBOOPS17 .
CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value 5.
methods : m1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD M1.
WRITE:/5 'I am M1 in C1'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : oref1 TYPE REF TO c1 .
CREATE OBJECT : oref1.
write:/5 oref1->w_num.
CALL METHOD : oref1->m1 .
-
7/30/2019 ABAP Objects 1
43/148
43
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Different places of implementing class
Class implemented at the end of
the programClass implemented after Definition
-
7/30/2019 ABAP Objects 1
44/148
44
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
Structure of a Class
The following statements define the structure of a
class:
A class contains components
Each component is assigned to a visibilitysection
Classes implement methods
-
7/30/2019 ABAP Objects 1
45/148
45
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
All components are declared in the declaration part of the
class.
When you define the class, each component is assigned to
one of the three visibility sections, which define theexternal interface of the class.
All of the components of a class are visible within theclass.
Instance components exist separately for each object inthe class
static components exist only once for the whole class,regardless of the number of instances.
All components that you can declare in classes can alsobe declared in interfaces
-
7/30/2019 ABAP Objects 1
46/148
46
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
Attributes:
Attributes are internal data fields within a class that canhave any ABAP data type.
The state of an object is determined by the contents of itsattributes.
One kind of attribute is the reference variable. Referencevariables allow you to create and address objects.
Instance Attributes: DATA
Static Attributes : CLASS-DATA
Static Attributes are accessible for the entire runtime of theclass.
-
7/30/2019 ABAP Objects 1
47/148
47
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
Methods
Methods are internal procedures in a class that define the behavior of anobject.
They can access all of the attributes of a class. This allows them to changethe data content of an object.
They are similar to function modules or procedures.
The private attributes of a class can only be changed by methods in thesame class.
In Definition Part Instance Methods: METHODS . Instance Methods can access all the attributes of a class and can trigger all
the events of a class.
Static Methods : CLASS-METHODS . Theycan only access static attributesand trigger static events.
Inimplementation Part.
METHOD ....
ENDMETHOD.
-
7/30/2019 ABAP Objects 1
48/148
48
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
Special Methods:
CONSTRUCTOR: Cannot call with CALL METHOD statement.
Called automatically when you create an object
CLASS_CONSTRUCTOR: Called when you first access the components of a class
Events: Objects or classes can use events to triggerevent
handler methods in other objects or classes. When an event is triggered, any number of event handler
methods can be called.
the handlerdetermines the events to which it wants toreact. There does not have to be a handler methodregistered for every event.
-
7/30/2019 ABAP Objects 1
49/148
49
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
The events of a class can be triggered in the methods of
the same class using the RAISE EVENT statement.
The event handler methods can be of the same or a
different class.
FOR EVENT OF . Addition
Events have a similar parameter interface to methods, butonly have output parameters.
These parameters are passed by the trigger (RAISE
EVENT statement) to the event handler method, which
receives them as input parameters.
The link between trigger and handler is established
dynamically in a program using the SET HANDLER
statement.
-
7/30/2019 ABAP Objects 1
50/148
50
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
The trigger and handlers can be objects or classes,
depending on whether you have instance or static eventsand event handler methods.
When an event is triggered, the corresponding eventhandler methods are executed in all registered handlingclasses.
Instance Events:EVENTS keyword. An instance event can only be triggered in an instance
method.
Static Events : CLASS-EVENTS
All methods (instance and static methods) can triggerstatic events.
Static events are the only type of event that can betriggered in a static method.
-
7/30/2019 ABAP Objects 1
51/148
51
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes : Class Components
Types:
You can define your own ABAP data types within
a class using the TYPES statement.
Types are not instance-specific, and exist once
only for all of the objects in a class.Constants:
Constants are special static attributes.
You declare them using the CONSTANTS
statement.
Constants are not instance-specific
-
7/30/2019 ABAP Objects 1
52/148
-
7/30/2019 ABAP Objects 1
53/148
-
7/30/2019 ABAP Objects 1
54/148
54
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Who can use a class?
class c2 definition inheritingfrom c1.
public section .methods : m2.
endclass.
class c2 implementation.
method m2.
write:/5 From subclass' ,w_num .
endmethod.
endclass.
REPORT YSUBOOPS17 .
CLASS c1 DEFINITION.
PUBLIC SECTION.
data : w_num type i value 5.
methods m1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
method m1.
write:/5 From class : ' , w_num.
endmethod.
ENDCLASS.
START-OF-SELECTION.
DATA :
oref1 TYPE REF TO c1 ,
oref2 type ref to c2 .
CREATE OBJECT : oref1.
write:/5 As an user ' ,
oref1->w_num.
Call method oref1->m1.
Call method oref2->m2.
Classitself
Subclassof theclass
External user
-
7/30/2019 ABAP Objects 1
55/148
55
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classes
-
7/30/2019 ABAP Objects 1
56/148
56
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ClassesREPORT demo_class_counter .
CLASS counter DEFINITION.
PUBLIC SECTION.METHODS: set IMPORTING value(set_value) TYPE i,
increment,get EXPORTING value(get_value) TYPE i.
PRIVATE SECTION.DATA count TYPE i.
ENDCLASS.
CLASS counter IMPLEMENTATION.METHOD set.count = set_value.
ENDMETHOD.METHOD increment.ADD 1 TO count.
ENDMETHOD.METHOD get.get_value = count.
ENDMETHOD.ENDCLASS.
DATA number TYPE i VALUE 5.DATA cnt TYPE REF TO counter.
START-OF-SELECTION.CREATE OBJECT cnt.CALL METHOD cnt->set EXPORTING set_value = number.DO 3 TIMES.CALL METHOD cnt->increment.
ENDDO.CALL METHOD cnt->get IMPORTING get_value = number.WRITE number.
-
7/30/2019 ABAP Objects 1
57/148
57
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Two Additions in Local Class Definition
Addition 1 : CLASS class DEFINITION DEFERRED.
Used to refer to a class at some point in a code and the class is notdefined before the line.
CLASS C2 DEFINITION DEFERRED.
CLASS C1 DEFINITION.
PUBLIC SECTION.
DATA O2 TYPE REF TO C2.
ENDCLASS.
CLASS C2 DEFINITION.
public section.
data : num type i value 5.
ENDCLASS.
start-of-selection.data : obj1 type ref to C1.
CREATE OBJECT obj1.
create object obj1->o2.
write:/5 obj1->o2->num .
-
7/30/2019 ABAP Objects 1
58/148
58
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Two Additions in Local Class Definition
Addition 2 : CLASS class DEFINITION LOAD
The compiler normally loads the description of a global class fromthe class library the first time you use the class in your program .However, if the first access to a global class in a program is to its
static components or in the definition of an event handler method ,you must load it explicitly using the statement CLASS classDEFINITION LOAD. This variant has no corresponding ENDCLASSstatement.
http://sapevent/http://sapevent/ -
7/30/2019 ABAP Objects 1
59/148
-
7/30/2019 ABAP Objects 1
60/148
60
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Handling
Each object has a unique identity and its own attributes.
Object References To access an object from an ABAPprogram, you use object references. Object references are
pointers to objects. In ABAP, they are always contained in
reference variables.
A reference variable that points to an object knows theidentity of that object. Users cannot access the identity of
the object directly.
Reference variable can occur as a component of a
structure or internal table as well as on its own.
There are two principal types of references: Classreferences and interface references
-
7/30/2019 ABAP Objects 1
61/148
61
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Handling
... TYPE REF TO
Creating Objects CREATE OBJECT .
Addressing the Components of Objects:
You can access the instance components of an object
using references in reference variables only. To access an attribute : ->
To call a method : CALL METHOD ->
You can access static components using the class nameas well as the reference variable.
It is also possible to address the static components of aclass before an object has been created.
-
7/30/2019 ABAP Objects 1
62/148
62
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Handling
Addressing a static attribute : =>
Calling a static method : CALL METHOD
=>
Within a class, you can use the self-reference ME toaccess the individual components:
To access an attribute in the same class: ME->
To call a method in the same class: CALL
METHOD ME->
Self references allow an object to give other objects areference to it.You can also access attributes in methodsfrom within an object even if they are obscured by local
attributes of the method.
-
7/30/2019 ABAP Objects 1
63/148
63
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Handling
Assigning References
When you assign a reference to a different
reference variable, their types must be either
compatible or convertible.
= and refer to same class.
type ref to root class OBJECT.
Inheritance & Interface situations.
Class OBJECT is just a container. You cannot
access components of class with OBJECT
reference
-
7/30/2019 ABAP Objects 1
64/148
-
7/30/2019 ABAP Objects 1
65/148
65
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Object Handling
-
7/30/2019 ABAP Objects 1
66/148
66
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
Declaring Methods :
You can declare methods in the declaration part of a classor in an interface.
To declare instance methods, use the following statement:
METHODS
IMPORTING.. [VALUE(][)] TYPE type
[OPTIONAL]..EXPORTING.. [VALUE(][)] TYPE type[OPTIONAL]..
CHANGING.. [VALUE(][)] TYPE type[OPTIONAL]..
RETURNING VALUE()
EXCEPTIONS.. ..and the appropriate additions.To declare static methods, use the following statement:
CLASS-METHODS ...
Both statements have the same syntax.
-
7/30/2019 ABAP Objects 1
67/148
67
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
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.If you useit, you cannot use EXPORTING or CHANGINGparameters.
You can use exception parameters
(EXCEPTIONS) to allow the user to react to errorsituations when the method is executed.
-
7/30/2019 ABAP Objects 1
68/148
68
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
Implementing Methods :
METHOD .
...
ENDMETHOD.
Static methods can work with only the static attributes of a
class.Calling Methods :
CALL METHOD EXPORTING... =....
IMPORTING... =....
CHANGING ... =....
RECEIVING r = h
EXCEPTIONS... = rc i...
-
7/30/2019 ABAP Objects 1
69/148
69
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Implementing Methods
All methods declared in the definition part of a classshould be implemented in the implementation section of
the class within the following block:-
METHOD ....
ENDMETHOD.
One should not specify any interface parameters at the
time of implementation, since these are defined in the
method declaration.
The interface parameters of a method behave like
local variables within the method implementation. You
can define additional local variables within a methodusing the DATA statement.
-
7/30/2019 ABAP Objects 1
70/148
-
7/30/2019 ABAP Objects 1
71/148
71
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
Within the implementation part of a class, use
CALL METHOD ...
Visible instance & static methods can be called from
outside the class using
CALL METHOD ->...
Visible static methods can be called from outside the classusing
CALL METHOD =>...
where is the name of the relevant class.
You need not import the output parameters into yourprogram using the IMPORTING or RECEIVING addition. C
-
7/30/2019 ABAP Objects 1
72/148
72
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
If the interface of a method consists only of a
single IMPORTING parameter, you can use
CALL METHOD ( f).
The actual parameter is passed to the input
parameters of the method. If the interface of a method consists only of
IMPORTING parameters, you can use
CALL METHOD (.... =....).
-
7/30/2019 ABAP Objects 1
73/148
73
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Dynamic Method Calls
Instance, self-referenced, andstatic methods can all be calleddynamically; the class name forstatic methods can also be
determined dynamically: oref->(method)
me->(method)
class=>(method)
(class)=>method
(class)=>(method)
D l i d C lli M h d
-
7/30/2019 ABAP Objects 1
74/148
74
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
Event Handler Methods :
Event handler methods cannot be called using the CALLMETHOD statement.Instead, they are triggered usingevents.
You define a method as an event handler method usingthe addition
... FOR EVENT OF ...in the METHODS or CLASS-METHODS statement.
The interface may only consist of IMPORTINGparameters.
Each IMPORTING parameter must be an EXPORTINGparameter of the event
The attributes of the parameters are defined in thedeclaration of the event (EVENTS statement) andare adopted by the event handler method.
D l i d C lli M h d
-
7/30/2019 ABAP Objects 1
75/148
75
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
Constructors:
Constructors are special methods that cannot be calledusing CALL METHOD. They are called automatically by the system to set the
starting state of a new object or class. Constructors are methods with a predefined name.
To use them, you must declare them explicitly in theclass.Instance constructor : You declare it in the public section as
follows:
METHODS CONSTRUCTORIMPORTING.. [VALUE(][)] TYPE type
[OPTIONAL]..EXCEPTIONS.. .
and implement it in the implementation section like anyother method.
D l i d C lli M h d
-
7/30/2019 ABAP Objects 1
76/148
76
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Declaring and Calling Methods
The system calls the instance constructor once for each
instance of the class, directly after the object has beencreated in the CREATE OBJECT statement.
You pass the parameters to the constructor and handle the
exceptions in CREATE OBJECT stmt.
static constructor : CLASS-METHODS CLASS_CONSTRUCTOR.
The static constructor has no parameters.
The system calls the static constructor once for each class,
before the class is accessed for the first time.
The static constructorcannot therefore access thecomponents of its own class.
I t C t t
-
7/30/2019 ABAP Objects 1
77/148
77
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Instance Constructor
Executed once for each instance.
Called automatically, immediately after theCREATE OBJECTstatement.
Can contain an interface with
IMPORTINGparameters andEXCEPTIONS , but cannot have anyEXPORTING/CHANGING/RETURNINGparameters .
The interfaces are defined using the same
syntax as for normal methods in theMETHODS statement. To transferparameters and handle exceptions, use theEXPORTINGandEXCEPTIONSadditionsto the CREATE OBJECT statement .
Static Constructor
-
7/30/2019 ABAP Objects 1
78/148
78
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Static Constructor
Static methods, declared as CLASS-METHODS :
CLASS_CONSTRUCTOR in the public section of the class
definition and are also implemented in the
implementation part.
Has no interface parameters and cannot triggerexceptions.
Executed once in each program. It is calledautomatically for the class before it is accessed forthe first time - that is, before one of the followingactions:
CREATE OBJECT obj from the class.
Call a static method : [CALL METHOD] class=>meth.
Registering a static event handler method using SETHANDLER class=>meth for obj.
Registering an event handler method for a static event
of the class class.
Addressing a static attribute with class=>a.
REPORT YSUBOOPS2.CLASS c1 DEFINITION .
PUBLIC SECTION.CLASS-DATA : NUM TYPE I VALUE 5.
CLASS-METHODS:CLASS_CONSTRUCTOR.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD CLASS_CONSTRUCTOR.WRITE:/5 'I am class
constructor'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
WRITE:/5 C1=>NUM.
S lf R f
http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/http://sapevent/ -
7/30/2019 ABAP Objects 1
79/148
79
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Self-Reference
Internally, each method has an implicit self-reference variable, the reserved word me
A method can access the components of itsclass simply by their name; however,
It may use me simply for clarity
If a method declares a local variable withthe same name as one of the classcomponents, to avoid ambiguity it mustuse me to address the variable originallybelonging to the class.
A method must use me to export areference to itself (that is, its object)
-
7/30/2019 ABAP Objects 1
80/148
2005 Intelligroup, Inc. Confidential and Proprietary
DAY 3
I h it
-
7/30/2019 ABAP Objects 1
81/148
81
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Inheritance allows you to derive a new class from an
existing class. CLASS DEFINITION INHERITING FROM
.
The new class inherits all of the components
of the existing class . However, only the public and protected components of
the super class are visible in the subclass.
You can declare private components in a subclass thathave the same names as private components of the
super class.Each class works with its own privatecomponents.
-
7/30/2019 ABAP Objects 1
82/148
82
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Creating Subclass
Subclasses can be created from its
superclass using the syntax:-
CLASS DEFINITIONINHERITING FROM .
Subclass inherits all the public and protected
components of the superclass.
Superclass should not be declared as a
FINAL class.
Inheritance
-
7/30/2019 ABAP Objects 1
83/148
83
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
You can add new components to the subclass.
This allows you to turn the subclass into aspecialized version of the super class.
A class can have more than one direct subclass,
but it may only have one direct super class. This
is called single inheritance.
The root node of all inheritance trees in ABAPObjects is the predefined empty class OBJECT.
Inheritance
-
7/30/2019 ABAP Objects 1
84/148
84
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Redefining Methods: you can use the REDEFINITION
addition in the METHODS statement to redefine aninherited public or protected instance method in asubclass and make its function more specialized.
The implementation of the redefinition in the subclass
obscures the original implementation in the super class.
Any reference that points to an object of the subclass uses
the redefined method, even if the reference was defined
with reference to the superclass.
This particularly applies to the self-reference ME->.
Within a redefined method, you can use the pseudoreference SUPER-> to access the obscured method.
M dif i h d i b l
-
7/30/2019 ABAP Objects 1
85/148
85
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Modifying methods in subclass
To redefine a public/protected method of a
superclass in one of its subclasses, use the syntax
in the subclass definition:-
METHOD REDEFINITION
The interface and visibility of a methodcannot be changed while redefining it.
The method declaration and implementation in
the superclass is not affected when you redefine
the method in a subclass.
Inheritance
-
7/30/2019 ABAP Objects 1
86/148
86
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Abstract and Final Methods and Classes :
An abstract method is defined in an abstract class andcannot be implemented in that class.
A final method cannot be redefined in a subclass.
References to Subclasses and Polymorphism:
Reference variables defined with reference to a superclass can also contain references to any of its subclasses.
A reference variable defined with reference to a superclass or an interface implemented by a super class cancontain references to instances of any of its subclasses.
Reference variable defined with reference to OBJECT cancontain reference to any reference variable.
CREATE OBJECT statement with type addition
Abstract Methods and Classes
-
7/30/2019 ABAP Objects 1
87/148
87
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Abstract Methods and Classes
One cannot create an object from an abstractclass.Only subclasses can be derived from them.
CLASS DEFINITION ABSTRACT.
Abstract methods cannot be implemented inthe same class. Only the subclasses of that
class can implement it.
METHODS .ABSTRACT
Any class containing an abstract method hasto be an abstract class. All subsequentsubclasses that do not implement the method
must also be abstract. To implement an abstract
method in a subclass, one need to redefine this
subclass using the REDEFINITION addition.
Final Methods and Classes
-
7/30/2019 ABAP Objects 1
88/148
88
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Final Methods and Classes
Final classes cannot have subclasses. Only the
class can be instantiated.
CLASS DEFINITION FINAL.
A final method cannot be redefined in
subclasses
METHODS .FINAL
Inheritance
-
7/30/2019 ABAP Objects 1
89/148
89
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Inheritance and Static Attributes
In terms of inheritance, static attributes are not
assigned to a single class, but to a part of the
inheritance tree.
When you address a static attribute that belongsto part of an inheritance tree, you always address
the class in which the attribute is declared,
irrespective of the class you specify in the class
selector.
This is particularly important when you call the
static constructors of classes in inheritance.
-
7/30/2019 ABAP Objects 1
90/148
-
7/30/2019 ABAP Objects 1
91/148
Inheritance
-
7/30/2019 ABAP Objects 1
92/148
92
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Supplying values using CREATE OBJECT in inheritance
Supplying values using
CALL METHOD SUPER->CONSTRUCTOR in inheritance.
The instance constructor of a subclass is divided into two
parts by the CALL METHOD SUPER->CONSTRUCTOR
statement. In the statements before the call, theconstructor behaves like a static method
In a constructor method, the methods of the subclasses of
the class are not visible.( REDEFINITION not effective )
Static Constructors
Inheritance and Instance
-
7/30/2019 ABAP Objects 1
93/148
93
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Constructors
1 None of the superclass and subclass haveexplicit constructor.
Not required
2 Superclass have explicit constructor, butsubclass does not have any explicit constructor.
Not required
3 Superclass does not have an explicitconstructor, but subclass have one.
Required
4 Both the superclass and subclass have explicitconstructor
Required
Superclasses and/or subclasses can have explicit constructors of their own.Constructor of a subclass sometimes have to call the constructor of the superclass
using : CALL METHOD : SUPER->CONSTRUCTORdepending on the following:-
Polymorphism via Inheritance
-
7/30/2019 ABAP Objects 1
94/148
94
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
y p
With inheritance, a reference variable
defined with respect to a class may not only
point to instances of that but also to
instances of subclasses of the same. One
can even create subclass objects using a
reference variable typed with respect to a
super class.
Polymorphism through inheritance can be
achieved by playing with static and dynamic
type of a reference variable.
Instances of a subclass may be used
through the super class's interface. When
this is done, a client can't access all
components defined in the subclass, only
those inherited from the respective super
class.
class c1 definition.
. . . . . . .
endclass.
class c1 implementation.
. . . . . .
endclass.
class c2 definition inheriting fromc1.
. . . . . .
endclass.
class c2 implementation.
. . . . . . .
endclass.
start-of-selection.
data : oref1 type ref to c1,
oref11 type ref to c1,
oref2 type ref to c2.
create object oref1 type c2 .
create object oref2.
oref11 = oref2.
write:/5 oref1->num ,
oref11->num .
Inheritance
-
7/30/2019 ABAP Objects 1
95/148
95
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Inheritance Overview:
Inheritance
-
7/30/2019 ABAP Objects 1
96/148
96
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Inheritance
Inheritance and Reference Variables
Interfaces
-
7/30/2019 ABAP Objects 1
97/148
97
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Interfaces
Interfaces are independent structures that you can implement ina class to extend the scope of that class.
a universal point of contact. They provide one of the pillars of polymorphism, since they allow
a single method within an interface to behave differently indifferent classes.
Global& Local Interfaces
The definition of a local interface is enclosed in thestatements:
INTERFACE .
...
ENDINTERFACE.
The definition contains the declaration for all components
(attributes, methods, events) of the interface. They automatically belong to the public section of the class in
which the interface is implemented.
Defining Interfaces
-
7/30/2019 ABAP Objects 1
98/148
98
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Defining Interfaces
Can be declared globally or locally within aprogram.
Locally declared in the global portion of a program
using:-
INTERFACE .
...
ENDINTERFACE.
The definition contains the declaration for all
components (attributes, methods, events) of the
interface.
Interfaces are included in the public section of a
class.
Interfaces do not have an implementation part,
since their methods are implemented in the class that
implements the interface.
report ysubdel .
interface i1.data : num type i .methods : meth1.endinterface.
class c1 definition.public section.methods : meth1.interfaces : i1.
endclass.
class c1 implementation.method : meth1.write:/5 'I am meth1 in c1'.endmethod.
method i1~meth1.write:/5 'I am meth1 from i1'.
endmethod.endclass.
start-of-selection.data : oref type ref to c1. create object oref.write:/5 oref->i1~num.call method oref->meth1.call method oref->i1~meth1.
Interfaces
-
7/30/2019 ABAP Objects 1
99/148
99
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Interfaces
Interfaces do not have instances.
To implement an interface in a class, use the statementINTERFACES .
in the declaration part of the class.
A component of an interface can beaddressed as though it were a member of the class underthe name .
Interface References Addressing Objects Using Interface ReferencesUsing the class reference variable : To access an attribute : ->
To call a method : CALL METHOD ->
Interfaces
-
7/30/2019 ABAP Objects 1
100/148
100
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Interfaces
Using the interface reference variable :
To access an attribute : < iref>-> To call a method : CALL METHOD ->
Addressing a constant : < intf>=> (Cannot useclass name).
Addressing a static attribute
: < class>=>
Calling a static method : CALL METHOD=>
(Cannot use Interface method ).
casting operator (?= )
?=
For the casting to be successful, the object to which pointsmust be an object of the same class as the type of the classvariable .
-
7/30/2019 ABAP Objects 1
101/148
2005 Intelligroup, Inc.Confidential and Proprietary
DAY 4
Triggering and Handling Events
-
7/30/2019 ABAP Objects 1
102/148
102
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Triggering and Handling Events
To trigger an event, a class must
Declare the event in its declaration part
Trigger the event in one of its methods
EVENTS EXPORTING... VALUE()
TYPE type [OPTIONAL].. To declare static events, use the following
statement:
CLASS-EVENTS ...
Both statements have the same syntax.
Triggering and Handling Events
-
7/30/2019 ABAP Objects 1
103/148
103
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Triggering and Handling Events
Triggering Events
RAISE EVENT EXPORTING... =
...
The self-reference ME is automatically passed to
the implicit parameter SENDER.
Handling Events
Events are handled using special methods. To
handle an event, a method must
1. be defined as an event handler method forthat event
2. be registered at runtime for the event.
-
7/30/2019 ABAP Objects 1
104/148
Triggering and Handling Events
-
7/30/2019 ABAP Objects 1
105/148
105
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Triggering and Handling Events
Handler Table:
What is an Exception?
-
7/30/2019 ABAP Objects 1
106/148
106
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
What is an Exception?
An exception is a situation that occurs during the execution of anABAP program, which renders a normal program continuationpointless.
Exceptions can be detected at the time of program compilation or at
runtime.If the exception detected at runtime is not handled properly
by the program itself, we get a short dump and the executionterminates.
Classification of Exceptions
-
7/30/2019 ABAP Objects 1
107/148
107
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Classification of Exceptions
Exceptions of various kinds can be broadly classified as :-
Exceptions that can be handled.
Exceptions that cannot be handled.
Exceptions that can be handled indicate error situations in the runtime
environment or in the ABAP program, in the case of which the programexecution can be continued - by handling the exception in the ABAP
program - without the system reaching a critical condition. If such a
situation is not handled a runtime error will occur.
Exceptions that cannot be handled indicate critical error situations inthe runtime environment, which cannot be handled with/by ABAP means
and always cause a runtime error. Database space problem can be anexample of such category.
Traditional Ways of Catching RuntimeExceptions
-
7/30/2019 ABAP Objects 1
108/148
108
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Exceptions
Areas Brief Overview
In ABAP catch system-exceptions = .. . . . . .
Endcatch.
If sy-subrc = .
< exception handling statements>
Endif.
In functionmodule
Creating exceptions for function module, raising them at
appropriate points in the FM , assigning different sy-subrc
values for each exceptions at the time of the FM call and later
dealing with them.
In Methods Creating different exceptions at the time of declaring methods,raising those exceptions within the method, assigning differentsy-subrc values at the time of method call and later dealing
with those values.
What is Class-based exceptionh dli ?
-
7/30/2019 ABAP Objects 1
109/148
109
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
handling?
In Class-based exceptions handling approach, exceptions are generally
represented by objects of exception classes. There are pre-defined
exception classes for error situations in the runtime environment .
Users can also define own exception classes globally/locally, if required
and can raise them using RAISE EXCEPTION statement.
The runtime environment only causes exceptions that are based on pre-
defined classes, while in ABAP programs one can use raise pre-defined as
well as user-specific exception classes.
Class-based exceptions are handled using the control structure TRY ...ENDTRY.
Class-based exceptions in procedures can be propagated to the caller inthe definition of the interface using the RAISING addition, if the exception isnot to be handled in the procedure.
TRYCATCHENDTRY
http://sapevent/http://sapevent/http://sapevent/http://sapevent/ -
7/30/2019 ABAP Objects 1
110/148
110
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
Class-based exceptions are handled using TRYCATCHENDTRYblock.
TRY.
< code to be checked forexception>
CATCH cx1 .cxn [ into
ref].
< exception handling code>.
ENDTRY.
REPORT YSUBCLASS_EXCEPTION.
DATA: i TYPE i VALUE 1.
START-OF-SELECTION.
TRY.
i = i / 0.
CATCH cx_sy_zerodivide.
write:/5 'Divide by zero caught'.
ENDTRY.
Class-Based ExceptionsSAPException Classes (2)
-
7/30/2019 ABAP Objects 1
111/148
111
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
Exception Classes (2)
CX_STATIC_CHECK: For exceptions that have to be declared. This type should be chosen if you
want to make sure that this exception is always dealt with and if a localexception handler has a chance to do something useful in an exceptionsituation
Corresponding exceptions must either be handled or forwarded explicitlywith the RAISING addition and this is checked at syntax check
CX_DYNAMIC_CHECK: For exceptions that do not have to be declared
Exceptions must be handled or explicitly forwarded with the RAISINGaddition though this is not checked at syntax check. Exceptions of this typeare checked at runtime only
Useful for potential error situations that do not have to be handled, sincethe program logic can more or less exclude them. Example:
cx_sy_zerodivide Most of the CX_SY_exceptions inherit from this class
CX_NO_CHECK:
For exceptions that must not be declared (i.e. resource bottlenecks)
Can be handled but not forwarded with RAISING. Otherwise will bepropagated through call chain automatically
Not checked by syntax check or runtime processing
SAP Exception Classes
-
7/30/2019 ABAP Objects 1
112/148
112
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
p
SAP provided exception-classes are derived from the specific classCX_ROOT and have the prefix CX_.
Exception classes are normal classes with one limitation:-
Apart from the constructor, no methods can be defined for them. However,
CX_ROOT has some pre-defined methods available, which can then be
inherited by all exception classes.
Component Name (M)ethod/(A)ttrib
ute
Description
GET_TEXT M Returns a text description of theexception
GET_SOURCE_POSITION M Returns the point at which theexception occurred
TEXTID A Used to define different texts forexceptions of a particular exceptionclass. Affects the result of themethod GET_TEXT.
SAP Exception Classes
-
7/30/2019 ABAP Objects 1
113/148
113
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
p
Component Name (M)ethod/(A)ttri
bute
Description
PREVIOUS A If one exception is mapped toanother, this attribute stores theoriginal exception, which allows thesystem to build a chain ofexceptions.
KERNEL_ERRID A Contains the name of theappropriate runtime error if theexception was triggered from thekernel. If the exception was raisedusing a RAISE EXCEPTION, thisattribute is initial.
TEXTID A Used to define different texts forexceptions of a particular exceptionclass. Affects the result of themethod GET_TEXT.
Nested TryCatchEndtry Blocks
http://sapevent/http://sapevent/ -
7/30/2019 ABAP Objects 1
114/148
114
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
y y
Try block
Catch block
Catch block
Cleanup block
TRY.TRY.
CATCH cx_class INTO oref
CATCH cx_class INTO oref
CLEANUP.
ENDTRY.
CATCH cx_class INTO oref.
CATCH cx_class INTO oref.
CLEANUP.
ENDTRY.
.
Try block
Cleanup block
.
Catch block
Catch block
.
CLEANUP
-
7/30/2019 ABAP Objects 1
115/148
115
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
Used within a TRYENDTRY
BLOCK , after all CATCH statements.
Each TRY block can contain
maximum of one CLEANUP area.
Used to release the external resourceswhen exception detected in a TRY
block is not handled within the block ,
but is caught further up in the call
hierarchy.
Possible only in cases of nested TRYblocks.
Report ysubdel.
data : w_num type i.
try.
try .
w_num = 5 / 0 .
cleanup.
write:/5 In cleanup.
endtry .
catch cx_sy_zerodivide.
write:/5 Div. By zero!.
endtry.
In cleanup
Div. by zero!
Creating Local Exception Class in aprogram
-
7/30/2019 ABAP Objects 1
116/148
116
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
program
To create a local exception class in a program and use it, follow the stepsoutlined below.
Step 1 :- Create a subclass from global exception class in your program.
REPORT YSUBCLASS_EXCEPTION_3.
CLASS CX_SOME_EXCEPTION DEFINITION INHERITING FROMCX_STATIC_CHECK.
public section.
methods : meth1.
ENDCLASS.
Creating Local Exception Class in aprogram
-
7/30/2019 ABAP Objects 1
117/148
117
2012/11/26 2005 Intelligroup, Inc.Confidential and proprietary
program
Step 2 :- Implement methods of the subclass which will raise exception
CLASS CX_SOME_EXCEPTION IMPLEMENTATION.
method : meth1.
write:/5 'I am a method in exception'.
endmethod.
ENDCLASS.
Step 3 :- Define another class which will call the exception class.
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1 raising cx_some_exception .
ENDCLASS.
Creating Local Exception Class in aprogram
-
7/30/2019 ABAP Objects 1
118/148
118
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
program
Step 4 :- Implement the method of the other class which will raiseexception of the locally declared exception class.
CLASS SOME_CLASS IMPLEMENTATION.
METHOD m1.
RAISE EXCEPTION TYPE CX_SOME_EXCEPTION.ENDMETHOD.
ENDCLASS.
Creating Local Exception Class in aprogram
-
7/30/2019 ABAP Objects 1
119/148
119
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
program
Step 5 :- Create an object of the other class and call its method whichwill raise the exception
DATA: c1 TYPE REF TO SOME_CLASS.
START-OF-SELECTION.
TRY.CREATE OBJECT c1.
c1->m1( ).
CATCH CX_some_exception.
write:/5 'Exception caught'.
ENDTRY.
Class-Based Exceptions Debug Mode
-
7/30/2019 ABAP Objects 1
120/148
120
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Exception has
occurred and has
been handled
Class-Based Exceptions Debug Mode
-
7/30/2019 ABAP Objects 1
121/148
121
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Trigger point of
exception
Display Exception
Object
Class-Based Exceptions Debug Mode
-
7/30/2019 ABAP Objects 1
122/148
122
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Class-Based ExceptionsCreating aGlobal Exception Class (1)
-
7/30/2019 ABAP Objects 1
123/148
123
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Global Exception Class (1)
Enter class name Click Create
Note Superclass
and class type
SE24
Class-Based ExceptionsCreating aGlobal Exception Class (2)
-
7/30/2019 ABAP Objects 1
124/148
124
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Global Exception Class (2)
Note the 2 attributes inherited from cx_root superclass
textid Used to define different texts for exceptions of a particular
class. Affects the result of method get_textprevious If one exception is mapped to another, this attribute can
store the original exception. If a runtime error occurs, the short dump
contains the texts belonging to all the exceptions in the chain
Go to
MethodsTab
Class-Based ExceptionsCreating aGlobal Exception Class (3)
-
7/30/2019 ABAP Objects 1
125/148
125
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Three methods are inherited from CX_ROOT
get_text, get_longtext Returns the textual representation as a string,according to the system language of the exception
get_source_position Returns the program name, include name, and linenumber reached where the exception was raised
A constructor method is automatically generated
Double click on
the constructor
method to view
code
Class-Based ExceptionsCreating aGlobal Exception Class (4)
-
7/30/2019 ABAP Objects 1
126/148
126
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
p ( )
Call to the constructor of superclasses is automatically
generated
Click on
previous
object button
to return tomethods tab
Class-Based ExceptionsCreating aGlobal Exception Class (5)
-
7/30/2019 ABAP Objects 1
127/148
127
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
First add an attribute to
the error class and
activate the class
Then return to the
methods tab and click on
the constructor again
Class-Based ExceptionsCreating aGlobal Exception Class (6)
-
7/30/2019 ABAP Objects 1
128/148
128
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
A line has been added to the constructor to initialize the new attribute.
This attribute will be available in the error object at runtime and will contain
the value that is passed to the constructor when the exception is raised
Click on
previous
object
button toreturn to
methods tab
Class-Based ExceptionsCreating aGlobal Exception Class (7)
-
7/30/2019 ABAP Objects 1
129/148
129
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Go to the Texts tab and add a text for theexception ID.
Class-Based Exceptions Creating a Global
Exception Class (8)
-
7/30/2019 ABAP Objects 1
130/148
130
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
The texts are stored in the Online Text Repository (OTR). The exceptionobject contains only a key that identifies the text (with system language)
The default text has the same name as the name of the exception class, inthis case ZCX_SOME_EXCEPTION.
You might wish to create an alternate text for the exception. That text can beentered on this screen with a new exception ID and can be displayed bypassing this value to the parameter textid of the exception constructor.
Class-Based ExceptionsCreating aGlobal Exception Class (9)
-
7/30/2019 ABAP Objects 1
131/148
131
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
After performing a syntax check and
adding the texts to the OTR, return to the
Attributes tab
Class-Based ExceptionsCreating aGlobal Exception Class (10)
-
7/30/2019 ABAP Objects 1
132/148
132
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Dont forget to activate the object!Note that the text IDs have been added to the attributes page as
class constants
-
7/30/2019 ABAP Objects 1
133/148
133
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
DAY 8
5 Reasons OO Programming is betterthan
Procedural Programming
-
7/30/2019 ABAP Objects 1
134/148
134
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Procedural Programming
Data Encapsulation
Instantiation
Code Reuse
Interfaces Events
3 ReasonsABAP Objects is BetterABAP
-
7/30/2019 ABAP Objects 1
135/148
135
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ABAP Objects is more Explicit and Simpler to Use.
ABAP Objects has a Stricter Syntax Check in
Classes.
ABAP Objects Provides Access to New ABAP
Technology.
ABAP Objects is more Explicit andSimpler to Use
-
7/30/2019 ABAP Objects 1
136/148
136
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ABAP Objects is much simpler and less error-prone
because :
Classes contains attributes and methods.
Objects are instances of Classes.
Objects are addressed via references.
Objects have clearly defined interfaces.
Comparison between Procedural ABAPand Object oriented ABAP
-
7/30/2019 ABAP Objects 1
137/148
137
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Procedural ABAP
Contains Obsolete
Statements.
Supports Overlapping
and
and some specialized
objects.
Shows Implicit Behavior.
Appears difficult to
learn.
Object Oriented
ABAP
Prohibits obsoletestatements and additions.
Requires implicit syntaxcompletions to be explicit.
Detecting and
preventing
incorrect data handling.
Improve your Procedural Programmingusing ABAP Objects
-
7/30/2019 ABAP Objects 1
138/148
138
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Use methods as much as possible.
Replace the use of Subroutines using static methods.
Use function modules only when technically necessary.
Disentangle procedural ABAP from ABAP Objects.
Decouple screen programming from application
programming.
Never uncheck the Unicode checks active checkbox.
Stricter Syntax Check
-
7/30/2019 ABAP Objects 1
139/148
139
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Stricter Syntax Check
-
7/30/2019 ABAP Objects 1
140/148
140
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Internal Tables definition
-
7/30/2019 ABAP Objects 1
141/148
141
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Database access
-
7/30/2019 ABAP Objects 1
142/148
142
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Explicit Typing
-
7/30/2019 ABAP Objects 1
143/148
143
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Data Handling
-
7/30/2019 ABAP Objects 1
144/148
144
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Unicode Restrictions
-
7/30/2019 ABAP Objects 1
145/148
145
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Unicode Restrictions
-
7/30/2019 ABAP Objects 1
146/148
146
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
ABAP Objects Provides Access toNew ABAP Technology
-
7/30/2019 ABAP Objects 1
147/148
147
2012/11/26 2005 Intelligroup, Inc. Confidential and proprietary
Frameworks for user dialogs such as SAP Control
Framework (CFW), BSP , Desktop Office Integration(DOI) etc.
Framework for persisting data in the database(Object Services) and Shared Objects (area classes).
Service classes such as
CL_GUI_FRONTEND_SERVICES for working withdata at the presentation server.
Language related classes such as Run Time TypeServices (RTTS), or CL_ABAP_EXPIMP subclassesfor extended IMPORT/EXPORT functionality.
-
7/30/2019 ABAP Objects 1
148/148
Thank You