6711457-pre-final-ppt.pdf

61
7/27/2019 6711457-Pre-Final-Ppt.pdf http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 1/61 OOPS in ABAP OOPS in ABAP 1 Object-Oriented Object-Oriented Programming in ABAP Programming in ABAP  Presented by Presented by Sylendra Prasad M Sylendra Prasad M

Upload: mpsinghrathour

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 1/61

OOPS in ABAPOOPS in ABAP 11

Object-OrientedObject-Oriented

Programming in ABAPProgramming in ABAP

  Presented byPresented by

Sylendra Prasad MSylendra Prasad M

Page 2: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 2/61

OOPS in ABAPOOPS in ABAP 22

SESSION OVERVIEWSESSION OVERVIEW

What is Object Orientation?What is Object Orientation?

Attributes of Object Oriented ProgrammingAttributes of Object Oriented Programming

ABAP ObjectsABAP Objects

How to implement classes,events...How to implement classes,events...

Advantages of OOP in ABAPAdvantages of OOP in ABAP

SAP Business ObjectsSAP Business Objects

Object Orientation tools in ABAPObject Orientation tools in ABAP

Page 3: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 3/61

OOPS in ABAPOOPS in ABAP 33

What is Object Orientation?What is O

bject Orientation?

Type of problem-solving method in which the softwareType of problem-solving method in which the software

solution reflects real-world objects.solution reflects real-world objects. Emphasis is given to data rather than to proceduresEmphasis is given to data rather than to procedures

Data is hidden and cannot be accessed by externalData is hidden and cannot be accessed by external

functions.functions.

Merits Of Object OrientationMerits Of 

Object Orientation

Complex software systems become easier toComplex software systems become easier to

understand.understand.

OO systems are easier to scale by using the concept of OO systems are easier to scale by using the concept of reusability.reusability.

Page 4: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 4/61

OOPS in ABAPOOPS in ABAP 44

Classes

Data Encapsulation

Inheritance

Polymorphism

 Attributes of Object Oriented Attributes of O

bject Oriented

ProgrammingPr 

ogramming

Objects

Page 5: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 5/61

OOPS in ABAPOOPS in ABAP 55

OBJECTSOBJECTS

Is any real-time entity (eg. Employee, customer etc )Is any real-time entity (eg. Employee, customer etc )

Contains data and behaviour.Contains data and behaviour.

Operations are done through message passing.Operations are done through message passing.

Format of message isFormat of message is

 message:[destination,operation,parameters] message:[destination,operation,parameters] 

destination receiver object stimulated by messagedestination receiver object stimulated by message

operation method that is to receive the messageoperation method that is to receive the message

parameters information needed for operation to beparameters information needed for operation to be

successful.successful.

Page 6: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 6/61

OOPS in ABAPOOPS in ABAP 66

CLASSCLASS

Central element of object orientation.Central element of object orientation.

 Abstract description of an object. Abstract description of an object.

Defines state and behaviour of objects.Defines state and behaviour of objects.

Structure of ClassStructure of Class Classes contain components.Classes contain components.

Each component is assigned a visibility section.Each component is assigned a visibility section.

Components implement methods.Components implement methods.

Page 7: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 7/61

OOPS in ABAPOOPS in ABAP 77

Data EncapsulationData Encapsulation

Protective covering preventing data and code fromProtective covering preventing data and code from

being defined outside the covering.being defined outside the covering.

Each obj. has an interface, which determines howEach obj. has an interface, which determines how

other obj. can interact with it.other obj. can interact with it.

Objs restrict visibility of their resources to other users.Objs restrict visibility of their resources to other users. Three visibility mechanisms.Three visibility mechanisms.

Private sectionPrivate section

Public sectionPublic section

Protected sectionProtected section

 All components defined in public section are accessible All components defined in public section are accessible

to all users of the class, methods of the class and anyto all users of the class, methods of the class and any

inherited classes.inherited classes.

Page 8: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 8/61

OOPS in ABAPOOPS in ABAP 88

Data Encapsulation contd..Data Encapsulation contd..

 All components declared in private section are only All components declared in private section are only

