0% found this document useful (0 votes)
2 views

Object Oriented ABAP Introduction

The document provides an introduction to Object Oriented ABAP programming, focusing on the concepts of classes, objects, attributes, methods, and events. It explains the differences between global and local classes, the visibility of class components, and how to create and use classes and methods in SAP. Additionally, it includes practical examples of creating classes, methods, and utilizing them in ABAP programs to manage material data from the MARA table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Object Oriented ABAP Introduction

The document provides an introduction to Object Oriented ABAP programming, focusing on the concepts of classes, objects, attributes, methods, and events. It explains the differences between global and local classes, the visibility of class components, and how to create and use classes and methods in SAP. Additionally, it includes practical examples of creating classes, methods, and utilizing them in ABAP programs to manage material data from the MARA table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 50

Introduction to Object Oriented ABAP, Concepts and principles of Object Oriented

ABAP programming

 The main feature of Object Oriented programming is representing real-time objects in


the form of class objects.
 Object Oriented ABAP focus on representing real-time objects of classes.
 SAP ABAP Object Oriented programming is available in two flavours .
 One is Global Classes and another one is local class.

Global Class
Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for
SAP Class Builder is SE24.
Local Class
Local classes are classes which are available in ABAP programs, we can access them via
ABAP editor SE38.
ABAP Classes and It`s components:-
----------------------------------------------
What is a Class ? A class is a user defined data type with attributes, methods, events, user-
defined types, interfaces etc for a particular entity or business application .
What are Objects ? Objects are nothing but instances of classes, each object has a unique
identity that is memory and it`s own attributes.
Declaring Classes and Objects

Syntax: DATA TYPE REF TO . "Declaring global class in ABAP program using type ref
to(type reference to )
CREATE OBJECT . "Create object for the declared instance

