ObjectObject-Oriented Programming in ABAP

Presented by Sylendra Prasad M

OOPS in ABAP

1

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      

What is Object Orientation?  



Type of problem-solving method in which the software problemsolution reflects real-world objects. realEmphasis 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

3

Attributes of Object Oriented Programming
Objects Classes Data Encapsulation Inheritance Polymorphism

OOPS in ABAP

4

OBJECTS 
  

Is any real-time entity (eg. Employee, customer etc ) realContains 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

5

CLASS 
 

Central element of object orientation. Abstract description of an object. Defines state and behavior of objects.

Structure of Class  Classes contain components.  Each component is assigned a visibility section.  Components implement methods.

OOPS in ABAP

6

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. 
 

Private section Public section Protected section 

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

Data Encapsulation contd..    

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

8

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) 

Single level Inheritance Multiple Hierarchical Hybrid

OOPS in 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 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 Objectin 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 Application Server, Releases 6.10, 6.20.  Friends, Shared Objects

OOPS in ABAP

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 in ABAP 12

ABAP Objects contd«     

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

Two types of references -

ABAP Objects contd« 

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. 

Addressing the components of objects Instance components 


To access attributes <attr>: <cref> -> <attr> To access methods <meth>: CALL METHOD <cref>-> <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

Defining local classes in ABAP 
  

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. DEFINITION. PUBLIC SECTION. SECTION. *declare public variables and methods. PRIVATE SECTION. SECTION. *declare private data. ENDCLASS. ENDCLASS.
OOPS in ABAP 17

Understanding Classes contd.. 

The syntax for class implementation is CLASS class_name IMPLEMENTATION. METHOD CONSTRUCTOR. *initialising the variables ENDMETHOD. METHOD method_name. *write code for the defined methods ENDMETHOD. ENDCLASS.
OOPS in ABAP 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 attributes and static attributes. attributes.  Instance attributes are declared using DATA keyword and determine the state of the instance.  Must create an object before working with instance attributes.  Static attributes are declared using CLASS-DATA CLASSkeyword and determine the state of the class.  Need not create an object before working with static attributes.
OOPS in ABAP 19

Methods( 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 CLASSkeyword and can access only static attributes of the class.

OOPS in ABAP

20 

The syntax of using methods is
METHODS <met> IMPORTING : [VALUE(] <ii> [)]TYPE type] [OPTIONAL] EXPORTING : [VALUE(] <ei> [)] TYPE type] [OPTIONAL] CHANGING : [VALUE(] <ci> [)] TYPE type] [OPTIONAL] RETURNING VALUE(<r1>) EXCEPTIONS: <ei>. 

The additions like IMPORTING, EXPORTING etc define attributes of interface parameters like pass-by-value pass-by(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. ENDMETHOD.  The interface parameters needn¶t 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

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 in ABAP 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. CONSTRUCTOR. G_EMPLOYEEG_EMPLOYEE-NO = IM_EMPLOYEE_NO. G_EMPLOYEEG_EMPLOYEE-NAME = IM_EMPLOYEE_NAME. G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1. ENDMETHOD.
OOPS in ABAP 24

Class Implementation -> An Example
CLASS CL_EMPLOYEE IMPLEMENTATION. METHOD CONSTRUCTOR. CONSTRUCTOR. G_EMPLOYEEG_EMPLOYEE-NO = IM_EMPLOYEE_NO. G_EMPLOYEEG_EMPLOYEE-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_EMPLOYEE-NAME. G_EMPLOYEEENDMETHOD. METHOD DISPLAY_NO_OF_EMPLOYEES. WRITE:/ 'Number of employees is : ', G_NO_OF_EMPLOYEES. ENDMETHOD. ENDCLASS.

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

CLASS COMPONENTS contd«
Events  Events are used to trigger event-handler methods in objects eventor 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 of same class using RAISE EVENT statement.  A method of same or different class can be declared as an event handler method for the event <evt> of class <class> by giving the addition FOR EVENT <evt> OF <class>.  The link between handler and trigger is established at runtime using the statement SET HANDLER.
OOPS in ABAP 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 <evt> EXPORTING.. VALUE(<ei>) TYPE type [OPTIONAL].  To declare static events, CLASSCLASS-EVENTS <evt>.. 

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

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. DATA: count TYPE i, threshold TYPE i VALUE 10. ENDCLASS. CLASS handler DEFINITION. PUBLIC SECTION. METHODS handle_excess FOR EVENT critical_value OF counter IMPORTING excess. ENDCLASS.
OOPS in ABAP 31

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. START-OFCREATE OBJECT: r1, h1. SET HANDLER h1->handle_excess FOR ALL INSTANCES. h1DO 20 TIMES. CALL METHOD r1->increment_counter. r1ENDDO.
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. CLASS 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 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 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 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 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

Advantages 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 in ABAP

41

Object Orientation Tools in ABAP
ABAP Class Builder ‡ Transaction Code: SE24. ‡ 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 in ABAP 42

OOPS in ABAP

43

HOW TO USE ALV USING OOPS

OOPS in ABAP

44

Tcode: SE38 Create a program

OOPS in ABAP

45

Goto Flow Logic and click on layout

OOPS in ABAP

46

Add a Custom Control on the screen

OOPS in ABAP

47

Give a name to custom control

OOPS in ABAP

48

Declare Gobal variables to be used for ALV Grid

OOPS in ABAP

49

Fill internal table with list data to be displayed

OOPS in ABAP

50

In PBO of the flow logic, write a module and inside the module write the code

OOPS in ABAP

51

If ALV Grid instance not exist. Creating custom container instance

OOPS in ABAP

52

OOPS in ABAP

53

Creating ALV Grid instance

OOPS in ABAP

54

Call the method for data display

OOPS in ABAP

55

OOPS in ABAP

56

If ALV Grid instance already exists

OOPS in ABAP

57

Output

OOPS in ABAP

58

If we assign values for layout structure fields

OOPS in ABAP

59

Output

OOPS in ABAP

60

THANK YOU

OOPS in ABAP

61