visible in the methods of same class.visible in the methods of same class. All components declared in protected section are visible All components declared in protected section are visible

to methods of the class and those classes that inheritto methods of the class and those classes that inherit

from this class.from this class.

Interfaces completely describes how the user of theInterfaces completely describes how the user of theclass interacts with it.class interacts with it.

Attributes will be hidden and user will use methods of  Attributes will be hidden and user will use methods of 

class to manipulate the data.class to manipulate the data.

Page 9: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 9/61

OOPS in ABAPOOPS in ABAP 99

INHERITANCEINHERITANCE

Relationship in which a class (subclass) inheritsRelationship in which a class (subclass) inherits

the main features of another class (superclass).the main features of another class (superclass).

Subclass can add new components (attributes, methods,Subclass can add new components (attributes, methods,

events) and replace inherited methods with its ownevents) and replace inherited methods with its own

implementation.implementation.

Types of InheritanceTypes of Inheritance

1)1) Single level InheritanceSingle level Inheritance

2)2) MultipleMultiple

3)3) HierarchicalHierarchical

4)4) HybridHybrid

Page 10: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 10/61

OOPS in ABAPOOPS in ABAP 1010

POLYMORPHISMPOLYMORPHISM

Comes from the Greek word “many forms”.Comes from the Greek word “many forms”.

Allows one interface to be used for a general class of  Allows one interface to be used for a general class of 

actions.actions.

When objects from different classes react differently toWhen objects from different classes react differently to

the same procedural call.the same procedural call.

User can work with different classes in a similar way,User can work with different classes in a similar way,

regardless of their implementation.regardless of their implementation.

Allows improved code organization and readability as Allows improved code organization and readability as

well as creation of “extensible” programs.well as creation of “extensible” programs.

Page 11: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 11/61

OOPS in ABAPOOPS in ABAP 1111

 ABAP Objects ABAP Objects

Complete set of Object-Oriented statements introducedComplete set of Object-Oriented statements introduced

in conventional ABAP.in conventional ABAP. ABAP Objects was introduced with SAP Basis Release ABAP Objects was introduced with SAP Basis Release

4.54.5

 Classes, Interfaces, EventsClasses, Interfaces, Events

ABAP Objects was completed with SAP Basis Release ABAP Objects was completed with SAP Basis Release

4.64.6

 Inheritance, Dynamic InvokeInheritance, Dynamic Invoke

Some enhancements were added with SAP Web Application

Server, Releases 6.10, 6.20.

Friends, Shared Objects

Page 12: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 12/61

OOPS in ABAPOOPS in ABAP 1212

 ABAP Objects contd.. ABAP Objects contd..

Runtime environmentRuntime environment

 ABAP Workbench allows you to create R/3 Repository ABAP Workbench allows you to create R/3 Repository

Objects like programs, lock objects and so on.Objects like programs, lock objects and so on.

Using Function Modules, we can encapsulate functionsUsing Function Modules, we can encapsulate functions

in different programs with defined interfaces.in different programs with defined interfaces.

Object Oriented enhancement of ABAP is based onObject Oriented enhancement of ABAP is based on

models of Java and C++.models of Java and C++.

Object ReferencesObject References Used to access objects from ABAP program andUsed to access objects from ABAP program and

contained incontained in reference variablesreference variables ( pointers to objects ).( pointers to objects ).

Page 13: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 13/61

OOPS in ABAPOOPS in ABAP 1313

 ABAP Objects contd… ABAP Objects contd…

Two types of referencesTwo types of references -- Class ReferencesClass References

Interface References.Interface References. Class References are defined using the additionClass References are defined using the addition

  <cref> TYPE REF TO <class><cref> TYPE REF TO <class>

in the TYPES or DATA statement.in the TYPES or DATA statement.

It allows the user to create an instance of theIt allows the user to create an instance of theclass.class.

Interface References are defined using theInterface References are defined using the

additionaddition

  …… TYPE REF TO <intf>TYPE REF TO <intf>

in the TYPES or DATA statement.in the TYPES or DATA statement. <intf> should be declared before actual reference declaration<intf> should be declared before actual reference declaration

