You are on page 1of 188

OOPS ABAP

Class : A Class is a user defined data type with attributes


methods types and events for a particular entity or business
scenario or business application

Class creation is of 2 steps:

CLASS CL_CLASSNAME DEFINITION.

ENDCLASS.

CLASS CL_CLASSNAME IMPLEMENTATION.

ENDCLASS.
OBJECTS
Objects are instances of classes.

Each object has a unique identity i.e. memory and its own attributes.

Objects are created with the command CREATE OBJECT.

Once an object is created, we have an instance of class with


separate memory

Syntax : It is also 2 step process

STEP1 : DATA: instance TYPE REF TO CL_CLASSNAME.

STEP2 : CREATE OBJECT instance.


Components of a Class
Components of a Class :

Attributes:- Any data variables), constants declared within a class


are called as attributes of the class.

Methods:- Methods contain a block of code, providing some


functionality for a class.

These are similar to function modules in ABAP.

Methods can access all of the attributes of a class.

Methods are defined in the definition part of a class and

Implemented in the implementation part.

Methods are called using the CALL METHOD statement.


Events:-

Events are like actions performed by the user.

Each event must be linked to a method for writing the


corresponding business logic for the action/event raised by the user
.

Interfaces:-

Interfaces are similar to classes which contain methods

Without any implementation.

These are mainly used to extend the scope or functionality of the


class.

Remaining things will be discussed later.


Instance and Static Components
Instance components : The components which exist
separately for each instance (object) of the class are called as
instance components,

that means for each object, instance components will be available


separately.

These are referred using the symbol .

Static components : The components which exists globally


or fixed for all instances(objects) of the class are called as STATIC
components,

that means all objects with be referring to same/fixed memory.

These are referred to using the symbol => .


Visibility of Components
Each class component has a visibility.

In ABAP Objects, the whole class definition is separated into three


visibility sections:

PUBLIC

PROTECTED

PRIVATE.
Public section:

Data declared in public section can be accessed by the class itself,


by its subclasses as well as by other users outside the class.

Protected section:

Data declared in the protected section can be accessed by the class


itself, and also by its subclasses but not by external users outside
the class.

Private Section:

Data declared in the private section can be accessed by the class


only, but not by its subclasses and by external users outside the
class.
Global Class and Local Class
Global Class and Local Class:

Classes in ABAP Objects can be declared either globally or locally.

Global Class: Global classes and interfaces are defined in the Class
Builder (Transaction SE24) in the ABAP Workbench.

All of the ABAP programs in an R/3 System can access the global
classes

Local Class: Local classes are define in an ABAP program


(Transaction SE38) and can only be used in the program in which
they are defined.
Ex on Class
Steps to create a class:-

Go to SE24

Give the class name ZCL_sample.

Click on create

Provide short description.

Click on save button

Go to attributes tab, define an attribute as below

V_name instancepublictypechar25.

Save and activate.

A class is created by the above steps

.
Creating Objects
Creating instance or objects for the class:-

. Creation of objects is of 2 steps.

1. First declare a variable for an object or instance.

Syntax :-<objname> Type ref to <class name>.

Eg1:- OBJ1 type ref to ZCL_sample.

Eg2:-OBJ2 type ref to ZCL_sample.

2. Next, instantiate/create copy for the object.

Syntax: create object <object name>

Eg1:- create object obj1

Eg2:- Create object obj2


Accessing class components
Accessing or calling class components:-
To call any class component with the instance, use the below Syntax.

Syntax: <Objname> <class-component name>

E.g:- OBJ1 v_name

OBJ2v_name

.
Example program :-

Data: OBJ1 type ref to ZCL_sample

Create object OBJ1

*set a value using obj1

Obj1v_name = IBM INDIA.

*print a value using obj1.

Write:/ obj1v_name color 5.

.
Similarly create another object

Create object obj2

*Set a value using OBJ2

Obj2v_name = Mahindra satyam.

*print a value using obj2

Write: / obj2v_name color 6.


.
*Changing the value using obj1

Obj1v_name = IBM-INDIA-Hyderabad loc.

*Print a value using obj1.

Write: / obj1v_name color 6.

*Print a value using obj2.

Write: / obj2v_name color 5.

NOTE: - If we change the v_name value using obj1,it will


not affect obj2 since both have separate memory areas
.
Static variables
Steps:

Go to se24

Give the class name as ZCL_SAMPLE.

Click on change button.

Click on attributes tab

Declare a static variable as below.

Save and activate.


Write the program as below.

Data : obj1 type ref to zcl_sample .

Data : obj2 type ref to zcl_sample .

Create object obj1 .

Create object obj2 .

*Set a value using obj1 .

Obj1 v_name2 = `IBM INDIA HYD .

*Now, Print a value using obj1&obj2 .

Write : / obj1 v_name2 .

Write : / obj2 v_name2 .

Write : / sy_uline .
*Set a value using obj2 .

Obj2 v_name2 = `TCS HYD .

*Print a value using obj1&obj2 .

Write : / obj1 v_name2 .

Write : / obj1 v_name2 .

Second way of using static members

ZCL_SAMPLE => V_NAME2 = IBM INDIA .

Write : / ZCL_SAMPLE => V_NAME2 .


Note

All the static variables will have a global memory .

If we change the static variable using object1, it will be reflect


remaining objects also because all the objects will be referring to the
same memory.

It is always preferred to call the v using the class name.

Syntax :<class name> => < static variable name>

Eg :- zcl_sample => v_name1 = ARJUN.

Static variables must be accessed by symbol => .

Never use static variables with objects.

In the real time , we use static variables with the class name only.
Using public, private , protected variables
Steps:

Go to se24

Give the class name as ZCL_SAMPLE.

Click on create

Declare the variables/Attributes as below