Components of a Class:-
Attributes: Attributes are variables, constants declared within a class .
Methods: Methods are coding blocks which provides some functionality .
 These methods are similar to Function Modules in ABAP.
 Methods can access all attributes of it`s own class.
 Methods are defined in definition part and implemented in implementation part.
 We can call methods using CALL METHOD statement.

Events: Event is a mechanism through which one method of a class can raise method of other
class, without hazard of instantiating that class.
Interfaces: Interfaces are similar to classes which contain methods Without any
implementation.
Interfaces are mainly used to extend the scope or functionality of the class.

Instance and Static Components


Instance components : These components exist separately in each instance (object) of the
class and are referred using instance component selector using ' ->'
Static components : These components exists globally for a class and are referred to using
static component selector ' => ' .
Visibility of Components of Class
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


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.

Create a simple class for using attributes


Go to SE24( Class Builder).
Give the class name ZCL_SAPN1 and create.

Provide short description, save.


Save it in a Local Object or in your test package.
Go to attributes tab and define an attribute as below.

AV_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.

Save and Activate.


Now class is created, we have to use this in our program.
Create a ABAP program in SE38 and add below code

DATA : LR_CLASS TYPE REF TO ZCL_SAPN1 . "STEP1--WE DECLARE CLASSES


USING REF TO BECAUSE THEY ARE OBJECTS
CREATE OBJECT LR_CLASS. "STEP2--CREATE OBJECT FOR THE CLASS
*CALL CLASS COMPONENT WITH THE INSTANCE
LR_CLASS->AV_NAME = 'ATTRIBUTE NAME'. "USE CLASS COMPONENTS
WRITE:/ LR_CLASS->AV_NAME.
*OUT PUT WILL BE 'ATTRIBUTE NAME'

Using multiple objects of class

DATA : LR_CLASS1 TYPE REF TO ZCL_SAPN1 . "Declare first class object


DATA : LR_CLASS2 TYPE REF TO ZCL_SAPN1 . "Declare second class object

CREATE OBJECT LR_CLASS1. "Create a first object


CREATE OBJECT LR_CLASS2. "Create a second object
*CALL CLASS COMPONENT WITH THE INSTANCE
LR_CLASS1->AV_NAME = 'FIRST ATTRIBUTE NAME'. "Assign value to first object

LR_CLASS2->AV_NAME = 'SECOND ATTRIBUTE NAME'. "Assign value to second


object
WRITE:/ LR_CLASS1->AV_NAME. "OUT PUT WILL BE 'FIRST ATTRIBUTE NAME'

WRITE:/ LR_CLASS2->AV_NAME. "OUT PUT WILL BE 'SECOND ATTRIBUTE


NAME'
What is the T-Code to create class in SAP ? What is SAP Class builder and introduction
to transaction SE24 in SAP

Transaction code for SAP class builder is SE24. SAP class builder(SE24) is used to create,
display and change SAP classes.

Go to SE24 and provide some class example ex: CL_ABAP_CHAR_UTILITIES, to see what
are the components of class.

Properties: The properties tab contains class properties like created user name, last changed
user name, package etc.
Interfaces: Contains the list of the interfaces that are implemented in this class( will discuss in
later lessons).
Friends: contains friend classes ( we will discuss later lessons).
Attributes: The attributes tab contains the list of attributes declared in that class.
Methods : Contains the methods with importing and exporting parameters and with some
business functionality (source code).
Events: Contains events declared and implemented in that class.
Types: The types tab contains user defined type decelerations.
Aliases: Contains alias names for interface methods ( will discuss in later lessons).

Class methods and Creating methods in SAP classes


Creating a method in SAP Classes using Object Oriented ABAP programming

What is a method in a Class ?


Methods are coding blocks of a class, which can provide some business functionality (ex:
read material data etc), these methods are similar to Function Modules.
Methods can access all attributes of a class (defined under attributes tab), can access user
defined types ( declared under types tab).
The methods can be called using key word CALL METHOD in SAP ABAP programs.
Uses of methods in SAP classes?
These methods can be reusable in multiple ABAP programs, a class may contain more than
one method.
We will define methods under methods tab of a class, to define a method provide a method
name, level( instance or static), visibility (Public,Private,Protected) and method description.
The importing and exporting parameters of a method will defined under parameters area.
Add a method, click on parameter button and you will go to parameter interface where you
can add parameters to a particular method. (Parameters are method specific).

To write source code, click on methods tab and double click on method name (METHOD in
the above example), you will go to source code editor where you can add code.
This is how we create methods in SAP classes, we will learn some advanced concepts of
using methods in next lessons.

Requirement: Create a class method to get material details for a material number.

: Requirement analysis For the above requirement we need to create a class method to get
details of a material for a material input, for this method the importing parameter is material
no (to pass material input to method) and exporting parameter is of type MARA(work area
one material no contains only one record in MARA).Go to SE24, provide class name as
ZCL_SAPN_MATERIALS.

Provide description, save.


Save, save it in a local object, go to methods tab and declare a
method.GET_MATERIAL_DETAILS-INSTANCE-PUBLIC-Get Material details

Select parameters button and declare two parameters as below IM_MATNR-IMPORTING-


TYPE-MARA-MATNR-Material Number EX_MARA-EXPORTING-TYPE-MARA-
General Master Data
Double click on CODE icon or Go back to methods and double click on method name and
add below code.
*Select material data from mara table into exporting parameter ex_mara (work

area) for a material no im_matnr


SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

Save, activate and execute the method( press F8).


Click on execute.

Provide a material no and execute.

You will get material details for that material, now class with method is created we have to
use it in our program.
Using Class method in SE38 Program
Go to SE38 and create a program ZSAPN_GET_MATERIAL_DETAILS and follow
below steps to add code.
Step1: Declare class and create object.

DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.


CREATE OBJECT LO_MATERIAL.

Step2: Program declerations.

PARAMETERS : P_MATNR TYPE MARA-MATNR.


DATA : WA_MARA TYPE MARA. "work area to store material details

Step3: Call class method and print out put.

CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Final code will be

REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.

PARAMETERS : P_MATNR TYPE MARA-MATNR.


DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
Requirement: Develop a report to display list of materials for a material type, use Object
Oriented ABAP to get materials.

Requirement analysis: For this requirement we need to get materials from MARA(material
Master) table for the specified material type, a material type may have more than one
materials, so we have to get materials into an internal table.In SAP classes we don`t have
options to directly specify tables, for this one we need to create table type in SE11(Data
Dictionary).
Learners directly coming for this lesson, please refer previous lesson creating class method
to display materials as we are adding one more method for the same class.
Before creating method to get material details for a material type, we need to create a table
type in SE11 .
Go to SE11, select data type radio, provide name as ZSAPN_MARA, create.