occurs.occurs.

Page 14: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 14/61

OOPS in ABAPOOPS in ABAP 1414

 ABAP Objects contd… ABAP Objects contd…

 After creating a reference variable for a class, you can After creating a reference variable for a class, you can

create an object using the statementcreate an object using the statement

CREATECREATE OBJECTOBJECT <cref>.<cref>.

This creates an instance of the object and <cref>This creates an instance of the object and <cref>

contains the reference to the object.contains the reference to the object.

 Addressing the components of objects Addressing the components of objects

Instance componentsInstance components

To access attributes <attr>:To access attributes <attr>:  <cref> -> <attr>  <cref> -> <attr> 

To access methods <meth>:To access methods <meth>: CALLCALL  METHOD METHOD <cref>-><cref>->

<meth><meth>

Page 15: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 15/61

OOPS in ABAPOOPS in ABAP 1515

Static componentsStatic components

To access attributes <attr>: <class> => <attr>.To access attributes <attr>: <class> => <attr>.

To access methods <meth>:To access methods <meth>: CALLCALL METHODMETHOD <class><class>

=> <meth>=> <meth>

Within a class, you can access individual componentsWithin a class, you can access individual components

using the keyword ME.using the keyword ME.

For example: ME -> <attr>For example: ME -> <attr>

  CALLCALL METHODMETHOD MEME -> <meth> .-> <meth> .

Page 16: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 16/61

OOPS in ABAPOOPS in ABAP 1616

Classes in ABAPClasses in ABAP

Types of ClassesTypes of Classes

Local ClassesLocal Classes Defined within ABAP program.Defined within ABAP program.

Can be used only with that program.Can be used only with that program.

Global ClassesGlobal Classes Defined in class builder SE24.Defined in class builder SE24.

Stored centrally in class library in R/3 repository.Stored centrally in class library in R/3 repository.

Can be accessed from all programs in R/3 system.Can be accessed from all programs in R/3 system.

For eg.For eg. CL_GUI_ALV_GRID,

CL_GUI_CUSTOM_CONTAINER

Page 17: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 17/61

OOPS in ABAPOOPS in ABAP 1717

Defining local classes in ABAPDefining local classes in ABAP

The main components are Attributes, Methods andThe main components are Attributes, Methods and

Events.Events. In ABAP, classes are defined between CLASS andIn ABAP, classes are defined between CLASS and

ENDCLASS statements.ENDCLASS statements. Class definition consists of declaration andClass definition consists of declaration and

implementation parts.implementation parts. Syntax for class definition isSyntax for class definition is

CLASSCLASS classnameclassname DEFINITIONDEFINITION..

PUBLICPUBLIC SECTIONSECTION..

*declare public variables and methods.*declare public variables and methods.PRIVATEPRIVATE SECTIONSECTION..

*declare private data.*declare private data.

  ENDCLASSENDCLASS..

Page 18: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 18/61

OOPS in ABAPOOPS in ABAP 1818

Understanding Classes contd..Understanding Classes contd..

The syntax for class implementation isThe syntax for class implementation is

CLASS class_name IMPLEMENTATION.CLASS class_name IMPLEMENTATION.

METHOD CONSTRUCTOR.METHOD CONSTRUCTOR.

*initialising the variables*initialising the variablesENDMETHOD.ENDMETHOD.

METHOD method_name.METHOD method_name.

*write code for the defined methods*write code for the defined methodsENDMETHOD.ENDMETHOD.

ENDCLASS.ENDCLASS.

Page 19: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 19/61

OOPS in ABAPOOPS in ABAP 1919

Understanding Class ComponentsUnderstanding Class Components

 Attributes Attributes

They are internal data variables in a class and can takeThey are internal data variables in a class and can takeany ABAP data type.any ABAP data type.

Can be classified intoCan be classified into instanceinstance attributesattributes andand staticstatic attributesattributes..

InstanceInstance attributesattributes are declared using DATA keywordare declared using DATA keywordand determine the state of the instance.and determine the state of the instance. Must create an object before working with instanceMust create an object before working with instance