Save and activate the class.


Write the code as below.

Create object obj1.

OBJ1 Pu_name = ARJUN No error Bcoz Pu_name is variable.

OBJ1 Pr_name = ARJUN Error Bcoz Pr_name is a private Variable

OBJ1 Po_name = ARJUNError Bcoz Po_name is a protected


variable.
Creating Methods
STEPS:

Go to se24.

Give the class name as ZCL_sample.

Click on the methods tab.

Give the method name as GET_CUST_DET

GET_CUST_DETINSTANCEPUBLIC

Click on the parameters button and & specify the parameters as


below.

IM_KUNNR IMPORTING TYPE KNA1-KUNNR.

EX_KNA1 EXPORTING TYPE KNA1.

.
Click on the button code. (or)

double click on the method name &write the below code.


METHOD GET_CUST_DET.

Select single * from kna1

Into corresponding ields of EX_KNA1

Where KUNNR = IM_KUNNR.

Endmethod.

save and activate the class.


Testing the Method :-

Click on execute button.

Click on create Instance button.

All the instance members will be visible now.

Click on the execute button for the method GET_CUST_DET.

The output will be displayed.


Calling the Method from the program
Code in abap program :

Data: wa_kna1 type kna1.

Data: OBJ1 type ref to zcl_sample.

Create object OBJ1.

PARAMETERS : P_KUNNR TYPE KNA1-KUNNR.

Click on PATTERN button.

Select ABAP object patterns.

select the radio button call method and provide the values as below.

Call method

Instance : OBJ1

Class : ZCL_Sample

Method : GET_CUST_DET, And press enter


The below code will be displayed automatically.

Call method OBJ1 GET_CUST_DET.

EXPORTING

IM_KUNNR = P_KUNNR.

IMPORTING

EX_KNA1 = WA_KNA1.

WRITE : / WA_KNA1-KUNNR ,

WA_KNA1-NAME1.

SaveActivateTest it.
Methods using table types
TABLES PARAMETER:

In the methods, we dont have any option for specifying internal tables
directly.

Whereas in function modules, we have an option by name TABLE where


we can specify the internal tables directly.

Tables are absolute in OO-ABAP.

Thats why this option is not available.

If we want to use tables parameters, then we need to create a table type in


data dictionary.
Steps to create a table type:

Go to se11.

Give the data type as zkna1_t.

Click on the button create.

Select table type

Give short description.

Give the line type as kna1

Save & activate.

The table type is created


Creating a method using table types:

Go to se24.

Give the class name.

Click on methods tab.

Define a method as below.

GET_CUST_LIST Instance Public

Click on the parameters button.

Define the parameters as below.

IM_LAND1 IMPORTING TYPE KNA1-LAND1.

EX_KNA1 EXPORTING TYPE ZKNA1_T.


Write the below code.

METHOD GET_CUST_LIST

SELECT * FROM KNA1

INTO TABLE EX_KNA1

WHERE LAND1 = IM_LAND1.

ENDMETHOD.

Save ,activate & test it.


Using the method in the program:

DATA: I_KNA1 TYPE ZKNA1_T.

DATA: WA_KNA1 LIKE LINE OF I_KNA1.

DATA:OBJ1 TYPE REF TO ZCL_SAMPLE.

CREATE OBJECT OBJ1.

PARAMETERS: P_LAND1 TYPE KNA1-LAND1.

CALL METHOD OBJ GET_CUST_LIST.

EXPORTING

IM_LAND1 = P_LAND1

IMPORTING

EX_KNA1 = I_KNA1.
LOOP AT I_KNA1 INTO WA_KNA1.

WRITE:/ WA_KNA1-KUNNR,

WA_KNA1- NAME1,

WA_KNA1-LAND1.

ENDLOOP.
USER-DEFINED TYPES IN THE CLASSES
USING USER-DEFINED TYPES IN THE CLASSES:

Open the Class ZCL_Sample.

Click on the TYPES Tab.

Define type TY_KNA1 as below

Click on the button and write the below code


Save and activate the class.
Define a method as below

Define the below Parameters

Write the below code


Method GET_CUST_DETAILS
Select single * from KNA1
into corresponding fields of EX_KNA1
where KUNNR = IM_KUNNR.
End Method.
Save, Activate and Test it.
USING THE METHOD IN THE PROGRAM:
DATA: WA_KNA1 TYPE ZCL_SAMPLE =>TY_KNA1 Bcoz TY_KNA1 is a
static member.
DATA: OBJ1 TYPE REF TO ZCL_SAMPLE
Create object OBJ1.
Parameters: P_KUNNR TYPE KNA1 KUNNR
CALL METHOD OBJ1 GET_CUST_DETAILS
Exporting
IM _KUNNR = P_KUNNR
Importing
EX_KNA1 = WA_KNA1.
WRITE:/ WA_KNA1 KUNNR, WA_KNA1 LAND1, WA_KNA1 NAME1.
Save, Activate & Test it .
EVENTS IN OO ABAP
Event:

Events are like actions performed by the user.

Each event must be linked to a method for writing the corresponding


business logic for the action/event raised by the user .

5 STEPS TO BE FOLLOWED WHEN WORKING WITH THE EVENTS:

Define an event .

Define a method for writing business logic for the action/event raised by the
user .

Link event and method and convert the method into event-handler method.

Raise the event in any method implementation.

Use set handler and register event handler method to a particular instance

in the program.
SET HANDLER:

It is the keyword which is used to register event handler method to a


particular instance of class where the event is raised.

For All Instances

Sometimes we want to execute event handler methods not to a particular


instance of class, but for all the instances of the class, then we use FOR
ALL INSTANCES.
TYPES OF EVENTS
There are 2 types of events:

