Top Banner
OOPS in ABAP OOPS in ABAP 1 Object-Oriented Object-Oriented Programming in ABAP Programming in ABAP Presented by Presented by Sylendra Sylendra Prasad M Prasad M
61
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OO ABAP

OOPS in ABAPOOPS in ABAP 11

Object-Oriented Object-Oriented Programming in ABAPProgramming in ABAP

Presented byPresented by

Sylendra Prasad MSylendra Prasad M

Page 2: OO ABAP

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: OO ABAP

OOPS in ABAPOOPS in ABAP 33

What is Object Orientation?What is Object Orientation?

Type of problem-solving method in which the software Type 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 external Data is hidden and cannot be accessed by external

functions.functions.

Merits Of Object OrientationMerits Of Object Orientation Complex software systems become easier to Complex 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: OO ABAP

OOPS in ABAPOOPS in ABAP 44

Classes

Data Encapsulation

Inheritance

Polymorphism

Attributes of Object Oriented Attributes of Object Oriented ProgrammingProgramming

Objects

Page 5: OO ABAP

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 is Format 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 be parameters information needed for operation to be

successful.successful.

Page 6: OO ABAP

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 behavior of objects.Defines state and behavior 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: OO ABAP

OOPS in ABAPOOPS in ABAP 77

Data EncapsulationData Encapsulation

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

being defined outside the covering.being defined outside the covering. Each obj. has an interface, which determines how Each 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 any to all users of the class, methods of the class and any inherited classes.inherited classes.

Page 8: OO ABAP

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 inherit to methods of the class and those classes that inherit from this class.from this class.

Interfaces completely describes how the user of the Interfaces completely describes how the user of the class 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: OO ABAP

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 own events) and replace inherited methods with its own implementation.implementation.

Types of InheritanceTypes of Inheritance1)1) Single level InheritanceSingle level Inheritance

2)2) MultipleMultiple

3)3) HierarchicalHierarchical

4)4) HybridHybrid

Page 10: OO ABAP

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 to When 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: OO ABAP

OOPS in ABAPOOPS in ABAP 1111

ABAP ObjectsABAP Objects

Complete set of Object-Oriented statements introduced Complete 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: OO ABAP

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 on Object 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 and Used to access objects from ABAP program and

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

Page 13: OO ABAP

OOPS in ABAPOOPS in ABAP 1313

ABAP Objects contd…ABAP Objects contd…

Two types of references Two 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 the class.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: OO ABAP

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 statement create 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 objectsAddressing 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 METHODMETHOD <cref>-> <cref>-> <meth><meth>

Page 15: OO ABAP

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 components Within 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: OO ABAP

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: OO ABAP

OOPS in ABAPOOPS in ABAP 1717

Defining local classes in ABAPDefining local classes in ABAP

The main components are Attributes, Methods and The main components are Attributes, Methods and Events.Events.

In ABAP, classes are defined between CLASS and In ABAP, classes are defined between CLASS and ENDCLASS statements.ENDCLASS statements.

Class definition consists of declaration and implementation Class definition consists of declaration and implementation parts.parts.

Syntax for class definition is Syntax for class definition is CLASSCLASS classname classname DEFINITIONDEFINITION..

PUBLICPUBLIC SECTIONSECTION..*declare public variables and methods.*declare public variables and methods.PRIVATEPRIVATE SECTIONSECTION..*declare private data.*declare private data.

ENDCLASSENDCLASS..

Page 18: OO ABAP

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 variables

ENDMETHOD.ENDMETHOD.

METHOD method_name.METHOD method_name.

*write code for the defined methods*write code for the defined methods

ENDMETHOD.ENDMETHOD.

ENDCLASS.ENDCLASS.

Page 19: OO ABAP

OOPS in ABAPOOPS in ABAP 1919

Understanding Class ComponentsUnderstanding Class Components

AttributesAttributes They are internal data variables in a class and can take They are internal data variables in a class and can take

any ABAP data type.any ABAP data type. Can be classified into Can be classified into instanceinstance attributesattributes and and staticstatic