attributes.attributes. StaticStatic attributesattributes are declared using CLASS-DATAare declared using CLASS-DATA

keyword and determine the state of the class.keyword and determine the state of the class. Need not create an object before working with staticNeed not create an object before working with static

attributes.attributes.

Page 20: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 20/61

OOPS in ABAPOOPS in ABAP 2020

MethodsMethods( Procedures)( Procedures)

They can access all class attributes and have parameter They can access all class attributes and have parameter 

interface similar to the Function Modules (IMPORTING,interface similar to the Function Modules (IMPORTING,

EXPORTING, CHANGING).EXPORTING, CHANGING).

Like Attributes, there are instance methods and staticLike Attributes, there are instance methods and staticmethods.methods.

Instance methods are declared using METHODSInstance methods are declared using METHODS

keyword and can access all the attributes of the class.keyword and can access all the attributes of the class.

Static methods are declared using CLASS-METHODSStatic methods are declared using CLASS-METHODSkeyword and can access only static attributes of thekeyword and can access only static attributes of the

class.class.

Page 21: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 21/61

OOPS in ABAPOOPS in ABAP 2121

The syntax of using methods isThe syntax of using methods is

  METHODS <met>METHODS <met>IMPORTING : [VALUE(] <iIMPORTING : [VALUE(] <i

ii> [)]TYPE type]> [)]TYPE type]

[OPTIONAL] EXPORTING : [VALUE(] <e[OPTIONAL] EXPORTING : [VALUE(] <eii> [)] TYPE> [)] TYPE

type]type]

[OPTIONAL] CHANGING : [VALUE(] <c[OPTIONAL] CHANGING : [VALUE(] <cii> [)] TYPE> [)] TYPE

type]type]

[OPTIONAL] RETURNING VALUE(<r[OPTIONAL] RETURNING VALUE(<r11>)>)

EXCEPTIONS: <eEXCEPTIONS: <eii>>..

The additions like IMPORTING, EXPORTING etc defineThe additions like IMPORTING, EXPORTING etc defineattributes of interface parameters like pass-by-valueattributes of interface parameters like pass-by-value(VALUE), its type (TYPE) and if it is optional (like(VALUE), its type (TYPE) and if it is optional (likeOPTIONAL).OPTIONAL).

Page 22: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 22/61

OOPS in ABAPOOPS in ABAP 2222

Implementing methodsImplementing methods

The syntax for implementation of a method isThe syntax for implementation of a method isMETHODMETHOD methodname.methodname.

*enter the code here*enter the code here

ENDMETHODENDMETHOD..

The interface parameters needn’t be specified in implementation.The interface parameters needn’t be specified in implementation. To handle error situations, statements like RAISE <exception> ,To handle error situations, statements like RAISE <exception> ,

MESSAGE RAISING etc can be used.MESSAGE RAISING etc can be used.

Calling MethodsCalling Methods

The way of addressing a method depends on the method itself andThe way of addressing a method depends on the method itself andfrom where you are calling it.from where you are calling it.

The basic form of calling a method isThe basic form of calling a method is

CALL METHOD methodname.CALL METHOD methodname.

Page 23: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 23/61

OOPS in ABAPOOPS in ABAP 2323

Class Definition -> An ExampleClass Definition -> An Example

CLASS CL_EMPLOYEE DEFINITION.CLASS CL_EMPLOYEE DEFINITION.

PUBLIC SECTION.PUBLIC SECTION.TYPES:TYPES:

BEGIN OF T_EMPLOYEE,BEGIN OF T_EMPLOYEE,

NO TYPE I,NO TYPE I,

NAME TYPE STRING,NAME TYPE STRING,END OF T_EMPLOYEE.END OF T_EMPLOYEE.

 METHODS: METHODS:

CONSTRUCTOR CONSTRUCTOR 

IMPORTING:IMPORTING: 

IM_EMPLOYEE_NO TYPE IIM_EMPLOYEE_NO TYPE I

IM_EMPLOYEE_NAME TYPE STRING,IM_EMPLOYEE_NAME TYPE STRING,