Standard Events : Events which are defined and raised by SAP are called
Standard Events

Ex : Double_Click, Right_Click

For these events we are suppose to create event handler


methods and write the corresponding logic.

Custom Events : Events which are defined, raised and also handled by
creating event handler methods are called Custom Events .

Events contain only exporting parameters which are imported by event


handler methods.
Ex On Events
Step1: STEPS TO DEFINE AN EVENT:-

Open the class zcl-sample.

Click on events tab.

Define an event as below,

NO_KUNNRINSTANCEPUBLIC

Click on save.
Step2: STEPS TO DEFINE A METHOD:-

Click on methods tab

Give the method name as below .

EVT_HDLER_METH instancepublic.

Write the below code in the method.

Write:/please enter customer number.

Click on save.
Step3: STEPS TO LIK EVENT AND METHOD:-

Put the cursor on the Method i.e EVT_HDLER_METH .

Click on the detail view icon

A pop-up is rised.

Select the checkbox event handler &provide the class name and event
name

Event handler for

class/interface : ZCL-SAMPLE.

Event : NO-KUNNR.

Click on the change button, which will convert the method into event
handler method.

The symbol >> will indicate that it is a event handler method.


Step4: Create a triggering method which will raise the event.

SYNTAX TO RAISE AN EVENT:

SYNTAX: RAISE EVENT<event name>

Example: Raise event <xyz>.

Ex: Modify the method as below GET_CUST_DET as below:

METHOD GET_CUST_DET.
If im_kunnr is initial .

Raise event NO_KUNNR.

Else.

Select single * from kna1

Into corresponding ields of EX_KNA1

Where KUNNR = IM_KUNNR.

Endmethod.
Step5: Use set handler and register event handler method to a particular
instance where the event is raised..
SYNTAX FOR SET HANDLER:

SET HANDLER <event-handler method> FOR <instance>

Ex:- Set handler OBJ1XYZ FOR OBJ1

Ex: Go to the program and call the method without giving any customer no.

Data: wa_kna1 type kna1.

Data:obj1-type ref to zcl-sample.

Create object OBJ1.

Parameters: P_kunnr type kna1-kunnr.

Set handler OBJ1EVT-HDLER-METH for obj1.


Call method OBJ1-GET_CUST_DET.

EXPORTING

Im_kunnr = P_kunnr

IMPORTING

Ex_kna1 = wa_kna1.

Write:/wa_kna1 kunnr,wa_kna1-name1.

The output is : Event NO_KUNNR is raised.


CONSTRUCTORS
Constructors are special type of methods.

constructor methods are executed automatically whenever a object is created or

Whenever a class is loaded.

These methods are mainly used to set default values in a class. i.e. Used to
default values to attributes. (Ex V_Werks = 0001)

These are declared only in public section

These methods cannot be called explicitly


Types of CONSTRUCTORS
There are 2 Types of constructors :

INSTANCE Constructor

STATIC Constructor
INSTANCE CONSTRUCTORS:
These are special type of methods.

constructor method is executed automatically whenever a object is created or


instantiated.

These methods are mainly used to set default values in a class.

The name of the constructor method is constructor.

These methods have only importing parameters.

There are no exporting parameters.


EXAMPLE ON CONSTRUCTORS:
Open the class zcl_sample.

Click on button constructor available at the top right corner.

A method by name constructor will be added automatically with the symbol .

Click on the parameters button & define the importing parameters .

Im_kunnr---->type--kna1-kunnr.

Double click on the constructor & write the code.

Save and activate.


CALLING CONSTRUCTORS IN ABAP PROG:

We never call the constructor method bcoz they are executed automatically whenever
an object is created.
STEPS TO CREATE OBJECT:

Click on the pattern button.


Select abap object patterns.
Select create object & provide instance name & class name.
Instance : obj1
Class : zcl_sample.
Press enter the code will be generated as below.
Create object obj1
Exporting
Im_kunnr=123.
Execute the program.
REAL TIME EXAMPLE ON CONSTRUCTORS:
The class by name cl_gui_alv_grid has a constructor method by name constructor.

This method needs an importing parameter by name i_parent.

This parameter takes the reference of custom container as a default value to display alv.

Note:
Constructor method is called for each instance created i.E.., Create 5 instances ,the
constructor method is called 5 times.
STATIC CONSTRUCTORS
STATIC CONSTRUCTORS:

STATIC CONSTRUCTORS:
It is a type of constructor method.

This method is executed automatically whenever a first call to the class is made.

The first call can be through instance or class name.

Ex: obj1--get_cust_det.

Zcl_sample--v_name2.

Static constructor are used to set the default values globally .i.e.,Irrespective of
instances.

The static constructors will not have any importing / exporting parameters .

These are executed only once.

The name of the static constructor is CLASS_ CONSTRUCTOR.


STEPS TO DEFINE STATIC CONSTRUCTORS:

Open the class ZCL_SAMPLE.

Click on the button CLASS_CONSTRUCTOR available at the top right corner.

A method by name CLASS_CONSTRUCTOR will be created.

Write the below code in the method.

Method class_constructor

Write : /

endmethod.

Save it & activate and Execute the abap program.

O/P :The default customer is:123(from static constructor)

Default customer is 123.(From instance constructor).


1. Interface
Interface
Interfaces are independent structure which are used in a class to
extend the functionality of a class.
Interfaces contains methods without any implementation.
Where as a class contains methods with implementation.
We need to define an interface with the required method names in
SE24 TCODE.
Now,this interface can be used by n no of classes to extend the
functionality of the class.
Just give the name of interface under the interface tab.
All the interface methods will be automatically copied to the classes
in a particular class without effecting the other classes.
In all the classes,the methods names are same but the
implementation will be different from one class to another class
thereby achieving the concept called POLYMORPHSM.

