Jayanth.C
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
2
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
3
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Unstructured Programming.
Procedural Programming.
Object Oriented Programming.
4
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Unstructured Programming
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.
The program stands for a sequence of commands which modify data that is global throughout the whole program.
Disadvantages Difficult to manage once the program becomes large. Same sequence of statements are repeated at multiple places, if they are needed at multiple locations.
Confidential and proprietary
2012/11/26
Procedural Programming
report ysubdel. DATA : sal type p decimals 2 , itax type p decimals 2 , net_sal type p decimals 2. sal = 12000. PERFORM sub_calc_tax USING sal itax net_sal. sal = 3500. PERFORM sub_calc_tax USING sal itax net_sal. FORM sub_calc_tax USING P_SAL P_ITAX P_NET_SAL. IF p_sal lt 5000 . p_itax = 0. ELSE. p_itax = sal * '0.01'. ENDIF. p_net_sal = p_sal - p_itax.
A procedure call is used to invoke the procedure. After the sequence is processed, flow of control proceeds right after the position where the call was made.
2012/11/26
7
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
8
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Business Server Pages GUI Control Framework Office Integration BADI, Workflow Generic Programming XML Transformations Email, Shared Objects, Persistent objects New ABAP Editor, Code Inspector, CATT
9
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
10
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
All objects contains state and behavior What they can do and what changes when they do
Software objects represent these as: Data ( like 4GL variables ) Methods
2005 Intelligroup, Inc.
2012/11/26
2012/11/26
Customer
SalesOrder
Cricket Team
Your Desktop PC
2012/11/26
Object-oriented Programming
Object-oriented programming is a method of
implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class... Grady Booch
14
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
15
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
16
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Attributes
Captain, VC, WC, FB1,FB2,FB3 SP1,SP2,SUB Account Number, Balance, CreditLimit CheckBooks
Methods
DoSingle,DoDouble DoBowl,DoCatch DoRunout,HitSix, Doplay,HitBowndary ATM_Transfer, E_transfer, Withdraw, Check_Credit_limit, Issue_check_book, Track_transactions
ICICI S.A.
Production
2012/11/26
Encapsulation
Hierarchies
18
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Abstraction
Public View of an Object
Abstraction is used to manage complexity Focus on the essential characteristics Eliminate the details Find commonalities among objects
2012/11/26
Abstraction - Example
Object: Automobile
Start Stop Drive PumpFuel
20
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Encapsulation
Hide Implementation Details
2012/11/26
Encapsulation - Example
Implementation
Object: Splendor
Outside View
2012/11/26
2012/11/26
Object: Splendor
EngineNum, Gear#, Fuel_MrtRd, SpeedometrRd StartEngine(), StopEngine(),Consu me_Diesel(), Move_Wheel())
Start(), Stop() Pump_Fuel(), Change_gear()
2005 Intelligroup, Inc.
This change was easy because users of the object will not be affected.
2012/11/26
2012/11/26
Hierarchies - Example
Automobile is a is a is a
26
Confidential and proprietary
2012/11/26
Hierarchies - Example
Automobile uses Engine (Aggregation)
Automobile
references Engine
27
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
28
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes and objects are used to model real world entity. Methods inside the classes perform the functions. Data used by the classes are protected between them.
29
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Defining a Referrence
Creating an Object
Method implementation
2005 Intelligroup, Inc.
Calling a Method
30
Confidential and proprietary
2012/11/26
Comparison between Procedural and Object Oriented Programming Features Procedure Oriented approach Emphasis on tasks Object Oriented approach
Emphasis Modularization
Emphasis on data
Programs are divided Programs are organized into smaller programs into classes and objects known as functions and the functionalities are embedded into methods of a class. Most of the functions share global data Data can be hidden and cannot be accessed by external sources.
Data security
Extensibility
Relatively more time New data and functions consuming to modify can be easily added for extending existing whenever necessary 31 Confidential and proprietary functionality. 2012/11/26
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
32
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Events:- A mechanism set within a class which can help a class to trigger
methods of other class.
33
Confidential and proprietary
2012/11/26
34
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Lets Design
35
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
DAY 2
Classes
Classes are templates for objects. Conversely, you can say that the type of an object is the same as its class. components of the class describe the state and behavior of objects. Local and Global Classes: Classes in ABAP Objects can be declared either globally or locally. You define global classes and interfaces in the Class Builder (Transaction SE24) in the ABAP Workbench. They are stored centrally in class pools in the class library in the R/3 Repository.
37
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes
Local classes are defined within an ABAP program. Local classes and interfaces can only be used in the program in which 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 of a 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.
38
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes
Defining Local Classes: A complete class definition consists of a declaration part and, if required, an implementation part. The declaration part of a class <class> 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.
39
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
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 <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 not accessible.
40
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Defined in the global area of a local program :CLASS <class name> DEFINITION. .. ENDCLASS.
All the attributes , methods, events and interfaces are declared here.
Cannot be declared inside a subroutine/function module.
2012/11/26
ENDCLASS.
CLASS c1 IMPLEMENTATION. METHOD M1. WRITE:/5 'I am M1 in C1'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. CREATE OBJECT : oref1. write:/5 oref1->w_num. CALL METHOD : oref1->m1 .
DATA : oref1 TYPE REF TO c1 . If the latter is adopted, one must then assign
subsequent non-declarative statements explicitly to a processing block, such as START-OFSELECTION, so that they can be accessed.
42
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
43
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes
Structure of a Class The following statements define the structure of a class: A class contains components Each component is assigned to a visibility section Classes implement methods
44
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
46
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
2012/11/26
2012/11/26
2012/11/26
2012/11/26
2012/11/26
Sections of a Class
CLASS C1 DEFINITION. PUBLIC SECTION. DATA: METHODS: EVENTS: PROTECTED SECTION. DATA: METHODS: EVENTS: PRIVATE SECTION. DATA: METHODS: EVENTS: ENDCLASS.
2005 Intelligroup, Inc.
There is no default visibility section in a class. This sequence of visibility must be maintained in a class
A Class puts its components under three distinct sections:Public Section:- Components placed here form the external interface of the class they are visible to all users of the class as well as to methods within the class and to methods of subclasses* Protected Section:- Components placed in protected section are visible to the children of the class(subclass) as well as within the class Private Section:-Components 53 Confidential and proprietary 2012/11/26 placed here are accessible by the
Externa l user
REPORT YSUBOOPS17 . CLASS c1 DEFINITION. PUBLIC SECTION. data : w_num type i value 5. methods m1. ENDCLASS.
class c2 definition inheriting from c1. public section . methods : m2. endclass. class c2 implementation.
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.
CLASS c1 IMPLEMENTATION. method m1. write:/5 From class : ' , w_num. endmethod. ENDCLASS.
54
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes
55
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classes
REPORT 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.
56
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
public section.
data : num type i value 5. ENDCLASS.
57
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
58
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
CREATE PUBLIC addition is implicit in every class definition if the other CREATE
additions are not used. It defines the default state, that is, that every user can create instances of the class.
Addition of CREATE PROTECTED means the class can only be instantiated by itself or its subclass.
Addition of CREATE PRIVATE means the class can only instantiate itself or the friends of the class can instantiate it.
59
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
Each object has a unique identity and its own attributes. Object References To access an object from an ABAP program, you use object references. Object references are pointers to objects. In ABAP, they are always contained in reference variables. A reference variable that points to an object knows the identity 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: Class references and interface references
60
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
... TYPE REF TO <class> Creating Objects CREATE OBJECT <cref>. Addressing the Components of Objects: You can access the instance components of an object using references in reference variables only. To access an attribute <attr>: <ref>-><attr> To call a method : CALL METHOD <ref>-><meth> You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created.
61
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
Addressing a static attribute <attr>: <class>=><attr> Calling a static method <meth>: CALL METHOD <class>=><meth> Within a class, you can use the self-reference ME to access the individual components: To access an attribute <attr> in the same class: ME><attr> To call a method <meth> in the same class: CALL METHOD ME-><meth> Self references allow an object to give other objects a reference to it. You can also access attributes in methods from within an object even if they are obscured by local attributes of the method.
62
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
Assigning References When you assign a reference to a different reference variable, their types must be either compatible or convertible. <cref1> = <cref2> <cref1> and <cref2> refer to same class. <cref1> type ref to root class OBJECT. Inheritance & Interface situations. Class OBJECT is just a container. You cannot access components of class with OBJECT reference
63
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
An object is in use by a program for as long as at least one reference points to it, or at least one method of the object is registered as an event handler. Automatic garbage collection Object Names in debugger
64
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Handling
65
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
2012/11/26
2012/11/26
Implementing Methods
All methods declared in the definition part of a class should be implemented in the implementation section of the class within the following block:METHOD <meth>. ... 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 method using the DATA statement.
69
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Static Method
oLike any other class components , methods can be static or instance.
oTo declare static methods, use the following statement: CLASS-METHODS <meth>...
oStatic methods can have import and ( export/ changing parameters ) or returning parameters . It can also raise exceptions.
oStatic methods can only work with the static attributes of your class. Instance methods can work with both static and instance attributes. oOne can call a static method using an object of the class or directly using the class by class component selector.
70
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
71
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
72
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
74
2012/11/26
2012/11/26
2012/11/26
Instance Constructor
Executed once for each instance. Called automatically, immediately after the CREATE OBJECT statement. Can contain an interface with IMPORTING parameters and EXCEPTIONS , but cannot have any EXPORTING/CHANGING/RETURNING parameters .
The interfaces are defined using the same syntax as for normal methods in the METHODS statement. To transfer parameters and handle exceptions, use the EXPORTING and EXCEPTIONS additions to the CREATE OBJECT statement .
77
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Static Constructor
REPORT YSUBOOPS2. CLASS c1 DEFINITION . PUBLIC SECTION.
CLASS-DATA : NUM TYPE I VALUE 5. CLASS-METHODS:CLASS_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 trigger exceptions. Executed once in each program. It is called automatically for the class before it is accessed for the first time - that is, before one of the following actions:
CREATE OBJECT obj from the class. Call a static method : [CALL METHOD] class=>meth. Registering a static event handler method using SET HANDLER class=>meth for obj. Registering an event handler method for a static event of the class class. Addressing a static attribute with class=>a.
78
2012/11/26
Self-Reference
Internally, each method has an implicit selfreference variable, the reserved word me A method can access the components of its class simply by their name; however, It may use me simply for clarity If a method declares a local variable with the same name as one of the class components, to avoid ambiguity it must use me to address the variable originally belonging to the class. A method must use me to export a reference to itself (that is, its object)
79
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
DAY 3
Inheritance
Inheritance allows you to derive a new class from an existing class. CLASS <subclass> DEFINITION INHERITING FROM <superclass>. The new class <subclass> inherits all of the components of the existing class <superclass>. However, only the public and protected components of the super class are visible in the subclass. You can declare private components in a subclass that have the same names as private components of the super class. Each class works with its own private components.
81
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Creating Subclass
Subclasses can be created from its superclass using the syntax:CLASS <subclass> DEFINITION INHERITING FROM <superclass>. Subclass inherits all the public and protected components of the superclass. Superclass should not be declared as a FINAL class.
82
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Inheritance
You can add new components to the subclass. This allows you to turn the subclass into a specialized 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 ABAP Objects is the predefined empty class OBJECT.
83
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Inheritance
Redefining Methods: you can use the REDEFINITION addition in the METHODS statement to redefine an inherited public or protected instance method in a subclass 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 pseudo reference SUPER-> to access the obscured method.
84
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
The interface and visibility of a method cannot be changed while redefining it.
The method declaration and implementation in the superclass is not affected when you redefine the method in a subclass.
85
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Inheritance
Abstract and Final Methods and Classes : An abstract method is defined in an abstract class and cannot 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 super class can also contain references to any of its subclasses. A reference variable defined with reference to a super class or an interface implemented by a super class can contain references to instances of any of its subclasses. Reference variable defined with reference to OBJECT can contain reference to any reference variable. CREATE OBJECT statement with type addition
86
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Abstract methods cannot be implemented in the same class. Only the subclasses of that class can implement it.
METHODS <method_name> .ABSTRACT
Any class containing an abstract method has to be an abstract class. All subsequent subclasses 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.
87
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Final classes cannot have subclasses. Only the class can be instantiated.
CLASS <classname> DEFINITION FINAL.
88
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
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 belongs to 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.
89
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Static attributes only exist once in each inheritance tree. One can change them from outside the class using the class component selector with any class name, or within any class in which they are shared. They are visible in all classes in the inheritance tree.
Output :
7
Confidential and proprietary
90
2012/11/26
Inheritance
Inheritance and Constructors Every class has an Instance constructor called CONSTRUCTOR. Instance constructors of the various classes in an inheritance tree are fully independent of one another. You cannot redefine the instance constructor of a super class in a subclass The instance constructor of a subclass has to ensure that the instance constructors of all of its super classes are also called. To do this, the instance constructor of each subclass must contain a CALL METHOD SUPER->CONSTRUCTOR statement.
91
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
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, the constructor 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
92
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
1 2 3 4
None of the superclass and subclass have explicit constructor. Superclass have explicit constructor, but subclass does not have any explicit constructor. Superclass does not have an explicit constructor, but subclass have one. Both the superclass and subclass have explicit constructor
2012/11/26
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.
94
Confidential and proprietary
2012/11/26
Inheritance
Inheritance Overview:
95
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Inheritance
Inheritance and Reference Variables
96
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Interfaces
Interfaces are independent structures that you can implement in a 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 in different classes. Global & Local Interfaces The definition of a local interface <intf> is enclosed in the statements: INTERFACE <intf>. ... 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.
97
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Defining Interfaces
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.
Locally declared in the global portion of a program using:INTERFACE <intf>. ... 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.
98
Confidential and proprietary
2012/11/26
Interfaces
Interfaces do not have instances. To implement an interface in a class, use the statement INTERFACES <intf>. in the declaration part of the class. A component <icomp> of an interface <intf> can be addressed as though it were a member of the class under the name <intf~icomp>. Interface References Addressing Objects Using Interface References Using the class reference variable <cref>: To access an attribute <attr>: <cref>-><intf~attr> To call a method <meth>: CALL METHOD <cref>><intf~meth>
99
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Interfaces
Using the interface reference variable <iref>: To access an attribute <attr>: < iref>-><attr> To call a method <meth>: CALL METHOD <iref>-><meth> Addressing a constant <const>: < intf>=><const> (Cannot use class name). Addressing a static attribute <attr>: < class>=><intf~attr> Calling a static method <meth>: CALL METHOD <class>=><intf~meth> (Cannot use Interface method ). casting operator (?= ) <cref> ?= <iref> For the casting to be successful, the object to which <iref> points must be an object of the same class as the type of the class variable <cref>.
100
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
DAY 4
102
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
104
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
105
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
What is an Exception?
An exception is a situation that occurs during the execution of an ABAP program, which renders a normal program continuation pointless. 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 execution terminates.
106
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Classification of Exceptions
Exceptions of various kinds can be broadly classified as :-
107
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
In function module
In Methods
2012/11/26
2012/11/26
TRYCATCHENDTRY
Class-based exceptions are handled using TRYCATCHENDTRY block. TRY.
< code to be checked for exception>
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.
ENDTRY.
110
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
2012/11/26
KERNEL_ERRID
TEXTID
2012/11/26
CLEANUP.
.
Cleanup block
ENDTRY.
CATCH cx_class INTO oref. CATCH cx_class INTO oref.
Catch block Catch block
CLEANUP.
. .
Cleanup block
114
Confidential and proprietary
2012/11/26
CLEANUP
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.
Used within a TRYENDTRY BLOCK , after all CATCH statements. Each TRY block can contain maximum of one CLEANUP area. Used to release the external resources when 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 TRY blocks.
115
Confidential and proprietary
2012/11/26
INHERITING FROM
116
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Step 3 :- Define another class which will call the exception class.
CLASS SOME_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: m1 raising cx_some_exception . ENDCLASS.
117
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
118
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
119
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
120
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
121
2012/11/26
122
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Note Superclass and class type SE24 Enter class name Click Create
123
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Go to Methods Tab
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_text previous 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
124
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
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 line number reached where the exception was raised A constructor method is automatically generated
125
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
126
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
First add an attribute to the error class and activate the class
2005 Intelligroup, Inc.
Then return to the methods tab and click on the constructor again
127
Confidential and proprietary
2012/11/26
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
128
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Go to the Texts tab and add a text for the exception ID.
129
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Exception Class
(8)
The texts are stored in the Online Text Repository (OTR). The exception object 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, in this case ZCX_SOME_EXCEPTION. You might wish to create an alternate text for the exception. That text can be entered on this screen with a new exception ID and can be displayed by passing this value to the parameter textid of the exception constructor.
130
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
After performing a syntax check and adding the texts to the OTR, return to the Attributes tab
131
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Dont forget to activate the object! Note that the text IDs have been added to the attributes page as class constants
132
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
DAY 8
133
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
134
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
135
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
136
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Object Oriented ABAP Prohibits obsolete statements and additions. Requires implicit syntax completions to be explicit. Detecting and preventing incorrect data handling.
Statements. Supports Overlapping and and some specialized objects. Shows Implicit Behavior. Appears difficult to learn.
2005 Intelligroup, Inc.
137
Confidential and proprietary
2012/11/26
138
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
139
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
140
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
141
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Database access
142
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Explicit Typing
143
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Data Handling
144
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Unicode Restrictions
145
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Unicode Restrictions
146
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
147
2005 Intelligroup, Inc.
Confidential and proprietary
2012/11/26
Thank You