DISPLAY_EMPLOYEE.DISPLAY_EMPLOYEE.

Page 24: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 24/61

OOPS in ABAPOOPS in ABAP 2424

 METHODS: METHODS: DISPLAY_NO_OF_EMPLOYEES.DISPLAY_NO_OF_EMPLOYEES.

PROTECTED SECTION.PROTECTED SECTION.

DATA:DATA: G_NO_OF_EMPLOYEES TYPE I.G_NO_OF_EMPLOYEES TYPE I.

PRIVATE SECTION.PRIVATE SECTION.

DATA DATA G_EMPLOYEE TYPE T_EMPLOYEE.G_EMPLOYEE TYPE T_EMPLOYEE.

ENDCLASS. CLASSENDCLASS. CLASS CL_EMPLOYEECL_EMPLOYEE IMPLEMENTATION.IMPLEMENTATION.

 METHOD METHOD CONSTRUCTORCONSTRUCTOR..

G_EMPLOYEE-NO = IM_EMPLOYEE_NO.G_EMPLOYEE-NO = IM_EMPLOYEE_NO.

G_EMPLOYEE-NAME = IM_EMPLOYEE_NAME.G_EMPLOYEE-NAME = IM_EMPLOYEE_NAME.

G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.

ENDMETHOD.ENDMETHOD.

Page 25: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 25/61

OOPS in ABAPOOPS in ABAP 2525

Class Implementation -> An ExampleClass Implementation -> An Example

CLASSCLASS CL_EMPLOYEECL_EMPLOYEE IMPLEMENTATION.IMPLEMENTATION.

METHOD METHOD CONSTRUCTORCONSTRUCTOR..G_EMPLOYEE-NO = IM_EMPLOYEE_NO.G_EMPLOYEE-NO = IM_EMPLOYEE_NO.

G_EMPLOYEE-NAME = IM_EMPLOYEE_NAME.G_EMPLOYEE-NAME = IM_EMPLOYEE_NAME.

G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.

ENDMETHOD.ENDMETHOD.

METHOD METHOD DISPLAY_EMPLOYEE.DISPLAY_EMPLOYEE.WRITE:/ ‘Employee Number’,G_EMPLOYEE_NO.WRITE:/ ‘Employee Number’,G_EMPLOYEE_NO.

WRITE:/ 'Employee Name', G_EMPLOYEE-NAME.WRITE:/ 'Employee Name', G_EMPLOYEE-NAME.

ENDMETHOD.ENDMETHOD.METHOD METHOD DISPLAY_NO_OF_EMPLOYEES.DISPLAY_NO_OF_EMPLOYEES.

WRITE:/ 'Number of employees is : ',WRITE:/ 'Number of employees is : ',

G_NO_OF_EMPLOYEES.G_NO_OF_EMPLOYEES.ENDMETHOD.ENDMETHOD.

ENDCLASSENDCLASS.. 

Page 26: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 26/61

OOPS in ABAPOOPS in ABAP 2626

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.

Page 27: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 27/61

OOPS in ABAPOOPS in ABAP 2727

Page 28: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 28/61

OOPS in ABAPOOPS in ABAP 2828

CLASS COMPONENTS contd…CLASS COMPONENTS contd…

 EventsEvents

Events are used to trigger event-handler methods in objectsEvents are used to trigger event-handler methods in objectsor classes.or classes.

When an event is triggered, any no: of handler methods canWhen an event is triggered, any no: of handler methods can

be called and the handler determines events to which it wantbe called and the handler determines events to which it want

to react.to react. Events of a class can be triggered in the methods of sameEvents of a class can be triggered in the methods of same

class using RAISE EVENT statement.class using RAISE EVENT statement.

 A method of same or different class can be declared as an A method of same or different class can be declared as an

event handler method for the event <evt> of class <class> byevent handler method for the event <evt> of class <class> by

giving the additiongiving the addition

FOR EVENT <evt> OF <class>.FOR EVENT <evt> OF <class>.

The link between handler and trigger is established at runtimeThe link between handler and trigger is established at runtime