A popup will open, select table type radio, enter.

Provide short text, provide line type as MARA.


Save and Activate.
Now table type is created for MARA, to create a method to get material details for a material
type, go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created
this class see previous lesson) and click change.

Go to methods tab and add a method with name GET_MATERIALS_FOR_TYPE-


INSTANCE-PUBLIC-Get materials for material type.
Click on parameters button and add parameters as below.

Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the
below code.

*Get data from mara for a material type MTART


SELECT * FROM MARA
INTO TABLE ET_MARA
up to 50 rows
WHERE MTART = IM_MTART.
*For testing purpose im getting first 50 rows for a material type

Save, activate and test the method.


Using class method in SE38 program to get Material details for a material type.
Go to SE38 and create a program ZSAPN_GET_MATERIALS_FOR_TYPE and add below
code.
REPORT ZSAPN_GET_MATERIALS_FOR_TYPE.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table to store materials
DATA : WA_MARA TYPE MARA. "work area for materials to loop
DATA : LO_MATERIALS TYPE REF TO ZCL_SAPN_MATERIALS. "declare materials
class

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

CREATE OBJECT LO_MATERIALS. "Create object for material class

START-OF-SELECTION.
CALL METHOD LO_MATERIALS->GET_MATERIALS_FOR_TYPE "call method to
get materials
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
ENDLOOP.

Program input.

Program output.
Requirement: Develop a report to display material number(MATNR), material type(mtart),
material group(MAKTL) and unit of measure(MEINS) for a given date( Created Date).

Requirement analysis: For this requirement we need to get limited fields (MATNR,
MTART, MATKL, MEINS, ERSDA from MARA (material Master) table for the specified
date(ERSDA-Created date), materials for the given date may be more than one so we need to
use tables and we need to get limited fields(columns) only (no need to get all fields data from
MARA), for this one we need to use concept of user defined table in SAP class method.
Learners directly coming for this lesson, please refer previous lesson creating class method
to display materials as we are adding one more method for the same class.
Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created
this class see previous lesson) and click change.

Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-


INSTANCE- PUBLIC-Get materials for a date.

We can declare user-defined types under types tab, go to types tab.


Click on Direct Type entry icon, save, it will take you to a editor
Don' change any thing, just replace types TY_MARA . with the below code.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.

TYPES : TT_MARA TYPE TABLE OF TY_MARA.

Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method,


select parameters button and add below parameters.
Go back to methods and double click on method GET_MATERIAL_FOR_DATE, write the
below code.

METHOD GET_MATERIAL_FOR_DATE.
*Get material no, created date, material type, material group, units of measue
*from MARA table
SELECT MATNR ERSDA MTART MATKL MEINS
FROM MARA INTO TABLE ET_MARA.
ENDMETHOD.

Save and activate.


Go to SE38 and create a program and add below code.
REPORT ZSAPN_GET_MATERIALS_FOR_DATE.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA .
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.

DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.


PARAMETERS : P_DATE TYPE MARA-ERSDA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.

CALL METHOD LO_MATERIAL->GET_MATERIAL_FOR_DATE


EXPORTING
IM_DATE = P_DATE
IMPORTING
ET_MARA = IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE:/ WA_MARA-MATNR, WA_MARA-ERSDA, WA_MARA-MTART,
WA_MARA-MATKL, WA_MARA-MEINS.
ENDLOOP.

Activate the program and test.


What are Events in SAP classes ?

What are events in SAP Classes?


Event is a mechanism by which method of one class can raise method of another class,
without the hazard of instantiating that class.
Follow the below steps to add a event
 Define an event .
 Define a method.
 Link event and method and convert the method into event-handler method.
 Create a triggering method which will raise the event.
 Use set handler and register event handler method to a particular instance in the
program.

Use below syntax to register a event handler method for a instance

SET HANDLER for . "here instance is the object created by using create object

Example of using Events in SAP OOABAP classes

Using of event handler methods in SAP classes, implementing event handler method in
OOABAP
+-

This is the basic example of using events in SAP classes.


Learners directly coming for this lesson, please refer previous lesson Events in SAP
classes and Working with class methods in SAP as we are adding one more method for the
same class.
Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created
this class see previous lesson) and click change.