Polymorphism is implemented in the from of interface.


1. Polymorphism
Polymorphism

It is a concept by which the same method names will behave


differently in different classes i.e each method will have its own
implementation in different different classes but with the same name.
1. creating an interface
Steps to create an interface:
Go to SE24.
Give the interface name as ZV_IF.
Click on create.
Select the interface radio button.
Click on the method tab.
Declare 2 methods as below.
Get_mara with im_mara as importing parameter
and ex_mara as exporting parameter
Get_KNA1 with im_kunnr as importing parameter
and ex_KNA1 as exporting parameter
We cannot write any ABAP code in the interface.
Save &activate the interface.
Using interface in the classes
Go to SE24.

Give the class name as ZCL_sample.

Go to the interface tab.

Give the interface name as ZV_IE and Press enter.

Click on the methods tab .

The interface methods will be copied automatically as below

ZV_IF~get _mara.

ZV_IF~ get_KNA1.

Double click on the method ZV_IF~get_mara &

write the below code.


Method ZV_IFN get_mara.

Select single * from mara.

Into corresponding fieldsof EX_mara

Where mara =IM_mara.

End method.

Save ,active &test it.

Similarly write the code for method ZV_IF~get_KNA1.


Using interface method
DATA : wa_mara type mara.

DATA : OBJ1 type ref to zc__sample.

Parameters : p_matnr type mara_matnr.

Create object OBJ1.

CALL METHOD OBJ1ZV_IF~GET_MARA.

EXPORTING

IM_LAND1 = P_MATNR

IMPORTING

EX_MARA = WA_MARA.

WRITE : / WA_MARA-MATNR, WA_MARA-MEINS.


Inheritance
Inheritance
Inheritance is the concept of passing the properties of one class to another class.
The class which passes the properties is called SUPER class or Parent class.
The class which receives the properties is called SUB class or Child class.
You can use an existing class to derive a new class.
Derived class inherits the data and methods of a super class.
Derived class can overwrite the existing methods also add new code.
Advantage of this property is reusability.
This means we can add additional features to an existing class without modifying it.
Super is the keyword used to represent the super class. You can access the methods
and attributes the super class using this word super.
REDIFINATION is the keyword which is used to overwrite the parent class methods
with new definition.
NOTE: In oo_ABAP, we have only single inheritance.
There is no multiple inheritance, But we have Multilevel inheritance
1. Polymorphism
Polymorphism

It is a concept by which the same method names will behave


differently in different classes i.e each method will have its own
implementation in different different classes but with the same name.

Polymorphism behavior is implemented in the form of

Interface Methods

Method overloading

Method Overriding.
Interface Methods : The Interface Methods can be implemented in
multiple classes with same name but with diff implementation which
is called as polymorphism.

Method overloading : Methods with same name but different


signatures i.e. imp/exp parameters is called method overloading

It is not supported in OO-Abap.

Method Overriding : A child class inherits the super class methods

And overwrites them with extra functionality is called Method


Overriding.

Redefinition is the keyword which is used to overwrite the super


class method.
Ex on Inheritance
STEP1: create a super class with a method.
Create a class by name ZCL_SUPER.
Remove the checkbox final because we want to inherit the class in a child class

Define a method by name Get_material_data with in_matnr and ex_mara as


parameters.
Write the belowcode in the method.
Method get_material_data.
Select single * from mara into corresponding fields of ex_mara
Where matnr = im_matnr
End method.
Save,active the class.
Step2: Create child class
Go to SE24.
Give the class name as ZCL_CHILD.
Click on create button.
Click on the icon below and provide the super class name as ZCL_SUPER.

Click on save.
The method Get_material_Data will be copied automatically as it is a super
class method.
Select the method name & click on the Redefine icon
So that we can redifine super class method with extra
functionality
Now ,double click on method & write the below code.

The coding will be generated automatically.

Now, Uncomment the code if you need.

Then, Write your own or extra code as below.

Select single* from makt

into corresponding fields of wa_makt

Where matnr = im_matnr

And spras=EN.

Declare wa_makt under attributes tab.

Save & activate the child class.


The method should look as below:
Using Child Class In the Program

DATA : OBJ type Ref to ZCL_CHLD.

DATA : Wa_mara type mara.

DATA : Wa_makt type makt.

PARAMETERS : P_matnr type mara_matnr.

Create object OBJ.

Call method OBJGet material_Data.

Exporting.

Im_matnr = P_matnr.

Importing

Ex_mara = wa_mara.
*Display material master data

Write: / wa_mara-matnr,

Wa_mara-mtart,

Wa_mara-meins .

*Display description Data

Write : / OBJwa_makt_matnr,

OBJwa_makt_spras,

OBJwa_makt_maktx
Abstract class
Abstract class

It is a class which contains methods with implementation as well as


methods without implementation .

(or)

Abstract class is a class which contains at least one abstract method


Abstract method :-

It is a method which doesnt contain any implementation.

Abstract class are mainly used for creating inheritance.

Note :- we cannot create an object to the abstract class because they are
not fully implemented, Instead create an object to the child class which
inherits the abstract class and then call the methods .
steps to define an Abstract class :-

Goto SE24

Give class by name ZCL_ ABSTRACT .

Click on Create

Remove the checkbox final as below.


Define the method by name Get_mara_data

with Im_mara as importing parameter

and Ex_mara as exporting parameter.

Double click on the method and write the code.


Create another method by name Get_kna1_data

with Im_kunnr as importing parameter

and Ex_kna1 as parameters .

Put the cursor on the method Get_kna1_data ,

Click on the detail view button as below

A pop is raised ,select the checkbox abstract.

Click on CHANGE button

Now the method is converted into abstract method .


Now the class has 2 methods as below :
Creating inheritance for the Abstract class
Steps:

