OOABAP
Short Description
Overall Presentation on SAP OO ABAP...
Description
Object- Object -Oriented Oriented Programming in ABAP Presented by Pooja Nayak
OOPS O OPS iin n ABAP ABAP
1
SESSION OVERVIEW
What is Object Orientation?
Attributes Attri butes of Object Oriented Programming
ABAP Objects
How to implement classes,events...
Advantages of OOP in ABA ABAP P
SAP Business Objects
Object Orientation tools in ABA ABAP P
OOPS O OPS iin n ABAP ABAP
2
What is Object Orientatio Orientation? n?
Type of problemproblem-solving -solving method in which the software s oftware solution reflects realreal-world -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 O OPS iin n ABAP ABAP
3
Attributes of Object Oriented Programming Objects Classes Data Encapsulation Inheritance Polymorphism
OOPS O OPS iin n ABAP ABAP
4
OBJECTS
Is any realreal-time -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
receiver object stimulated by message
operation
method that is to receive the message
parameters
information informatio n needed for operation to be successful.
OOPS O OPS iin n ABAP ABAP
5
CLASS
Central element of object orientation. Abstract description of an object. Defines state and behaviour of objects.
Structure of Class
Classes contain component components. s.
Each component is assigned a visibility section.
Components implement methods.
OOPS O OPS iin n ABAP ABAP
6
Data Encapsulatio Encapsulation n
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.
Private section
Public section
Protected section
All components components defined in public section are accessible to all users of the class, c lass, methods of the class and any inherited classes. OOPS O OPS iin n ABAP ABAP
7
Data Encapsulation contd..
All components components declared in private section are only visible in the methods of same class. All components declared in protected section are visible to methods methods of the class and those classes c lasses 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 manipulate the data.
OOPS O OPS iin n ABAP ABAP
8
INHERITANCE
Relationship Relationshi p in which a class (subclass) inherits the main features of another class (superclass).
Subclass can add new components (attributes, methods, events) and replace replace inherited inherited methods methods with its own implementation. Types of Inheritance 1)
Single level Inheritance
2)
Multiple
3)
Hierarchical
4)
Hybrid
OOPS O OPS iin n ABAP ABAP
9
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 impleme implementation. ntation. Allows improved code organization and readability as well as creation of ³extensible´ programs.
OOPS O OPS iin n ABAP ABAP
10 10
ABAP Objects
Complete set of ObjectObject-Oriented -Oriented statements introduced in conventional ABAP.
ABAP Objects was introduced with SAP Basis Release 4.5
Classes, Interfaces, Events
ABAP Objects was completed with SAP Basis Release 4.6
Inheritance, Dynamic Invoke
Some enhancements were added with SAP Web W eb Application Server, Releases 6.10, 6.20.
Friends, Shared Objects
OOPS O OPS iin n ABAP ABAP
11 11
ABAP Objects contd.. Runtime environment
ABAP Workbench allows you to create R/3 Repository Objects like programs, lock objects and so on. Using Function Modules, we can encapsulate functions in different programs with defined interfaces.
Object Oriented enhancement of ABAP is based on models of Java and C++.
Object References
Used to access objects from ABAP program and contained in reference variables ( pointers to objects ).
OOPS O OPS iin n ABAP ABAP
12 12
ABAP Objects contd«
Two types of references -
C Class lass References References Interface References. Class References are defined using the addition TYPE REF TO
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
in the TYPES or DATA statement. should be declared before actual reference declaration occurs. OOPS O OPS iin n ABAP ABAP
13 13
ABAP Objects contd«
After creating a reference variable for a class, you can create an object using the statement CREAT CRE CREATE ATE E OB OBJE OBJECT JECT CT . This creates an instance of the object and contains the reference to the object.
Addressing the components of objects Instance componen c omponents ts
To access attributes :
CALL METHOD METHO MET HOD D To access methods : CALL -> ->
ref> -> ->
OOPS O OPS iin n ABAP ABAP
14 14
Static components
To access attributes :
<
class> => .
To access methods : =>
CA ALL LL
METH ME METHOD THOD OD
Within a class, you can access individual componen components ts using the keyword ME. For example: ME --> > CA ALL LL
METH THOD OD M ME E -> -> .
OOPS O OPS iin n ABAP ABAP
15 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. CL_GUI_ALV_G RID, For eg. CL_GUI_CUSTOM_CONTAINER
OOPS O OPS iin n ABAP ABAP
16 16
Defining local classes in ABAP
The main components are Attributes Attributes,, Methods and Events. In ABAP, classes are defined between CLASS and ENDCLASS statements. Class definition definition consists of declaration and implementation parts. Syntax for class definition is CLASS classname DEFINITION . PUBLIC SEC PUBLIC SECTIO SECTION TION N. *declare public variables and methods.
PRIVAT PRI PRIVATE VATE E SEC SECTIO SECTION TION N. *declare private data. ENDCLASS. OOPS O OPS iin n ABAP ABAP
17 17
Understanding Understandin g Classes contd..
The syntax for class implementation is CLASS class_name IMPLEMENTAT IMPLEMENTATION. ION. METHOD CONSTRUCTOR. *initialising *initialisin g the variables ENDMETHOD. METHOD method_name. *write code for the defined methods ENDMETHOD. ENDCLASS. OOPS O OPS iin n ABAP ABAP
18 18
Understanding Class Components Attributes They are internal data variables in a class and can take any ABAP data type. Can be classified into instance insta in stance nce att attrib attr at trib ribute ibut utes es s an and d static attributes.. attributes Instan Ins Instance tance ce att attrib attribut attr ribute ibutes utes es s are declare dec declared lared d using DATA keyword keyword keyw ord and determine the state of the instance. Must create an object before working with instance attributes. Static at Static attri attribut attr tribut ibutes butes es are declared declared decla red using using CLASS CLAS CLASSS-DATA -DATA keyword and determine the state of the class. Need not create an object before working with static attributes. OOPS O OPS iin n ABAP ABAP
19 19
Methods( Procedures) Methods(
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 methods are declared using METHODS keyword keywor d and can access all the attributes of the class. Static methods methods are declared using CLASSCLASS -METHODS -METHODS keyword and can access only static attributes of the class.
OOPS O OPS iin n ABAP ABAP
20 20
The syntax of using methods is METHODS IMPORTING : [VALUE(] [)]TYPE type] [OPTIONAL] EXPORTING : [VALUE(] [)] TYPE type] [OPTIONAL] CHANGING
: [VALUE(] [)] TYPE type]
[OPTIONAL] RETURNING VALUE() EXCEPTIONS: .
The additions like IMPORTING, EXPORTING etc define attributes of interface parameters like passpass -by -by by--value -value (VALUE), its type (TYPE) and if it is optional (like OPTIONAL). OOPS O OPS iin n ABAP ABAP
21 21
Implementing methods
The syntax for implementation implem entation of a method is METHOD methodname. *enter the code here ENDMETHOD.. ENDMETHOD
The interface parameters needn¶t be specified in implementation. implem entation. To handle error situations, statements like RAISE , 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 O OPS iin n ABAP ABAP
22 22
Class Definition --> > An Example CLASS CL_EMPLOYEE DEFINITION. PUBLIC SECTION. TYPES: BEGIN OF T_EMPLOYEE, NO TYPE I, NAME TYPE STRING, END OF T_EMPLOYEE.
METHODS: CONSTRUCTOR IMPORTING: IM_EMPLOYEE_NO TYPE I IM_EMPLOYEE_NAME TYPE STRING,
DISPLAY_EMPLOYEE. OOPS O OPS iin n ABAP ABAP
23 23
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. OYEE-NO -NO = IM_EMPLOYEE_NO. G_EMPLOYEEOYEE-N -NAME = IM_EMPLOYEE_NAME. G_EMPLOYEEG_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.
ENDMETHOD. OOPS O OPS iin n ABAP ABAP
24 24
Class Implementatio Im Implementation plementation n --> > An Example CLASS CL_EMPLOYEE IMPLEMENTATION. METHOD CONSTRUCTOR. OYEE-NO -NO = IM_EMPLOYEE_NO. G_EMPLOYEEG_EMPLOYEEOYEE-N -NAME = IM_EMPLOYEE_NAME. G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1. ENDMETHOD. METHOD DISPLAY_EMPLOYEE. WRITE:/ µEmployee Number¶,G_EMPLOYEE_NO. WRITE:/ 'Employee Name', G_EMPLOYEEOYEE-N -NAME. ENDMETHOD. METHOD DISPLAY_NO_OF_EMPLOYEES. WRITE:/ 'Number of employees is : ', G_NO_OF_EMPLOYEES. ENDMETHOD. ENDCLASS.
OOPS O OPS iin n ABAP ABAP
25 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 O OPS iin n ABAP ABAP
26 26
OOPS O OPS iin n ABAP ABAP
27 27
CLASS COMPONENTS contd« Events
Events are used to trigger event event--handler -handler methods in objects or classes. When an event is triggered, any no: of handler methods can be called and the handler determines events to which it want to react. Events of a class can be triggered in the methods m ethods of same class using RAISE EVENT statement. A method of same or different class can be declared as an event handler method for f or the event of class by giving the addition FOR EVENT OF .
The link between between handler and trigger is established at runtime using the statement SET HANDLER. OOPS O OPS iin n ABAP ABAP
28 28
Handling and Triggering Events To trigger an event, a class must a) declare the event in declaration part b) trigger the event in one of its events. Declaring Events To declare instance events, EVENTS EXPORTING.. VALUE() TYPE type [OPTIONAL]. To declare static events, CLASS--EVENTS CLASS -EVENTS ..
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 EXPORTING = .. OOPS O OPS iin n ABAP ABAP
29 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 FOR EVENT OF IMPORTING ei> = (for instance method).
<
To register event handler method, use the following statement. SET HANDLER.. .. 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 O OPS iin n ABAP ABAP
30 30
Event Handling --> > An example REPORT ZGAS_NEW . CLASS counter DEFINITION. PUBLIC SECTION. METHODS increment _counter. EVENTS critical _value EXPORTING value(excess) TYPE i. PRIVATE SECTION. TYPE i , DATA: count threshold TYPE i VALUE 10. ENDCLASS. CLASS handler DEFINITION. PUBLIC SECTION.
excess FOR EVENT METHODS handle _ ex OF counter IMPORTING excess. ENDCLASS. OOPS O OPS iin n ABAP ABAP
critical _value
31 31
CLASS counter IMPLEMENTATION.
METHOD increment _counter. DATA diff TYPE i. ADD 1 TO count. IF count > threshold.
diff = count - t threshold. hr esho ld.
RAISE EVENT critical _value EXPORTING excess = diff. ENDIF. ENDMETHOD. ENDCLASS. OOPS O OPS iin n ABAP ABAP
32 32
CLASS handler IMPLEMENTATION.
excess. METHOD handle _ ex WRITE: / 'Excess is', excess. ENDMETHOD. ENDCLASS. DATA: r1 TYPE REF TO counter ,
h1 TYPE REF TO handler. RT-OF -OF OF-SELECTION. STARTCREATE OBJECT: r1, h1.
SET HANDLER h1h1->handle ->handle _ ex excess FOR ALL INSTANCES. DO 20 TIMES.
r1->increment ->increment _counter. CALL METHOD r1ENDDO.
OOPS O OPS iin n ABAP ABAP
33 33
OOPS O OPS iin n ABAP ABAP
34 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 CLASSCLASS-METHODS -METHODS CLASS CONSTRUCTOR. Used to initialize static attributes. Constructor implementation is similar to a method implementation.
OOPS O OPS iin n ABAP ABAP
35 35
Inheritance
The statement is CLASS DEFINTION INHERITING FROM .
A class can have more than one subclass, but may have only one superclass(single inheritance). OBJECT C1
C2
OOPS O OPS iin n ABAP ABAP
36 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 redefine public or protected instance method in a subclass. The method retains the name and interface, but with a new implementation. OOPS O OPS iin n ABAP ABAP
37 37
Interfaces
Exclusively describes the external point of contact, but don¶t 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 resolution operator(~ operator(~)) enables to access interface components components using an object reference belonging to the class implementing the interface. OOPS O OPS iin n ABAP ABAP
38 38
Defining Interfaces
Use the statement INTERFACE ------------------------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 Componen ts don¶t have to be assigned individually to a visibility section. Interfaces don¶t have an implementation part, since their methods are implemented in the class that implements it. OOPS O OPS iin n ABAP ABAP
39 39
Implementing Interfaces
Use in the declaration part of the class (public section), INTERFACES . During implementation, components are added to other components componen ts in the public section. The class must implement the methods of all interfaces implemented in it . The implementation implementation part of the class must contain a method implementation for each interface method : METHOD -----------------------------ENDMETHOD. OOPS O OPS iin n ABAP ABAP
40 40
Advantages Advanta ges of OOPS in ABAP
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 O OPS iin n ABAP ABAP
41 41
Object Orientation Orientation Tools T ools in ABAP ABAP Class Builder
Transaction T ransaction Code: Code:
S SE24. E24.
Allows you to create and maintain global classes and interfaces.
Features
Display an overview of global data types and their relationships.
Create and specify attributes, methods and events of global classes and interfaces.
Create internal types in a class.
Implement methods. OOPS O OPS iin n ABAP ABAP
42 42
OOPS O OPS iin n ABAP ABAP
43 43
HOW TO USE ALV USING OOPS
OOPS O OPS iin n ABAP ABAP
44 44
Tcode: SE38 Create
a program
OOPS O OPS iin n ABAP ABAP
45 45
Goto
Flow Logic and click on layout
OOPS O OPS iin n ABAP ABAP
46 46
Add a Custom Control on the screen
OOPS O OPS iin n ABAP ABAP
47 47
Give
a name to custom control
OOPS O OPS iin n ABAP ABAP
48 48
Declare Gobal variables to be used for ALV Grid
OOPS O OPS iin n ABAP ABAP
49 49
Fill internal table with list data to be displayed
OOPS O OPS iin n ABAP ABAP
50 50
In
PBO of the flow logic, write w rite a module and inside the module write the code
OOPS O OPS iin n ABAP ABAP
51 51
If ALV Grid Creating
instance not exist.
custom container instance
OOPS O OPS iin n ABAP ABAP
52 52
OOPS O OPS iin n ABAP ABAP
53 53
Creating
ALV AL V Grid instance
OOPS O OPS iin n ABAP ABAP
54 54
Call
the method for data display
OOPS O OPS iin n ABAP ABAP
55 55
OOPS O OPS iin n ABAP ABAP
56 56
If ALV Grid
instance already exists
OOPS O OPS iin n ABAP ABAP
57 57
Output
OOPS O OPS iin n ABAP ABAP
58 58
If we assign values for layout structure fields
OOPS O OPS iin n ABAP ABAP
59 59
Output
OOPS O OPS iin n ABAP ABAP
60 60
THANK YOU
OOPS O OPS iin n ABAP ABAP
61 61
View more...
Comments