Steps for creating and using event in SAP classes


 Step 1: Define an event .
 Step 2: Define a method.
 Step 3: Link event and method and convert the method into event-handler method.
 Step 4: Create a triggering method which will raise the event.
 Step 5: Use set handler and register event handler method to a particular instance in
the program.

Define an event
Go to Events tab and add a event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered

Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.
Save, double click on the method and add some code.
WRITE:/ 'NO material entered'.

Save and activate immediately.


Link event and method and convert the method into event-handler method.
Now we have to link the method to event to make the method as event handler.
Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view
icon(see below image).
A pop up will open, provide description, select Event Handler for check box, provide our
class name as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4),
enter.
You will see an icon(event handler icon), which means the method is event handler method.

Create a triggering method which will raise the event


Event handler method is created, now we have to trigger that event, the vent can be triggered
by using below syntax.
RAISE EVENT <EVENT NAME>.

We will trigger the event for the method GET_MATERIAL_DTAILS (we previously
created Get material details ), double click on the method GET_MATERIAL_DTAILS and
add the below code.
METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work area) for a
material no im_matnr
IF IM_MATNR IS INITIAL .
RAISE EVENT NO_MATERIAL.
ELSE.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDIF.
ENDMETHOD.
Use set handler and register event handler method to a particular instance in the program
Now we have to register this event in our SE38 program, to register a event handler method
we use below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>.
"here INSTANCE is object and EVENT HANDLER METHOD handler method created in
se24

Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code


REPORT ZSAPN_CLASS_EVENT.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration
DATA WA_MARA TYPE MARA. "work area to store material data

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

CREATE OBJECT LO_MATERIAL. "create object for material calsss


SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL.
"register event handler method

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Now execute the program with out giving any input, the result will be

Assignment for you: Create and implement a event and event handler method for material
not found in the above class, if user provided the input but the data is not there in MARA
table, raise an event.

what is a constructor method in SAP Classes ? and Uses of Constructor method in


OOABAP
+-

What is a constructor in a class ?


CONSTRUCTOR is a special type of method, it is executed automatically whenever a object
is created or instantiated.
These methods are mainly used to set default values in classes.
CONSTRUCTORS are two types in SAP classes.
1. CONSTRUCTOR ( Instance Constructor).
2. CLASS CONSTRUCTOR (Static Constructor).

CONSTRUCTOR.
These methods can only have importing parameters, there will not be any exporting
parameters.
The name of the CONSTRUCTOR method must be CONSTRUCTOR only.
CLASS CONSTRUCTOR (Also called as STATIC CONSTRUCTOR).
It is a type of constructor, this method will be executed automatically whenever a first call to
the class is made, the first call may be through instance or through class.
These CLASS CONSTRUCTORS are mainly used to set the default values globally i:e
irrespective of instances, these methods will not have any importing and exporting
parameters.These methods will be executed only once.
Name of the CLASS CONSTRUCTOR must be CLASS_CONSTRUCTOR.
Using CONSTRUCTOR method in SAP Classes to set global default value for the call
+-

This CONSTRUCTOR method is very useful in setting default value in a class, the below is
the example of using CONSTRUCTOR method in a SAP Class.Requirement: Display for
material description for a material, and depends upon language selected.
All material descriptions are stored in MAKT table, for a material there may be different
descriptions example for English one description, for German one description etc, we need to
get description based on the language, for this one we create a class method for re-
usablity(the same method can be used in different programs to get descriptions based on the
selected languages).
Go to SE24,provide class ZCL_SAPN_MATERIAL and click on change.

Go to Methods and provide method name as CONSTRUCTOR, enter

Once you press enter, you will see a symbol (Constructor symbol), it indicates that the
method is a constructor method.

Now put cursor on method name and click on parameters button.


Click on parameter button and add below parameter( what ever the parameters you add, they
will become importing parameters only).

Select attributes tab and add below attribute.

Go to methods and double click on CONSTRUCTOR method and add below code.
Save and activate, go to methods tab and add one more method
GET_MATERIAL_DESCRIPTIONS to get material descriptions.

Select parameters button and add below parameters.


Save, go back to methods and double click on method name
GET_MATERIAL_DESCRIPTIONS, add below code.

METHOD GET_MATERIAL_DESCRIPTIONS.

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = LANGUAGE. "LANGUAGE IS THE ATTRIBUTE DEFINED IN
METHOD
ENDSELECT.