attributesattributes.. InstanceInstance attributesattributes are declared using DATA keyword are declared using DATA keyword

and determine the state of the instance.and determine the state of the instance. Must create an object before working with instance Must create an object before working with instance

attributes.attributes. StaticStatic attributesattributes are declared using CLASS-DATA are 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 static Need not create an object before working with static

attributes.attributes.

Page 20: OO ABAP

OOPS in ABAPOOPS in ABAP 2020

MethodsMethods( Procedures)( Procedures) They can access all class attributes and have parameterThey 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 static Like Attributes, there are instance methods and static

methods.methods. Instance methods are declared using METHODS Instance 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-METHODS Static methods are declared using CLASS-METHODS

keyword and can access only static attributes of the keyword and can access only static attributes of the class.class.

Page 21: OO ABAP

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(] <iii> [)]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 define The additions like IMPORTING, EXPORTING etc define attributes of interface parameters like pass-by-value attributes 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 (like OPTIONAL).OPTIONAL).

Page 22: OO ABAP

OOPS in ABAPOOPS in ABAP 2222

Implementing methodsImplementing methods The syntax for implementation of a method is The syntax for implementation of a method is

METHODMETHOD 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 and The way of addressing a method depends on the method itself and

from where you are calling it.from where you are calling it. The basic form of calling a method is The basic form of calling a method is

CALL METHOD methodname.CALL METHOD methodname.

Page 23: OO ABAP

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:CONSTRUCTORCONSTRUCTORIMPORTING:IMPORTING:

IM_EMPLOYEE_NO TYPE IIM_EMPLOYEE_NO TYPE IIM_EMPLOYEE_NAME TYPE STRING,IM_EMPLOYEE_NAME TYPE STRING,

DISPLAY_EMPLOYEE.DISPLAY_EMPLOYEE.

Page 24: OO ABAP

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.

DATADATA G_EMPLOYEE TYPE T_EMPLOYEE. G_EMPLOYEE TYPE T_EMPLOYEE.

ENDCLASS. CLASSENDCLASS. CLASS CL_EMPLOYEE CL_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 + G_NO_OF_EMPLOYEES = G_NO_OF_EMPLOYEES + 1.1.

ENDMETHOD.ENDMETHOD.

Page 25: OO ABAP

OOPS in ABAPOOPS in ABAP 2525

Class Implementation -> An ExampleClass Implementation -> An Example

CLASSCLASS CL_EMPLOYEE CL_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. METHODMETHOD 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. METHODMETHOD 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: OO ABAP

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: OO ABAP

OOPS in ABAPOOPS in ABAP 2727

Page 28: OO ABAP

OOPS in ABAPOOPS in ABAP 2828

CLASS COMPONENTS contd…CLASS COMPONENTS contd…

EventsEvents Events are used to trigger event-handler methods in objects Events are used to trigger event-handler methods in objects

or classes.or classes. When an event is triggered, any no: of handler methods can When an event is triggered, any no: of handler methods can

be called and the handler determines events to which it want be 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 same Events 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> by event 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 runtime The link between handler and trigger is established at runtime

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

Page 29: OO ABAP

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 musta) 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(<e ii>) 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 class Instance 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 <e ii> = <f> = <fii>..>..

Page 30: OO ABAP

OOPS in ABAPOOPS in ABAP 3030

Handling EventsHandling Events To handle an event, a method mustTo handle an event, a method must

a) 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> IMPORTING METHODS <meth> FOR EVENT <evt> OF <cif> IMPORTING