using the statement SET HANDLER.using the statement SET HANDLER.

Page 29: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 29/61

OOPS in ABAPOOPS in ABAP 2929

Handling and Triggering EventsHandling and Triggering Events

To trigger an event, a class mustTo trigger an event, a class must

a) declare the event in declaration parta) declare the event in declaration partb) trigger the event in one of its events.b) trigger the event in one of its events.

Declaring EventsDeclaring Events To declare instance events,To declare instance events,

EVENTS <evt> EXPORTING.. VALUE(<eEVENTS <evt> EXPORTING.. VALUE(<eii>) TYPE type>) TYPE type[OPTIONAL].[OPTIONAL]. To declare static events,To declare static events,

CLASS-EVENTS <evt>..CLASS-EVENTS <evt>..

Triggering EventsTriggering Events Instance events can be triggered by any method in the classInstance events can be triggered by any method in the class

while static events can be done using only static methods.while static events can be done using only static methods.

RAISE EVENT <evt> EXPORTING <eRAISE EVENT <evt> EXPORTING <eii> = <f > = <f 

ii>..>..

Page 30: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 30/61

OOPS in ABAPOOPS in ABAP 3030

Handling EventsHandling Events

To handle an event, a method mustTo handle an event, a method musta) be defined as an event handler method for that event.a) be defined as an event handler method for that event.

b) be registered at runtime for the event.b) be registered at runtime for the event.

To declare an event handler method, use following statement.To declare an event handler method, use following statement.

METHODS <meth> FOR EVENT <evt> OF <cif> IMPORTINGMETHODS <meth> FOR EVENT <evt> OF <cif> IMPORTING

<e<eii> = <f > = <f 

ii> (for instance method).> (for instance method).

To register event handler method, use the following statement.To register event handler method, use the following statement.

SET HANDLER.. <h>.. FOR..SET HANDLER.. <h>.. FOR..

 After the RAISE EVENT statement, all registered event handler  After the RAISE EVENT statement, all registered event handler 

methods are executed before the next statement is processed.methods are executed before the next statement is processed.

Handler methods are executed in the order in which are registered./Handler methods are executed in the order in which are registered./

Page 31: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 31/61

OOPS in ABAPOOPS in ABAP 3131

Event Handling -> An exampleEvent Handling -> An example

REPORT ZGAS_NEW .REPORT ZGAS_NEW .

CLASS counter DEFINITION.CLASS counter DEFINITION.PUBLIC SECTION.PUBLIC SECTION.

METHODS increment_counter.METHODS increment_counter.

  EVENTS critical_value EXPORTINGEVENTS critical_value EXPORTINGvalue(excess) TYPE i.value(excess) TYPE i.

PRIVATE SECTION.PRIVATE SECTION.

DATA: count TYPE i,DATA: count TYPE i,

  threshold TYPE i VALUE 10.threshold TYPE i VALUE 10.ENDCLASS.ENDCLASS.

CLASS handler DEFINITION.CLASS handler DEFINITION.

PUBLIC SECTION.PUBLIC SECTION.   METHODS handle_excess FOR EVENT critical_value METHODS handle_excess FOR EVENT critical_value

OF counter IMPORTING excess.OF counter IMPORTING excess.ENDCLASS.ENDCLASS.

Page 32: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 32/61

OOPS in ABAPOOPS in ABAP 3232

CLASS counter IMPLEMENTATION.CLASS counter IMPLEMENTATION.

METHOD increment_counter.METHOD increment_counter.

DATA diff TYPE i.DATA diff TYPE i.

ADD 1 TO count.ADD 1 TO count.

IF count > threshold.IF count > threshold.diff = count - threshold.diff = count - threshold.

RAISE EVENT critical_valueRAISE EVENT critical_value

EXPORTING excess = diff.EXPORTING excess = diff.

ENDIF.ENDIF.

ENDMETHOD.ENDMETHOD.

ENDCLASSENDCLASS..

Page 33: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 33/61

OOPS in ABAPOOPS in ABAP 3333