ENDMETHOD.
Using CONSTRUCTOR method with ABAP program
Go to SE38 and create a program ZSAPN_MATERIAL_DESCRIPTION and follow below
steps.
Step1: Data declarations
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
DATA : WA_MAKT TYPE MAKT. "declare work area for makt
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

Step2: Create object for material class, the object can be created using ABAP patterns also.
To create object, click on pattern button.

A popup will open, select ABAP object patterns and enter.


One more pop up will come, select create object, provide instance name (We declared at the
declerations step LO_MATERIAL ), provide class name (our class name is
ZCL_SALN_MATERIALS) and enter.
Now the CONSTRUCTOR method is triggered, it will ask for a parameter IM_SPRAS
(Which is importing parameter of constructor method).It will set language key as English.
Finally call the method and get material descriptions.
The final code will be .
REPORT ZSAPN_GET_MATERIAL_DESCRIPTION.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
CREATE OBJECT LO_MATERIAL
EXPORTING
IM_SPRAS = 'E'.

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE : WA_MAKT-MATNR, WA_MAKT-MAKTX.

Testing the above program


Go to MAKT table and get a material number with multiple language descriptions.

Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).

What is a Class Constructor in OOABAP ?, Working with Class Constructor in Object


Oriented ABAP Programming
+-

This is a type of CONSTRUCTOR, this method is executed whenever a first call to the class
is made, the call may be through instance or through class name.
These are also called as STATIC CONSTRUCTORS, the name must be
CLASS_CONSTRUCTOR.
If you are coming directly for this lesson we highly recommend go through previous
lesson Using CONSTRUCTOR method in SAP classes for better understanding.

Example: We will add a class constructor to make default material type as 'FERT'(Finished
Product)
Follow the below steps to create a class constructor.
Go to SE24, provide the class name ZCL_SAPN_MATERIALS, click on change.
Go to attributes tab and add an attribute to make default material type.

Go to methods and add a method name CLASS_CONSTRUCTOR as below.

We can not add any exporting and importing parameters to a CLASS_CONSTRUCTOR


method.
Double click on the method CLASS_CONSTRUCTOR and add below code.
METHOD CLASS_CONSTRUCTOR.
*Set default material type as FERT
MAT_TYPE = 'FERT'.
ENDMETHOD.

Here we take a small example to test this one.The below on is a simple example and explains
you a lot.
REPORT ZSAPN_CLASS_CONSTRUCTOR.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class

CREATE OBJECT LO_MATERIAL "create object CONSTRUCTOR method will trigger


EXPORTING
IM_SPRAS = 'E'.
**When ever first call to a class is made class constructor will trigger
WRITE:/ 'Executed through class constructor', ZCL_SAPN_MATERIALS=>MAT_TYPE.
"Executed through Class Constructor

WRITE:/ 'Executed through Constructor', LO_MATERIAL->LANGUAGE . "Executed


through constructor method

If we have both CONSTRUCTOR and CLASS_CONSTRUCTOR in our class, upon a class


call which will trigger first.....First CLASS_CONSTRUCTOR will be triggered.

Interface Concept in Object Oriented ABAP

Before going to interfaces, we must know about Polymorphism .


What is Polymorphism in Object Oriented Programming model ?

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.
Interface is one of the concept in Object Oriented ABAP to achieve Polymorphism.
What is an interface in Object Oriented ABAP ?
 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.
 The interfaces can be used by no of classes to extend the functionality of the class.
 To implement an interface in a class just give the name of interface under the
'Interfaces' tab.
 All the interface methods will be automatically copied to the classes in a particular
class without effecting the other classes.
 The main use of interfaces is re-usability and maintain standard project framework.

In the above diagram, application is business application, Class A, Class B, Class C, Class D
and Class E are independent classes, all these classes are using one Interface called
Interface( in diagram).
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 POLYMORPHISM.
Polymorphism is implemented in the from of interface.

Creating Class Interface in SAP OOABAP

As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all
the custom interfaces starts with ZIF_ , in the below example we will be learning how to
create a use interfaces in SAP.
All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with
ZIF_, based on the provided name (at the time of creation), SAP will automatically determine
and creates (interface or class).
Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.
Provide description, save, save it in a local object.

Go to methods tab, add method name as GET_MATERIAL_DETAILS-INSTANCE, click on


parameters button and add below parameters.

