Professional Documents
Culture Documents
OOPS in ABAP
SESSION OVERVIEW
What is Object Orientation? Attributes of Object Oriented Programming ABAP Objects How to implement classes,events... Advantages of OOP in ABAP SAP Business Objects Object Orientation tools in ABAP
OOPS in ABAP 2
Type of problem-solving method in which the software solution reflects real-world objects. Emphasis is given to data rather than to procedures Data is hidden and cannot be accessed by external functions.
Merits Of Object Orientation Complex software systems become easier to understand. OO systems are easier to scale by using the concept of reusability.
OOPS in ABAP
OOPS in ABAP
OBJECTS
Is any real-time entity (eg. Employee, customer etc ) Contains data and behaviour. Operations are done through message passing. Format of message is message:[destination,operation,parameters] destination operation parameters receiver object stimulated by message method that is to receive the message information needed for operation to be successful.
OOPS in ABAP
CLASS
Central element of object orientation. Abstract description of an object. Defines state and behaviour of objects.
Structure of Class Classes contain components. Each component is assigned a visibility section. Components implement methods.
OOPS in ABAP
Data Encapsulation
Protective covering preventing data and code from being defined outside the covering. Each obj. has an interface, which determines how other obj. can interact with it. Objs restrict visibility of their resources to other users. Three visibility mechanisms.
All components defined in public section are accessible to all users of the class, methods of the class and any inherited classes.
OOPS in ABAP 7
All components declared in private section are only visible in the methods of same class. All components declared in protected section are visible to methods of the class and those classes that inherit from this class. Interfaces completely describes how the user of the class interacts with it. Attributes will be hidden and user will use methods of class to manipulate the data.
OOPS in ABAP
INHERITANCE
Relationship in which a class (subclass) inherits the main features of another class (superclass). Subclass can add new components (attributes, methods, events) and replace inherited methods with its own implementation.
Types of Inheritance
1) 2) 3) 4)
OOPS in ABAP
POLYMORPHISM
Comes from the Greek word many forms. Allows one interface to be used for a general class of actions. When objects from different classes react differently to the same procedural call. User can work with different classes in a similar way, regardless of their implementation. Allows improved code organization and readability as well as creation of extensible programs.
OOPS in ABAP
10
ABAP Objects
Complete set of Object-Oriented statements introduced in conventional ABAP. ABAP Objects was introduced with SAP Basis Release 4.5
Some enhancements were added with SAP Web Application Server, Releases 6.10, 6.20. Friends, Shared Objects
OOPS in ABAP
11
Class References Interface References. Class References are defined using the addition <cref> TYPE REF TO <class> in the TYPES or DATA statement. It allows the user to create an instance of the class. Interface References are defined using the addition TYPE REF TO <intf> in the TYPES or DATA statement. <intf> should be declared before actual reference declaration occurs.
OOPS in ABAP 13
After creating a reference variable for a class, you can create an object using the statement CREATE OBJECT <cref>. This creates an instance of the object and <cref> contains the reference to the object.
To access attributes <attr>: <cref> -> <attr> To access methods <meth>: CALL METHOD <cref>-> <meth>
OOPS in ABAP 14
Static components To access attributes <attr>: <class> => <attr>. To access methods <meth>: CALL METHOD <class> => <meth>
Within a class, you can access individual components using the keyword ME. For example: ME -> <attr> CALL METHOD ME -> <meth> .
OOPS in ABAP
15
Classes in ABAP
Types of Classes Local Classes Defined within ABAP program. Can be used only with that program. Global Classes Defined in class builder SE24. Stored centrally in class library in R/3 repository. Can be accessed from all programs in R/3 system. For eg. CL_GUI_ALV_GRID, CL_GUI_CUSTOM_CONTAINER
OOPS in ABAP
16
The main components are Attributes, Methods and Events. In ABAP, classes are defined between CLASS and ENDCLASS statements. Class definition consists of declaration and implementation parts. Syntax for class definition is CLASS classname DEFINITION. PUBLIC SECTION. *declare public variables and methods. PRIVATE SECTION. *declare private data. ENDCLASS.
OOPS in ABAP 17
Methods( Procedures) They can access all class attributes and have parameter interface similar to the Function Modules (IMPORTING, EXPORTING, CHANGING). Like Attributes, there are instance methods and static methods. Instance methods are declared using METHODS keyword and can access all the attributes of the class. Static methods are declared using CLASS-METHODS keyword and can access only static attributes of the class.
OOPS in ABAP
20
The additions like IMPORTING, EXPORTING etc define attributes of interface parameters like pass-by-value (VALUE), its type (TYPE) and if it is optional (like OPTIONAL).
OOPS in ABAP 21
Implementing methods The syntax for implementation of a method is METHOD methodname. *enter the code here ENDMETHOD. The interface parameters neednt be specified in implementation. To handle error situations, statements like RAISE <exception> , MESSAGE RAISING etc can be used. Calling Methods The way of addressing a method depends on the method itself and from where you are calling it. The basic form of calling a method is CALL METHOD methodname.
OOPS in ABAP 22
METHODS: DISPLAY_NO_OF_EMPLOYEES. PROTECTED SECTION. DATA: G_NO_OF_EMPLOYEES TYPE I. PRIVATE SECTION. DATA G_EMPLOYEE TYPE T_EMPLOYEE. ENDCLASS. CLASS CL_EMPLOYEE IMPLEMENTATION. METHOD CONSTRUCTOR. G_EMPLOYEE-NO = IM_EMPLOYEE_NO. G_EMPLOYEE-NAME = IM_EMPLOYEE_NAME. G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1. ENDMETHOD.
OOPS in ABAP 24
OOPS in ABAP
25
DATA : G_EMPLOYEE1 TYPE REF TO LCL_EMPLOYEE. START-OF-SELECTION. CREATE OBJECT G_EMPLOYEE1 EXPORTING IM_EMPLOYEE_NO = 1 IM_EMPLOYEE_NAME = 'John Jones'. CALL METHOD G_EMPLOYEE1->DISPLAY_EMPLOYEE. CALL METHOD G_EMPLOYEE1->DISPLAY_ NO_OF_EMPLOYEES.
OOPS in ABAP
26
OOPS in ABAP
27
Triggering Events Instance events can be triggered by any method in the class while static events can be done using only static methods. RAISE EVENT <evt> EXPORTING <ei> = <fi>..
OOPS in ABAP 29
Handling Events To handle an event, a method must a) be defined as an event handler method for that event. b) be registered at runtime for the event.
To declare an event handler method, use following statement. METHODS <meth> FOR EVENT <evt> OF <cif> IMPORTING <ei> = <fi> (for instance method). To register event handler method, use the following statement. SET HANDLER.. <h>.. FOR.. After the RAISE EVENT statement, all registered event handler methods are executed before the next statement is processed. Handler methods are executed in the order in which are registered./
OOPS in ABAP
30
CLASS counter IMPLEMENTATION. METHOD increment_counter. DATA diff TYPE i. ADD 1 TO count. IF count > threshold. diff = count - threshold. RAISE EVENT critical_value EXPORTING excess = diff. ENDIF. ENDMETHOD. ENDCLASS.
OOPS in ABAP 32
CLASS handler IMPLEMENTATION. METHOD handle_excess. WRITE: / 'Excess is', excess. ENDMETHOD. ENDCLASS. DATA: r1 TYPE REF TO counter, h1 TYPE REF TO handler. START-OF-SELECTION. CREATE OBJECT: r1, h1. SET HANDLER h1->handle_excess FOR ALL INSTANCES. DO 20 TIMES. CALL METHOD r1->increment_counter. ENDDO.
OOPS in ABAP 33
OOPS in ABAP
34
Constructors
Special methods called automatically by the system to set the starting state of an object or class. Called when a class is instantiated. Types of Constructors
Instance constructors Declared using keyword METHODS CONSTRUCTOR Used to initialize instance attributes. Static Constructors Declared using CLASS-METHODS CLASS CONSTRUCTOR. Used to initialize static attributes. Constructor implementation is similar to a method implementation.
OOPS in ABAP
35
Inheritance
The statement is CLASS <subclass> DEFINTION INHERITING FROM <superclass>. A class can have more than one subclass, but may have only one superclass(single inheritance).
OBJECT C1 C2
OOPS in ABAP
36
Inheritance contd
When subclasses inherit from superclass, which itself a subclass of another class, all classes form inheritance tree.
Redefining Methods
Use the addition REDEFINITION in METHODS statement to redefine public or protected instance method in a subclass.
The method retains the name and interface, but with a new implementation.
OOPS in ABAP 37
Interfaces
Exclusively describes the external point of contact, but dont contain any implementation part. Has only declaration part, in the public section of classes.
A class can implement any number of interfaces and interface can be implemented by any number of classes. Interface resolution operator(~) enables to access interface components using an object reference belonging to the class implementing the interface.
OOPS in ABAP 38
Defining Interfaces
Use the statement INTERFACE <intf> ------------------------ENDINTERFACE. Can be defined either globally in R/3 repository or locally in ABAP program. You can define the same components in an interface as in a class. Components dont have to be assigned individually to a visibility section. Interfaces dont have an implementation part, since their methods are implemented in the class that implements it.
OOPS in ABAP 39
Implementing Interfaces
Use in the declaration part of the class (public section), INTERFACES <intf>. During implementation, components are added to other components in the public section. The class must implement the methods of all interfaces implemented in it . The implementation part of the class must contain a method implementation for each interface method <imeth>: METHOD <intf~imeth> -----------------------------ENDMETHOD.
OOPS in ABAP 40
The implementation of object-oriented elements in ABAP language has considerably increased response times. Use of OOPS in ABAP helps to have a better control of development complexity, a better means for encapsulation and extensibility. Reusability of the objects will reduce the coding effort and helps in utilizing the existing code for other programs.
OOPS in ABAP
41
OOPS in ABAP
43
OOPS in ABAP
44
Tcode: SE38
Create a program
OOPS in ABAP
45
OOPS in ABAP
46
OOPS in ABAP
47
OOPS in ABAP
48
OOPS in ABAP
49
OOPS in ABAP
50
In PBO of the flow logic, write a module and inside the module write the code
OOPS in ABAP
51
OOPS in ABAP
52
OOPS in ABAP
53
OOPS in ABAP
54
OOPS in ABAP
55
OOPS in ABAP
56
OOPS in ABAP
57
Output
OOPS in ABAP
58
OOPS in ABAP
59
Output
OOPS in ABAP
60
THANK YOU
OOPS in ABAP
61