CLASS handler IMPLEMENTATION.CLASS handler IMPLEMENTATION.

  METHOD handle_excess. METHOD handle_excess.WRITE: / 'Excess is', excess. WRITE: / 'Excess is', excess.

ENDMETHOD.ENDMETHOD.

ENDCLASS.ENDCLASS.

DATA: r1 TYPE REF TO counter,DATA: r1 TYPE REF TO counter,h1 TYPE REF TO handler.h1 TYPE REF TO handler.

START-OF-SELECTION.START-OF-SELECTION.

CREATE OBJECT: r1, h1.CREATE OBJECT: r1, h1.

  SET HANDLER h1->handle_excess FOR ALL INSTANCES.SET HANDLER h1->handle_excess FOR ALL INSTANCES.

DO 20 TIMES.DO 20 TIMES.

CALL METHOD r1->increment_counter.CALL METHOD r1->increment_counter.

ENDDO.ENDDO.

Page 34: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 34/61

OOPS in ABAPOOPS in ABAP 3434

Page 35: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 35/61

OOPS in ABAPOOPS in ABAP 3535

ConstructorsConstructors

Special methods called automatically by the system to set theSpecial methods called automatically by the system to set the

starting state of an object or class.starting state of an object or class. Called when a class is instantiated.Called when a class is instantiated.

Types of ConstructorsTypes of Constructors

Instance constructorsInstance constructors Declared using keyword METHODS CONSTRUCTORDeclared using keyword METHODS CONSTRUCTOR Used to initialize instance attributes.Used to initialize instance attributes.

Static ConstructorsStatic Constructors Declared using CLASS-METHODS CLASS CONSTRUCTOR.Declared using CLASS-METHODS CLASS CONSTRUCTOR.

Used to initialize static attributes.Used to initialize static attributes. Constructor implementation is similar to a methodConstructor implementation is similar to a method

implementation.implementation.

Page 36: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 36/61

OOPS in ABAPOOPS in ABAP 3636

InheritanceInheritance

The statement isThe statement is

CLASS <subclass> DEFINTION INHERITING FROMCLASS <subclass> DEFINTION INHERITING FROM<superclass>.<superclass>.

 A class can have more than one subclass, but may have A class can have more than one subclass, but may have

only one superclass(single inheritance).only one superclass(single inheritance).

OBJECT

C1

C2

Page 37: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 37/61

OOPS in ABAPOOPS in ABAP 3737

Inheritance contd…Inheritance contd…

When subclasses inherit from superclass, which itself aWhen subclasses inherit from superclass, which itself a

subclass of another class, all classes form inheritancesubclass of another class, all classes form inheritancetree.tree.

Redefining MethodsRedefining Methods

Use the addition REDEFINITION in METHODSUse the addition REDEFINITION in METHODS

statement to redefine public or protected instancestatement to redefine public or protected instance

method in a subclass.method in a subclass.

The method retains the name and interface, but with aThe method retains the name and interface, but with a

new implementation.new implementation.

Page 38: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 38/61

OOPS in ABAPOOPS in ABAP 3838

InterfacesInterfaces

Exclusively describes the external point of contact, butExclusively describes the external point of contact, but

don’t contain any implementation part.don’t contain any implementation part.

Has only declaration part, in the public section of Has only declaration part, in the public section of 

classes.classes.

 A class can implement any number of interfaces and A class can implement any number of interfaces and

interface can be implemented by any number of classes.interface can be implemented by any number of classes.

Interface resolution operator(~) enables to accessInterface resolution operator(~) enables to access

interface components using an object referenceinterface components using an object reference

belonging to the class implementing the interface.belonging to the class implementing the interface.

Page 39: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 39/61

OOPS in ABAPOOPS in ABAP 3939

Defining InterfacesDefining Interfaces

Use the statementUse the statement

INTERFACE <intf>INTERFACE <intf>--------------------------------------------------

ENDINTERFACE.ENDINTERFACE. Can be defined either globally in R/3 repository or Can be defined either globally in R/3 repository or 

locally in ABAP program.locally in ABAP program. You can define the same components in an interfaceYou can define the same components in an interface