Go to SE24

Give class by name ZCL_ ABSTRACT_CHILD .

Click on Create

Click on create Inheritance button

Give the super class name as ZCL_ ABSTRACT.

The super class methods will be automatically copied .

Select the abstract method Get_kna1_data and click on the


Redefine button and write the abap code.
Select single *from kna1
Into wa_kna1 where kunnr = im_kunnr.
Using abstract classes in programs :-
Note :- create an object to the child class as we cannot create the objects to the abstract class .

Data : wa_kna1 type kna1.


Data: OBJ type ref to ZCL_ ABSTRACT_CHILD .
Parameters : p_kunnr type kna1-kunnr.
Create object OBJ .
Call method OBJ get_kna1_data
Exporting
im_kunnr =p_kunnr.
Importing
Ex_kna1 = wa_kna1.
Write :/ wa_kna1-kunnr , wa_kna1-name1.
LOCAL CLASS
LOCAL CLASS :
Defining a class inside a program is called local class

Class creation is of 2 steps:

CLASS CL_CLASSNAME DEFINITION.

ENDCLASS.

CLASS CL_CLASSNAME IMPLEMENTATION.

ENDCLASS.
USING STATIC AND INSTANCE COMPONENTS IN LOCAL CLASS
USING PUBLIC,PROTECTED,PRIVATE VISIBILITY:
Syntax :

CLASS CL_CLASSNAME DEFINITION.

Public-section .

DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS

Protected-section .

DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS
Private-section .

DATA :
CLASS-DATA
METHODS:
CLASS-METHODS
EVENTS
CLASS-EVENTS

ENDCLASS.
USING EVENTS IN LOCAL CLASSES:
1.Syntax to Define an Event
CLASS lcl_sample DEFINITION.
PUBLIC SECTION.
EVENTS : <EVENTNAME>.
END CLASS.

Example:
2.Syntax to Define event handler method
CLASS lcl_sample DEFINITION.
METHODS : <EVENT HANDLER METH>
FOR EVENT <EVENTNAME>
OF <CLASSNAME>
END CLASS
USING INHERITANCE IN LOCAL CLASS
CLASS <superclassname> DEFINITION.
METHODS : <Methodname>
END CLASS.

CLASS <childclassname> DEFINITION INHERITING ROM <SUPERCLASS>.


METHODS : <Methodname> REDIFINATION
END CLASS.
DEFINATION DEFERRED :
It is the keyword which indicates the class definition is delayed or postponed or
Defined at some place in program.
Syntax : CLASS <Classname> DEFINITION DEFERED.
LOCAL CLASS WITH ATTRIBUTES
LOCAL CLASS WITH METHODS
LOCAL CLASS WITH EVENTS
CONSTRUCTORS
LOCAL CLASS WITH INHERITANCE
OOPS ALV PROGRAMS
Introduction:

The ALV grid data is displayed on an UI element called as custom


container.
Advantages of OO ALV :

We have n no of events available in the classes when compared to


ALV with function modules which gives flexibility for the
programmer to develop ALVS for various scenarios.

We can display more than one ALV grid data on a single screen.

The ALV grid data is displayed in the form of custom container with
which we can control the size of ALV grid Whereas we cannot
control the size of the ALV with function Modules.

We can also place different UI elements like checkbox,


Radiobutton on the same screen in addition ALV grid data.
CLASSES USED IN OO ALVS
Most commonly used classes are :

CL_GUI_ALV_Grid

CL_GUI_CUSTOM_CONTAINER

CL_dd_document

CL_GUI_ALV_tree_simple

CL_GUI_container

CL_GUI_splitter_container.

The most commonly used method to display ALV grid data is,
SET_TABLE_FOR_FIRST_DISPLAY.
1. OOPS ALV with Structure.
Steps for a simple OO-ALV:
STEP1: Create data declarations for ALV/custom container.
DATA : ALV_CONT TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
DATA : ALV_GRID TYPE REF TO CL_GUI_ALV_GRID
DATA : I_MARA TYPE TABLE OF MARA .
DATA : WA_MARA TYPE MARA .

STEP2 : Create screen 100.


START-OF-SELECTION .
CALL SCREEN 100.Double click on 100 and create a screen

STEP3:Goto screen layout and drag and drop a custom container.


Give the name as ALV_CONT.
STEP4:Click on flow-logic and create the PBO Module and write the below code .
MODULE STATUS_0100 OUTPUT.
PERFORM CREATE_OBJECTS.
PERFORM GET_DATA.
PERFORM DISPLAY_ALV .
ENDMODULE.
STEP5 : Define each subroutine as below:
FORM CREATE_OBJECTS .
CREATE OBJECT ALV_CONT
EXPORTING
CONTAINER_NAME = 'ALV_CONT'.
CREATE OBJECT ALV_GRID
EXPORTING
I_PARENT = ALV_CONT.
Endform.
FORM GET_DATA .
SELECT * FROM MARA
INTO TABLE I_MARA UP TO 100 ROWS
ENDFORM.
FORM DISPLAY_ALV .
CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_STRUCTURE_NAME = 'MARA'
CHANGING
IT_OUTTAB = I_MARA.
ENDFORM.
Save->activate->test it.
1. OOPS ALV with Fieldcatelog
Same steps as previous program with additional changes as below.
STEP1: Create data declarations for ALV/custom container.
TYPES : BEGIN OF ty_mara,
matnr TYPE mara-matnr,
mtart TYPE mara-mtart,
mbrsh TYPE mara-mbrsh,
meins TYPE mara-meins,
END OF ty_mara.
DATA : i_mara TYPE TABLE OF ty_mara .
DATA : wa_mara TYPE ty_mara .
DATA : alv_cont TYPE REF TO cl_gui_custom_container.
DATA : alv_grid TYPE REF TO cl_gui_alv_grid .
DATA : i_fcat TYPE lvc_t_fcat .
DATA : wa_fcat LIKE LINE OF i_fcat .
STEP2 : Create screen 100.