<e<eii> = <f> = <fii> (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: OO ABAP

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 EXPORTING value(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: OO ABAP

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: OO ABAP

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: OO ABAP

OOPS in ABAPOOPS in ABAP 3434

Page 35: OO ABAP

OOPS in ABAPOOPS in ABAP 3535

ConstructorsConstructors

Special methods called automatically by the system to set the Special 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 method Constructor implementation is similar to a method

implementation.implementation.

Page 36: OO ABAP

OOPS in ABAPOOPS in ABAP 3636

InheritanceInheritance

The statement is The statement is

CLASS <subclass> DEFINTION INHERITING FROM CLASS <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: OO ABAP

OOPS in ABAPOOPS in ABAP 3737

Inheritance contd…Inheritance contd…

When subclasses inherit from superclass, which itself a When subclasses inherit from superclass, which itself a subclass of another class, all classes form inheritance subclass of another class, all classes form inheritance tree.tree.

Redefining MethodsRedefining Methods

Use the addition REDEFINITION in METHODS Use the addition REDEFINITION in METHODS statement to redefine public or protected instance statement to redefine public or protected instance method in a subclass.method in a subclass.

The method retains the name and interface, but with a The method retains the name and interface, but with a new implementation.new implementation.

Page 38: OO ABAP

OOPS in ABAPOOPS in ABAP 3838

InterfacesInterfaces

Exclusively describes the external point of contact, but Exclusively 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 access Interface resolution operator(~) enables to access interface components using an object reference interface components using an object reference belonging to the class implementing the interface.belonging to the class implementing the interface.

Page 39: OO ABAP

OOPS in ABAPOOPS in ABAP 3939

Defining InterfacesDefining Interfaces

Use the statementUse the statementINTERFACE <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 implements it.methods are implemented in the class that implements it.

Page 40: OO ABAP

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 interfaces The class must implement the methods of all interfaces implemented in it .implemented in it .

The implementation part of the class must contain a The implementation part of the class must contain a method implementation for each interface method method implementation for each interface method <imeth>: <imeth>:

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

------------------------------------------------------------

ENDMETHOD.ENDMETHOD.

Page 41: OO ABAP

OOPS in ABAPOOPS in ABAP 4141

Advantages of OOPS in ABAPAdvantages 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: OO ABAP

OOPS in ABAPOOPS in ABAP 4242

Object Orientation Tools in ABAPObject Orientation Tools in ABAP

ABAP Class BuilderABAP 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 theirDisplay 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: OO ABAP

OOPS in ABAPOOPS in ABAP 4343

Page 44: OO ABAP

OOPS in ABAPOOPS in ABAP 4444

HOW TO USE ALV USING OOPSHOW TO USE ALV USING OOPS

Page 45: OO ABAP

OOPS in ABAPOOPS in ABAP 4545

Tcode: SE38

Create a program

Page 46: OO ABAP

OOPS in ABAPOOPS in ABAP 4646

Goto Flow Logic and click on layout

Page 47: OO ABAP

OOPS in ABAPOOPS in ABAP 4747

Add a Custom Control on the screen

Page 48: OO ABAP

OOPS in ABAPOOPS in ABAP 4848

Give a name to custom control

Page 49: OO ABAP

OOPS in ABAPOOPS in ABAP 4949

Declare Gobal variables to be used for ALV Grid

Page 50: OO ABAP

OOPS in ABAPOOPS in ABAP 5050

Fill internal table with list data to be displayed

Page 51: OO ABAP

OOPS in ABAPOOPS in ABAP 5151

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

Page 52: OO ABAP

OOPS in ABAPOOPS in ABAP 5252

If ALV Grid instance not exist.

Creating custom container instance

Page 53: OO ABAP

OOPS in ABAPOOPS in ABAP 5353

Page 54: OO ABAP

OOPS in ABAPOOPS in ABAP 5454

Creating ALV Grid instance

Page 55: OO ABAP

OOPS in ABAPOOPS in ABAP 5555

Call the method for data display

Page 56: OO ABAP

OOPS in ABAPOOPS in ABAP 5656

Page 57: OO ABAP

OOPS in ABAPOOPS in ABAP 5757

If ALV Grid instance already exists

Page 58: OO ABAP

OOPS in ABAPOOPS in ABAP 5858

Output

Page 59: OO ABAP

OOPS in ABAPOOPS in ABAP 5959

If we assign values for layout structure fields

Page 60: OO ABAP

OOPS in ABAPOOPS in ABAP 6060

Output

Page 61: OO ABAP

OOPS in ABAPOOPS in ABAP 6161

THANK YOU