as in a class.as in a class. Components don’t have to be assigned individually toComponents don’t have to be assigned individually to

a visibility section.a visibility section. Interfaces don’t have an implementation part, since their Interfaces don’t have an implementation part, since their 

methods are implemented in the class that implementsmethods are implemented in the class that implementsit.it.

Page 40: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 40/61

OOPS in ABAPOOPS in ABAP 4040

Implementing InterfacesImplementing Interfaces

Use in the declaration part of the class (public section),Use in the declaration part of the class (public section),

INTERFACES <intf>.INTERFACES <intf>. During implementation, components are added to other During implementation, components are added to other 

components in the public section.components in the public section.

The class must implement the methods of all interfacesThe class must implement the methods of all interfaces

implemented in it .implemented in it . The implementation part of the class must contain aThe implementation part of the class must contain a

method implementation for each interface methodmethod implementation for each interface method

<imeth>:<imeth>:

METHOD <intf~imeth>METHOD <intf~imeth>------------------------------------------------------------

ENDMETHOD.ENDMETHOD.

Page 41: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 41/61

OOPS in ABAPOOPS in ABAP 4141

 Advantages of OOPS in ABAP 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.

Page 42: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 42/61

OOPS in ABAPOOPS in ABAP 4242

Object Orientation Tools in ABAPObject Orientation Tools in ABAP

 ABAP Class Builder  ABAP Class Builder 

• Transaction Code:Transaction Code: SE24.SE24.•  Allows you to create and maintain global classes Allows you to create and maintain global classes

and interfaces.and interfaces.

FeaturesFeatures

• Display an overview of global data types and their Display an overview of global data types and their 

relationships.relationships.

• Create and specify attributes, methods and eventsCreate and specify attributes, methods and events

of global classes and interfaces.of global classes and interfaces.• Create internal types in a class.Create internal types in a class.

• Implement methods.Implement methods.

Page 43: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 43/61

OOPS in ABAPOOPS in ABAP 4343

Page 44: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 44/61

OOPS in ABAPOOPS in ABAP 4444

HOW TO USE ALV USING OOPSHOW TO USE ALV USING OOPS

Page 45: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 45/61

OOPS in ABAPOOPS in ABAP 4545

Tcode: SE38

Create a program

Page 46: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 46/61

OOPS in ABAPOOPS in ABAP 4646

Goto Flow Logic and click on layout

Page 47: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 47/61

OOPS in ABAPOOPS in ABAP 4747

Add a Custom Control on the screen

Page 48: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 48/61

OOPS in ABAPOOPS in ABAP 4848

Give a name to custom control

Page 49: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 49/61

OOPS in ABAPOOPS in ABAP 4949

Declare Gobal variables to be used for ALV Grid

Page 50: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 50/61

OOPS in ABAPOOPS in ABAP 5050

Fill internal table with list data to be displayed

Page 51: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 51/61

OOPS in ABAPOOPS in ABAP 5151

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

write the code

Page 52: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 52/61

OOPS in ABAPOOPS in ABAP 5252

If ALV Grid instance not exist.

Creating custom container instance

Page 53: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 53/61

OOPS in ABAPOOPS in ABAP

5353

Page 54: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 54/61

OOPS in ABAPOOPS in ABAP

5454

Creating ALV Grid instance

Page 55: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 55/61

OOPS in ABAPOOPS in ABAP

5555

Call the method for data display

Page 56: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 56/61

OOPS in ABAPOOPS in ABAP 5656

Page 57: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 57/61

OOPS in ABAPOOPS in ABAP 5757

If ALV Grid instance already exists

Output

Page 58: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 58/61

OOPS in ABAPOOPS in ABAP 5858

Output

Page 59: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 59/61

OOPS in ABAPOOPS in ABAP 5959

If we assign values for layout structure fields

Output

Page 60: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 60/61

OOPS in ABAPOOPS in ABAP 6060

Output

Page 61: 6711457-Pre-Final-Ppt.pdf

7/27/2019 6711457-Pre-Final-Ppt.pdf

http://slidepdf.com/reader/full/6711457-pre-final-pptpdf 61/61

THANK YOU