STEP3: Design screen with custom container

STEP4:Click on flow-logic and create the PBO Module and write


below code

MODULE STATUS_0100 OUTPUT.


PERFORM CREATE_OBJECTS.

PERFORM GET_DATA.

PERFORM create_fcat .

PERFORM DISPLAY_ALV .

ENDMODULE.
STEP5 : Define each subroutine as previous program .

Define field catalog subroutine as below:

FORM create_fcat .

CLEAR wa_fcat .

wa_fcat-col_pos = '1' .

wa_fcat-fieldname = 'MATNR' .

wa_fcat-tabname = 'I_MARA' .

wa_fcat-SCRTEXT_M = 'Material No' .

APPEND wa_fcat TO i_fcat .

ENDFORM
Finally display ALV with fieldcatelog instead of structure.

FORM display_alv .

CALL METHOD alv_grid->set_table_for_first_display

CHANGING

it_outtab = i_mara

it_fieldcatalog = i_fcat.

ENDFORM.
INTERACTIVE ALV
Introduction :
For Interactive ALVS, We have the event DOUBLE_CLICK in class CL_GUI_ALV_GRID.
For this event, we need to define a event handler method.
So, to define a event handler method, we need to define a LOCAL CLASS as below.
CLASS lcl_event_handler DEFINITION .
ENDCLASS
Once we define a LOCAL CLASS, we need to define a event handler method as below.
CLASS lcl_event_handler DEFINITION .
PUBLIC SECTION .
METHODS : handle_double_click FOR EVENT double_click
OF cl_gui_alv_grid
IMPORTING e_row.
ENDCLASS

The E_ROW contains the importing parameters.


E_ROW-INDEX gives the index or row no where double click event is raised.
Using this we can write our own logic
Same steps as previous program with additional changes as below.
STEP1: Create data declarations for ALV/custom container, also define object or local class.
CLASS lcl_event_handler DEFINITION DEFERRED .
DATA : i_mara TYPE TABLE OF ty_mara .
DATA : i_makt TYPE TABLE OF ty_makt .
DATA : wa_mara TYPE ty_mara .
DATA : wa_makt TYPE ty_makt .
*BASIC ALV
DATA : alv_cont TYPE REF TO cl_gui_custom_container.
DATA : alv_grid TYPE REF TO cl_gui_alv_grid .
DATA : i_fcat TYPE lvc_t_fcat .
DATA : wa_fcat LIKE LINE OF i_fcat .
*SECONADARY LIST
DATA : alv_cont2 TYPE REF TO cl_gui_custom_container.
DATA : alv_grid2 TYPE REF TO cl_gui_alv_grid .
DATA : i_fcat2 TYPE lvc_t_fcat .
DATA : wa_fcat2 LIKE LINE OF i_fcat .
DATA : obj TYPE REF TO lcl_event_handler .
STEP2 : Define a local class and its implementation.

STEP3: Define and implement EVENT HANDLER method.

CLASS lcl_event_handler DEFINITION .

PUBLIC SECTION .

METHODS : handle_double_click FOR EVENT double_click

OF cl_gui_alv_grid

IMPORTING e_row.

ENDCLASS .

CLASS lcl_event_handler IMPLEMENTATION.

METHOD handle_double_click .

ENDMTHOD.

ENDCLASS.
STEP4 : Create screen 100.

STEP3: Design screen with custom container

STEP4:Click on flow-logic and create the PBO Module and write below code

MODULE STATUS_0100 OUTPUT.

PERFORM CREATE_OBJECTS.

PERFORM GET_DATA.

PERFORM create_fcat .

PERFORM DISPLAY_ALV .

CREATE OBJECT OBJ. create object for local class

SET HANDLER obj->handle_double_click FOR alv_grid . use set handler

ENDMODULE.
Write the custom logic for DOUBLE_CLICK event
CLASS lcl_event_handler IMPLEMENTATION.
METHOD handle_double_click .
READ TABLE i_mara INTO wa_mara INDEX e_row-index .
SELECT * FROM makt
INTO CORRESPONDING FIELDS OF TABLE i_makt
WHERE matnr = wa_mara-matnr .

CALL SCREEN 200.


ENDMETHOD .
ENDCLASS. "LCL_EVENT_HANDL
Screen 200.

Create screen 200.

Place a custom container by name ALV_CONT2.

Write the below code in PBO of screen 200.

Perform create_object2.

Perform create_Fcat2

Perform Display_ALV2
FORM create_objects2 .
IF alv_cont2 is initial .
CREATE OBJECT alv_cont2
EXPORTING
container_name = 'ALV_CONT2.
CREATE OBJECT alv_grid2
EXPORTING
i_parent = alv_cont2.
Endif.
ENDFORM.
FORM create_fcat2 .
If I_FCAT IS INTIAL .
CLEAR wa_fcat2 .
wa_fcat2-col_pos = '1' .
wa_fcat2-fieldname = 'MATNR' .
wa_fcat2-tabname = 'I_MAKT' .
wa_fcat2-scrtext_m = 'Material No' .
APPEND wa_fcat2 TO i_fcat2 .
ENDIF.
ENDFORM
FORM display_alv2 .
CALL METHOD alv_grid2->set_table_for_first_display
CHANGING
it_outtab = i_makt
it_fieldcatalog = i_fcat2.
ENDFORM.
INTERACTIVE ALV WITH 2 ALV GRIDS ON
A SINGLE SCREEN
STEP1: Create data declarations for ALV/custom
container, also define object or local class.