Save, Go back to methods and add one more method GET_MATERIAL_DESCRIPTIONS.


Click on parameters and provide parameters as below.

Save and activate.


We have created an interface with two methods (which dosen`t have implementation).
No Go to SE24 and create a class ZCL_SAPN_CLASS_EX
Provide description, save it in a local object.

Go to interfaces tab and add interface name as ZIF_SAPN_MATERIAL_EXAMPLE,


enter

Now go to methods tab, you can see all the methods in the interface are automatically copied.
Save, double click on each method and create your own implementation.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and
add below code.

SELECT SINGLE * FROM MARA


INTO EX_MARA
WHERE MATNR = IM_MATNR.

Double click on
ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add below
code
SELECT * FROM MAKT INTO EX_MAKT
WHERE MATNR = IM_MATNR
AND SPRAS = 'E'. "English description only
ENDSELECT.

Save and activate the class.


Using class in program
Create a SE38 program and follow the steps below. The interface method will be clalled by
using INTERFACENAME~METHODNAME.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
CALL METHOD LO_CLASS-
>ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS-
>ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART,
WA_MARA-MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-
MAKTX, WA_MAKT-SPRAS.

Similarly create another class and repeat the same process, we can add interfaces into multiple
classes.

Inheritance in SAP OOABAP


Last Updated: November 2nd 2016 by Ashok Kumar Reddy

What is inheritance in SAP Classes, uses of inheritance, properties of inheritance in SAP


OOABAP
+-

What is Inheritance in SAP OOABAP ?


Inheritance is the concept of passing the behavior of one class to another class.
You can use an existing class to derive a new class.
Derived class inherits the data and methods of a super class.
However they can overwrite the existing methods also add new code. Advantage of this
property is re-usability.
This means we can add additional features to an existing class without modifying it.
SUPER is the keyword used to represent the super class in oops, 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 Object Oriented ABAP we have only single inheritance. There is no multiple
inheritance.
Inheritance in general
Super Class Is a main class by using this we can derive a new class which will be called
as Child class.
Final Class is a class which can not be used for inheritance, it is a class property (check box
under properties of class).

If the final check box is selected, we can not use this class for inheritance, to use inheritance
remove the final check box.

Using inheritance in SAP OOABAP


Last Updated: November 2nd 2016 by Ashok Kumar Reddy

Working with inheritance in SAP classes, using inheritance in SAP classes


+-

Go to SE24, provide name as ZCL_SAPN_SUPERCLASS, create

A pop up will open provide description, un-select final check box, save and save as local
object.
Go to methods tab, provide method name as GET_MATERIAL_DETAILS-INSTANCE-
PUBLIC.

Go to parameters tab and add below paramaters.

Go back to methods, double click on method name and add below code.
METHOD GET_MATERIAL_DETAILS.

SELECT SINGLE * FROM MARA


INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDMETHOD.

Save and activate the class.


Super class is created, now we have to inherit this class and derive a new class(child class).
Go to SE24, provide name as ZCL_SAPN_CHILDCLASS, create.

A pop up will open provide description, click on create inheritance icon(see below image),
provide super class name.
Save, save as a local object, now you can see the inherited method, using inheritance we can
overwrite the method implementation to do this click on redifinition icon (see image below)
We will add additional functionality to get material description details along with material
details, go to attributes tab and add a attribute ls_makt-instance-public-makt.

Go back to methods, doublle click on method, uncomment the inherited code, pass parameters
and add additional code to get material descriptions.
METHOD GET_MATERIAL_DETAILS.

CALL METHOD SUPER->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = IM_MATNR
IMPORTING
EX_MARA = EX_MARA.
***Additional code to get material descriptions
SELECT * FROM MAKT
INTO LS_MAKT
WHERE MATNR = EX_MARA-MATNR.
ENDSELECT.

ENDMETHOD.

Using Class inprogram


Create a program in SE38 and follow below steps(code).
REPORT ZSAPN_CLASS_INHERITANCE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CHILDCLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare MARA
DATA WA_MAKT TYPE MAKT. "declare MAKT

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input


CREATE OBJECT LO_CLASS. "create instance for class

START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WA_MAKT = LO_CLASS->LS_MAKT. "access material description from class attribute


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS,
WA_MARA-MATKL. "print material details
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX. "print material descriptions

You might also like