Introduction to Class and Object - ABAP OOPS

This article covers the basic introduction of ABAP OOPS concepts like definition of class, object, attributes, method and their visibility.

Introduction to Class and Object - ABAP OOPS

Class: A class defines the structure (attributes) and behavior (methods) of objects

Types of classes in ABAP:

Global Class: Classes created in SE24 transaction are known as Global Class. The Class created in SE24 can be accessed by all ABAP programs.

Local Class: Class created in SE38 as an ABAP program or in include is known as Local class. These are limited to the program in which it is defined or created. We cannot create objects for local class in other programs.

Object: Object is an instance of class.

Components of the Class:

Attributes: Variables and Constants declared within a class.

Instance: Instance attributes are specific to the object state. They can have different values for each object instance. It is declared using DATA statement.

Static: Static attributes are common to all the instances of a class. It can be accessed directly using Class name without creating the object reference. Static attributes are declared using CLASS-DATA.

Constant: Constants contain one value across all the objects. A constant cannot be changed.

Methods: Methods are the coding blocks which provides functionality like function modules.

·        Methods are defined in DEFINITION part and logic is implemented in IMPLEMENTATION part of a class.

·        Methods can access all the attributes of a class.

·        A method can be called using CALL METHOD statement.

·        Static methods are defined using statement CLASS-METHOD.

·        A Static method can be called using the class name without creating an instance of class.

·        If two variables are declared with same name one at class level and another in method then the class level variable can be accessed in method using ME keyword.

·        A Static method can access only static attributes of a class whereas an instance method can access both the instance and static attributes of a class.


Attributes and Methods declared in

Public Section: can be accessed by that class and other class and subclass of the program
Protected Section: can be accessed by that class and sub class or derived class only
Private Section: can be accessed by that class and not by any other class or subclass.

A Simple Example:

REPORT zzap_test.

    DATA: text1 TYPE char25 VALUE 'Public Data'.
    METHODS meth1.

    CLASS-DATA: text_st TYPE char25 VALUE 'Static Data'.
    CLASS-METHODS: meth2.

    DATA: text2 TYPE char25 VALUE 'Protected Data'.

    DATA: text3 TYPE char25 VALUE 'Private Data'.

  METHOD meth1.
    DATA: text1 TYPE char25 VALUE 'METHOD Data' .
    WRITE: / 'Public Method:',
           / text1,
           / me->text1,
           / text2,
           / text3.
  METHOD meth2.
    WRITE: / 'This is Static Method'.


*  DATA: lo_obj1 TYPE REF TO lcl_class1.
*  CREATE OBJECT: lo_obj1.
  DATA(lo_obj1) = NEW lcl_class1( ). " New Syntax
  CALL METHOD: lo_obj1->meth1.
  WRITE: / lo_obj1->text1.

  CALL METHOD lcl_class1=>meth2. " Static Method Call.

 Output :

Post a Comment

Previous Post Next Post