STEP2 : Define a local class and its implementation.

STEP3: Define and implement EVENT HANDLER method.

STEP4 : Create screen 100.

STEP5: Design screen with 2 custom containers by


name ALV_CONT1 & ALV_CONT2.

STEP6: Click on flow-logic and create the PBO Module


and write below code
IF ALV_CONT1 IS INITIAL.

PERFORM CREATE_OBJECTS.

PERFORM GET_DATA.

PERFORM CREATE_CAT1 .

PERFORM DISPLAY_ALV1 .

CREATE OBJECT OBJ. create object for local class

SET HANDLER obj->handle_double_click FOR alv_grid . use set handler


ELSE.
PERFORM CREATE_OBJECTS2.

PERFORM CREATE_FCAT2.

PERFORM DISPLAY_ALV2.
ENDIF.
DISPLAYING LOGO & TOP OF PAGE IN OO-ALV
CL-DD-DOCUMENT : It is the class used to display logo & top-of-page.

Below are the commonly used methods :

Add - picture

New line

Add gap

Add text

Display document

Steps :

STEP1 :Upload logo into SAP document server using OAER Tcode.

STEP2: Create data declaration

STEP3 : Create a screen 0100

STEP4: Place to custom containers by names LOGO & ALV_CONT


STEP5: Write the bellow code in pbo

PERFORM CREATE_OBJECTS.

PERFORM GET_DATA.

PERFORM CREATE_FCAT .

PERFORM DISP_LOGO.

PERFORM DISPLAY_ALV1 .
FORM DISP_LOGO .

DATA : HEADING TYPE SDYDO_ATTRIBUTE .

CALL METHOD DD->ADD_PICTURE

EXPORTING

PICTURE_ID = 'ARJU' .

CALL METHOD DD->ADD_GAP

EXPORTING

WIDTH = '25' .

HEADING = CL_DD_DOCUMENT=>HEADING .

CALL METHOD DD->ADD_TEXT

EXPORTING

TEXT = 'MATERIAL MASTER REPORT'

SAP_STYLE = HEADING .
CALL METHOD DD->DISPLAY_DOCUMENT

EXPORTING

CONTAINER = 'LOGO.

ENDFORM. " DISP_LOGO


USING FINAL/FRIEND AND ALIAS
FINAL:-
It is a keyword which specifies weather inheritance is possible or not.

If FINAL option is selected, we cannot create inheritance (or) the class cannot be inherited.

If FINAL option is not selected, we can create inheritance.

FRIENDS:-
By default , the child class cannot access the private variables of a SUPER class.

It can access only PUBLIC&PROTECTED variables.

FRIEND is a concept which is used to access the PRIVATE variables of a SUPER class.

So, To access the PRIVATE variables of a super class, the SUPER class has to treat the
child as a friend.

To do this, go to the super class Go to FRIENDS tab Enter child class name

press Enter.

From now onwards the child class can access the PRIVATE variables of a SUPER class.
Steps for using FRIEND concept:-

Go to Super class ZCL_SUPER.

Create a private variable.

PR_NAMEINSTANCEPRIVATETYPECHAR20.

Go to FRIENDS Tab.

Give child class name as ZCL_CHILD.

Save & activate.

Go to child class ZCL_CHILD.

Create a method by name Friend_Method.

Write the below code.

DATA: obj type REF TO ZCL_SUPER.

CREATE OBJECT obj.

ObjPR_NAME = IBM INDIA.

WRITE :/ objPR_NAME.

Save , activate & test it.


Create a ABAP program & write the below code.

DATA: obj type REF TO ZCL_CHILD.

CRATE OBJECT obj.

Call method obj Friend_Method.

Save ,activate & test it.

ALIASES:-

It is used to provide the alternative name for the interface methods.


Interview Questions
Interview questions:

What is a class?how do u create

What is an object? how do u create

What do u mean by attributes and methods?Any real time ex?

What is the diff b/w instance and static comp?how do u access


them?Explain?

What is diff b/w private,public,protected?

In how many ways we can create a class?

How do u create a method?

Can we define user defined types in classes?

What is a table type and what is its use??

What is an event?
Interview questions:

What is an event handler method?explain syntax?

What is set handler? please explain syntax?

How do u raise an event?

Explain the concept of events?any real time ex?

What is a constructor and types?

Why do we use construcors? Ay real time ex?

What is the diff b/w instance and static construcor?

Can we define importing and exp parameters for constructors?

Which constructor I s executed first?

What is an Inteface?any real time ex?


Interview questions:

What is polymorphism?

How do we use interfaces in classes?

What is aliases?

What is inheritance?explain syntax?

What is super?

What is redifination?

Do w have multiple inheritance and mulit level inheritance?

What is an abstract class?

Can we create an object to abstract class? If no,then how?

What is FINAL?
Interview questions:

Can we access private variables of super class? If yes how??

Did you work on OOPS ALV? If yes explain?

What are the advantages of OO ALVS?

What are the classes in OO ALVS?

What are the methods?

What is custom container?

What is a fieldcatelog?

Did u work on interactive ALV? I f yes explain the procedure?

Also explain the procedure of interactive ALV using FMS?

Can we display 2 grids on a single screen, If yes how?

How did u display logo/top of page in OOALV and normal ALV?


OOPS ALV WITH SPLITTER CONTAINER
SPLITTER CONTAINER
STEP1: Create data declarations for

CL_GUI_CUSTOM_CONTAINER.

CL_GUI_SPLITTER_CONTAINER.

CL_GUI_CONTAINER.

CL_GUI_ALV_GRID.

STEP2 : Create screen 100.

STEP3: Design screen with custom container by name MAIN_CONT

MODULE STATUS_0100 OUTPUT.

