Online Tutorials & Training Materials |
Register Login

Classes in SAP ABAP

|| || 1

Classes in SAP ABAP

1. Features of object-oriented programming

An ABAP program consists of various program blocks, such as data declaration, start-of-selection, end-of-selection. The program blocks are executed in sequence. This is known as the procedural programming model, which is the classical approach to programming. The procedural programming model uses functions and subroutines that can be called in the program. The data and the functions are not linked. Object-oriented programming is based on the concept of classes.

A class is a collection of similarwith properties similar to that of the class. An object is an instance of a class.

For example, if there are two departments – Finance and HR – in a company, "Finance" and "HR" objects of the class "Department". They have properties similar to that of "Department". Object-oriented programming uses methods that can be called in a program. The data and methods linked and belong to the class in which they are defined.

Object-oriented programming offers various benefits over procedural programming. The benefits are

  • It enables you to implement business models
  • It enables you to reuse the code when creating a new program
  • Multiple programmers can develop specific sections of a program
  • Programs are easier to edit because changes in a specific section don't affect theprogram
  • Programs are easy to understand because they contain separate program blocks
  • The size of program code is less

A class contains three components. These are

  • Attributes
  • Methods
  • Events


Attributes are variables that you define in a class. Attributes specify the data type of the objects that a class supports. For example, you can define a variable "name" with the character data type and a variable attributes "emp_code" with the numeric data type in a class. Therefore, the class supports objects of the character and numeric data types.


Methods are the functions that a class supports. Methods receive values from and pass values to a program. For example, you can define a method "display_data" that displays the name of an employee based on employee code.


Events are functions that are triggered based on the result of a condition. When an event is triggered, special program blocks, called event handlers are executed. methods events

2. Defining a Class and its Attributes

Classes form the basis of object-oriented programming.

There are two types of classes:

  • Local
  • Global

Local classes can only be accessed by programs on the local application server. You define local classes by using the ABAP Editor. Global classes can be accessed by the programs on all the application servers. You define global classes by using a tool called Class Builder.

A class consists of

  • A definition part
  • An implementation part

You define various components – attributes, events, and methods – in the definition part. The implementation part contains the code for the events and methods defined in the definition part.

The three sections in the definition part of a class are

  • Public
  • Protected
  • Private


The public section contains components that all other classes can access. Suppose there are three classes "A", "B", and "C". if you define a variable "company_code" in the public section of the class "A", the classes "B" and "C" can also access the variable "company_code".


The protected section contains components that a superclass and its subclasses can access. For example, you have defined a superclass "department" and its subclass "finance". If you define a variable "employee" in the protected section of "department", both "department" and "finance" can access "employee". However, any other classes can not access the variable "employee".


The private section contains components that only the superclass can access. The subclasses can't access the private components. For example, you have defined a superclass "department" and its subclass "finance". Now, if you define a variable "employee" in the private section of "department", then only "department" can access "employee". public protected private When defining a class, you specify the keyword class along with the class name. You also specify the keywords definition or implementation to indicate the part of the class you are defining.

3. Working with Methods

Methods are components of a class. Methods receive data from a program, process the data, and return the output to the program.

If you want to use methods in an ABAP program, you need to first define them in the definition part of a class. You then write the complete code of the method in the implementation part of the class.

You use the methods statement to define a method in a class. You need to specify the method name in the methods statement.

methods : method name.

Suppose you define a method "display_attributes" in the class "employees". Only the objects of the class "employee" can access this private method.

class employees definition.
private section.
methods : display_attributes.

When you need to define a method that receives data from the program, you use the keyword importing . You specify the name of the data object in the methods statement.

methods method name importing variable type data type.

Suppose you want to define a method "get_name" that receives data from the program and stores it in the variable "name". The variable is 15 characters long.

methods get_name importing name(15) type c.

When you want to return the output of the method to the program, you use the keyword exporting to define the method.

methods method name exporting variable type data type.

You use this code if you want to define a method "display_name" that passes the value stored in the variable "name" to the calling program.

methods display_name exporting name(15) type c.

Suppose you want to define a method "display_info" that uses a variable "code" to pass an output to the calling program.

class employee definition.
private section.
methods display_info MISSING CODE code type c.


  • 05 Aug 2008 11:59 am
    Excellent material for beginner and mid level Application Developers

Related Articles

0.0127 seconds.