STEP4:Click on flow-logic and create the PBO Module and write below code

PERFORM CREATE_OBJECTS.

PERFORM SPLIT_MAIN_CONT.

PERFORM DISP_ALV1 .

PERFORM DISP_ALV2.

ENDMODULE.
STEP1 :
DATA : O_CC TYPE REF TO cl_gui_custom_container .
DATA : O_PART1 TYPE REF TO CL_GUI_CONTAINER.
DATA : O_PART2 TYPE REF TO CL_GUI_CONTAINER.
DATA : O_SC TYPE REF TO CL_GUI_SPLITTER_CONTAINER.
DATA : O_GRID1 TYPE REF TO CL_GUI_ALV_GRID .
DATA : O_GRID2 TYPE REF TO CL_GUI_ALV_GRID .
DATA : I_MARA TYPE TABLE OF MARA .
DATA : I_MAKT TYPE TABLE OF MAKT .
STEP2 :
START-OF-SELECTION .
CALL SCREEN 0300.
STEP3:
Design screen with custom container by name MAIN_CONT
STEP4:
Click on flow-logic and create the PBO Module and write below code
module STATUS_0300 output.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
PERFORM CREATE_OBJECTS.
PERFORM SPLIT_MAIN_CONT.
PERFORM DISP_ALV1.
PERFORM DISP_ALV2.
endmodule.
form CREATE_OBJECTS .
CREATE OBJECT o_cc
EXPORTING
container_name = 'MAIN_CONT'.
endform.
form SPLIT_MAIN_CONT .

CREATE OBJECT o_sc


EXPORTING
parent = O_CC
rows = 2
columns = 1

CALL METHOD o_sc->get_container


EXPORTING
row =1
column = 1
RECEIVING
container = O_PART1
CALL METHOD o_sc->get_container
EXPORTING
row =2
column = 1
RECEIVING
container = O_PART2

endform.
form DISP_ALV1 .
CREATE OBJECT o_grid1
EXPORTING
i_parent = O_PART1 .

SELECT * FROM MARA INTO TABLE I_MARA UP TO 100 ROWS .

CALL METHOD o_grid1->set_table_for_first_display


EXPORTING
i_structure_name = 'MARA'
CHANGING
it_outtab = I_MARA .
endform.
form DISP_ALV2 .

CREATE OBJECT o_grid2


EXPORTING
i_parent = O_PART2 .

SELECT * FROM MAKT INTO TABLE I_MAKT UP TO 100 ROWS .

CALL METHOD o_grid2->set_table_for_first_display


EXPORTING
i_structure_name = 'MAKT'
CHANGING
it_outtab = I_MAKT .

endform.
OO ALV WITH TREE
OO ALV WITH TREE

STEP1: Create data declarations for ALV/custom container.

STEP2 : Create screen 100.

STEP3: Design screen with custom container with name as TREE

STEP4:Click on flow-logic and create the PBO Module and write below code

MODULE STATUS_0100 OUTPUT.

PERFORM CREATE_OBJECTS.

PERFORM GET_DATA.

PERFORM create_fcat .

PERFORM CALC_SUBTOTALS.

PERFORM DISPLAY_ALV .

ENDMODULE.
STEP1:

data : o_tree type ref to cl_gui_alv_tree_simple .


data : o_cc type ref to cl_gui_custom_container .
data : i_fcat type lvc_t_fcat .
data : wa_fcat like line of i_fcat .
data : i_sort type lvc_t_sort .
data : wa_sort like line of i_sort .
data : i_bseg type table of bseg .
data : wa_bseg type bseg .

STEP2: Create screen 100.

start-of-selection .
call screen 100.
STEP3:

Design screen with custom container with name as TREE

STEP4:

Click on flow-logic and create the PBO Module and write below code

MODULE status_0100 OUTPUT.


* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
PERFORM create_objects .
PERFORM get_data.
PERFORM create_fcat .
PERFORM calc_subtotals .
PERFORM display_tree .
ENDMODULE.
FORM create_objects .
CREATE OBJECT o_cc
EXPORTING
container_name = 'TREE' .
CREATE OBJECT o_tree
EXPORTING
i_parent = o_cc .
ENDFORM.

FORM get_data .
SELECT * FROM bseg
INTO TABLE i_bseg UP TO 100 ROWS .
ENDFORM.
FORM create_fcat .
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'BSEG'
CHANGING
ct_fieldcat = i_fcat.

LOOP AT i_fcat INTO wa_fcat .


CASE wa_fcat-fieldname .
WHEN 'DMBTR' .
wa_fcat-do_sum = 'X' .
ENDCASE .
MODIFY i_fcat FROM wa_fcat INDEX sy-tabix .
ENDLOOP .
ENDFORM.
FORM calc_subtotals .

wa_sort-spos = '1' .
wa_sort-fieldname = 'BUKRS' .
wa_sort-up = 'X' .
wa_sort-subtot = 'X' .
APPEND wa_sort TO i_sort .

wa_sort-spos = '2' .
wa_sort-fieldname = 'BELNR' .
wa_sort-up = 'X' .
wa_sort-subtot = 'X' .
APPEND wa_sort TO i_sort .

wa_sort-spos = '3' .
wa_sort-fieldname = 'GJAHR' .
wa_sort-up = 'X' .
APPEND wa_sort TO i_sort .

ENDFORM.
FORM display_tree .
CALL METHOD o_tree->set_table_for_first_display
CHANGING
it_outtab = i_bseg
it_fieldcatalog = i_fcat
it_sort = i_sort.

CALL METHOD o_tree->expand_tree


EXPORTING
i_level = '1'.
ENDFORM.
.THANK YOU

SAP AG 2001, Smart Forms - the Form Printing Solution,


Claudia Binder / Jens Stumpe 188