Category: ABAP

ABAP

  • Differences between Constructor and Class Constructor

    Preface – This post is part of the SAP ABAP OData Tutorial series.

    Before discussing the difference between Constructor and Class Constructor, let’s have a short introduction of the two.

    Introduction

    Constructors are a special type of method in a class that is called automatically by the system to set the initial state of a new object or the class. They cannot be called using the CALL METHOD statement. The constructors are always present in a class but to implement a constructor it must be declared explicitly with the METHODS or CLASS-METHODS statements.

    There are two types of constructors: Instance constructor and Static Constructor.

    Constructor

    The predefined method CONSTRUCTOR is an instance constructor of the class.  This instance constructor is automatically called when an instance of the class is created. Its signature can have only importing parameters or exceptions. Since it is an instance method, it can access all the attributes of the class.

    Syntax to declare :

                    METHODS : constructor.

    Class Constructor

    The predefined method CLASS_CONSTRUCTOR is a static constructor of the class and is automatically called when the components of the class are accessed for the first time. Its signature cannot have importing parameter or exceptions.  It is called once for each class irrespective of the instance created.

    Syntax to declare :

                    CLASS-METHODS : class_constructor.

    Now, let’s have a look at their difference.

    Constructor and Class Constructor

    Differences between Constructor and Class Constructor

    Constructor Class Constructor
    It is executed automatically whenever an object is created or instantiated. It is executed automatically whenever the first call to the class is made.
    It is called once for each instance. It is called only once for each class and internal session.
    It is declared using METHODS statement. It is declared using CLASS-METHOD statement.
    It has only Importing Parameters. It has no Importing and Exporting Parameters.
    It can raise exception. It cannot raise exception.
    It is an Instance method. It is a static method.
    It can access all attributes of a class. It can access only static attributes of a class.

     

  • Differences between Call by Value and Call by Reference

    Preface – This post is part of the SAP ABAP OData Tutorial series.

    Before discussing the differences between Call by Value and Call by Reference, let’ have a quick introduction of the two.

    Introduction

    Modularization techniques in ABAP is a way to write codes in different processing blocks like subroutine, Function Modules and ABAP objects. If these blocks have any parameter interface, while calling them it is required to pass values to all the formal parameter interface. These blocks with parameters can be called via either Call by Value or Call by Reference.

    Call by Value

    In Call by Value, the value of the actual parameters is copied to the formal parameter of the function. The value of the two types of parameters is stored in different memory locations. The memory location of the formal parameter is allocated when the function/subroutine is called and gets freed when the function/subroutine returns. The references to the formal parameter refer to a unique memory and are only known within the function/subroutine. Any changes done inside the function/subroutine are not reflected in the actual parameter.

    Call by Reference

    In Call by Reference, the address of the actual parameters is copied to the formal parameters of the function. The references to the formal parameter reference the same memory as of the actual parameter. Any changes done inside the function/subroutine are reflected in the actual parameter i.e. all the operations are performed on the value stored at the address of the actual parameter, and the modified value is stored at the same address.

    Now, let’s have a look at their difference.

    call by value and call by reference

    Differences between Call by Value and Call by Reference

    Call by Value Call by Reference
    The value of the actual parameter is copied into the formal parameter. The address of the actual parameter is copied into the formal parameter
    Both the parameters are stored in different memory locations. Both the parameters refer the same memory location.
    The change in formal parameters is not reflected in the actual parameters. The change in formal parameters is reflected in the actual parameters.
    Original value cannot be changed. Original value is changed.

     

  • How APIs work

    Preface – This post is part of the SAP ABAP OData Tutorial series.

    Introduction

    Everything is online nowadays. From grocery website to online medical services, we can do anything and everything right from our home with the help of any electronic device such as laptop, PC or mobile. Have you ever wondered that how the App or the website you visit understands you and processes your payment. This all happens with the help of different APIs. In this article, we will learn all about APIs. All the OData servies are also an example of APIs.

    What is an API

    An API stands for Application Programming Interface. As the name suggest, an API is an interface between two applications. These are the programs written in such a way that they accept certain inputs, process them and returns an output.

    E.g. We can create an API which can accept two numbers and return their sum. It sounds crazy to do such basic job, but it is just an example.

    How APIs Works

    To better understand this flow, we will illustrate the same using step by step process, as shown below:

    How API Works

    The API is triggered by a script code in UI, in our case UI5/FIORI App. The call is in the form of either of any CRUD operation. In case of Read calls, the returning response consist of JSON data. In case of Create, Update and Delete operations, the response is just a status where 200/201 represents success. We will study more about API Response codes in upcoming article.

    An API is in the form of a URL i.e. Uniform Resource Locator.

    Example: https://service_name/payload

    An API contains the following:

    • HTTP Request: It defines which CRUD operation is being called. E.g. GET is for Read call and POST is for Create call
    • Service Name: It can be your website name or any other registered service name. In case of OData it varies with the OData name.
    • Payload: In case of Create, Update and Delete call, payload is an object sent to the API
    • Response: This is a status with response data. The status represents if the API call was successful or not.

    Example:

    In real world, we can take an example that the API accepts Credit Card details with an amount to deduct, process deduction and returns success status. These types of APIs are widely used by payment gateway industries. The same is illustrated in the GIF below:

    APIs

    Advantages of an APIs

    • It helps in abstraction of Data between two systems
    • It reduces the duplication of code and architecture and enhances modularity
    • It reduces the coding language dependency as any two systems using different coding languages can be connected via an API
    • It reduces rework and enhances code quality as well as readability
  • Difference between Static Attribute and Instance Attribute

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the differences between a Static attribute and Instance attribute, let’ have a quick introduction.

    ABAP Class Attribute

    Attributes are data objects of any data type within a class. The state of the class is defined by the content of the attribute. They are defined in the declaration section of the class.

    Static Attribute

    Static attributes and instance attributes are two common types of attributes in the class.

    Static attribute defines the state of the class and this state is common to all the instances of the class. Static attributes can be accessed before any instance of the class is created. For a static attribute, memory for it is allocated only once irrespective of the class instance.  All the instances of the class share the same static attribute. Whenever there is a change in the value of this attribute, it is visible to all the instances of the class.

    Syntax to declare a static attribute:

                    CLASS-DATA static_attribute TYPE data_type.

    Instance Attribute

    Instance attribute defines the instance-specific state of the object. An instance attribute can only be accessed using a reference variable. Memory for an instance attribute is allocated for each instance of the class i.e. if a class have 3 instance attributes and 2 instances of the class then 6 memory locations are allocated for this attribute. Whenever there is a change in the content of the attribute, this change is only visible to that instance which has changed it.

    Syntax to declare an Instance attribute:

    DATA instance_attribute TYPE data_type.

    Difference between Static Attribute and Instance Attribute

    Now, let’s have a look at their difference.

    Static Attribute Instance Attribute
    It can be called irrespective of the class instance. It can be only called using object reference.
    It is declared using CLASS-DATA keyword. It is declared using DATA keyword.
    Memory is allocated only once. Memory is allocated for each instance.
    Every instance shares the same static attribute. Each instance has its own copy of the instance attribute.
    Change to static attribute are reflected to all the instances of the class. Change to an instance attribute is restricted to that particular instance.
    It cannot be redefined in subclasses. It can be redefined in subclasses.

     

  • Difference between Local Classes and Global Class

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the difference between Local classes and Global class, let’s have a short introduction of the two.

    Class

    Classes are a blueprint for objects and represent a set of properties or methods common to all objects of the same type. Classes in SAP ABAP can be defined either locally or globally.

    Local classes

    Local classes hide in a report source whereas Global classes sit themselves out centrally in the class pool within the class library of the R/3 Repository. If you are creating a report to do some specific task without worrying about the outside world, you can create a local class. But if you’re working on a class that may be of interest to other reports, consider creating a global class.

    Let see an example of local class implementation below:

    *---------------------------------------------------------------------*
    *       CLASS MAIN DEFINITION
    *---------------------------------------------------------------------*
    CLASS main DEFINITION.
      PUBLIC SECTION.
    
        "// Instance Methods ( Note: we use the statement 'METHODS'
        "// to define an instance method )
        METHODS set_data IMPORTING i_data TYPE string.
    
        "// Instance Methods ( Note: we use the statement 'CLASS-METHODS'
        "// to define a static method )
        CLASS-METHODS set_classdata IMPORTING i_data TYPE string.
    
      PROTECTED SECTION.
    
        "// Instance Attribute ( Note we use the statement 'DATA'
        "// to define an instance attribute )
        DATA attribute TYPE string.
        "// Static Attribute ( Note we use the statement 'CLASS-DATA'
        "// to define a static attribute )
        CLASS-DATA classattribute TYPE string.
    
      PRIVATE SECTION.
    
        "// Instace event ( Note we use the statement 'EVENTS'
        "// to define aN instance event )
        EVENTS event EXPORTING value(e_data) TYPE string.
    
        "// Instace event ( Note we use the statement 'CLASS-EVENTS'
        "// to define a static event )
        CLASS-EVENTS classevent EXPORTING value(e_data) TYPE string.
    
        "// For more informations about events see the following example:
        "// ABAP Objects - Creating your First Local Class - Using Events
    
    ENDCLASS.                    "main DEFINITION
    Class Implementation
    
    Error rendering macro 'code': Invalid value specified for parameter 'lang'
    *---------------------------------------------------------------------*
    *       CLASS main IMPLEMENTATION
    *---------------------------------------------------------------------*
    CLASS main IMPLEMENTATION.
      METHOD set_data.
        CONCATENATE 'Instance Attribute value' i_data
                                   INTO attribute SEPARATED BY space.
      ENDMETHOD.                    "set_data
      METHOD get_data.
        MOVE attribute TO r_data.
      ENDMETHOD.                    "get_data
      METHOD set_classdata.
        CONCATENATE 'Static Attribute value' i_data
                                   INTO classattribute SEPARATED BY space.
      ENDMETHOD.                    "set_classdata
      METHOD get_classdata.
        MOVE main=>classattribute TO r_data.
      ENDMETHOD.                    "get_classdata
      METHOD print_attribute.
        WRITE: lv_data, /.
      ENDMETHOD.                    "print_attribute
      METHOD print_classattribute.
        WRITE: lv_data, /.
      ENDMETHOD.                    "print_classattribute
    ENDCLASS.                    "main IMPLEMENTATION

     

    When a class is used in an ABAP program, the system first searches for the local class with the specified name, if not found, it looks for a global class.

    As the Local class is visible to a particular program, visibility of all the component can be set to PUBLIC. A Local class can be defined in a global class. It is defined in the Local Types section and implemented in the Implementation section of the Class Editor.

    Global Class

    The Global class is visible to all the programs, visibility to all components are explicitly assigned. The components with PRIVATE visibility are only accessible in the class itself and are not visible to the subclass.

    SAP ABAP Global Class Builder

    Now, let’s have a look at their difference.

    Difference between Local Classes and Global Class

    Local Class Global Class
    It is defined within a program. It is defined in Class Builder (T-Code SE24).
    It can be only used in programs in which it is defined. It is stored in class pools and can be used by any ABAP programs.
    It can be designed “more open” as it is only accessible to within the context. It can be designed “less open” as it is accessible to all the ABAP programs.
    It can be defined in a global class. It cannot be defined in a local class.

     

  • Difference between Class and Function Module

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before jumping to the difference between Class and Function Module, let’s have a short introduction.

    An ABAP program consists of program blocks and is executed sequentially. This is a classical approach for programming known as procedural programming model. This model uses functions and subroutines.

    Object Oriented programming paradigm is based on class and object and aims to implement real-world entities such as inheritance, abstraction, etc.

    Function Module

    Function modules are procedures that have a set of re-usable statements with importing, exporting parameters, etc. These are created in ABAP workbench using the Function Module Builder. They are managed in the central function library. They play an important role in updating and in the interaction between different SAP systems, between SAP systems and remote systems through remote calls. Unlike Include, they can execute independently. Every function module needs to be assigned to a function pool called the function group. A Function group is a container that holds function modules that should be together logically. Function modules also support exception handling to catch any errors while they are running.

    Class

    Classes are a blueprint for objects and represent a set of properties or methods common to all objects of the same type. The components of a class are called data members. The class provides the flexibility to assign visibility to all its data members. Every instance or objects have unique identity and has its own set of values for the attributes. A class can inherit another class and interfaces. You can read more about classes here.

    Now, let’s have a look at their difference.

    Difference between Class and Function Module

    Function Module Class
    It is procedural oriented. It is object oriented.
    It is created using Function Module Builder (T-code se37). It is created using Class Builder (T-Code SE24).
    It is always public. A class can be Public, Private or Protected.
    Variables are always Private. Variables can be Public, Private or Protected.
    Cannot create any instances. Can create multiple instances.
    Inheritance is not possible. Inheritance is possible.
    Screens can be created using it. To create a screen, the class must call another program.

     

  • Difference between Static Method and Instance Method

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Introduction

    Before discussing the differences between an Instance method and Static method, let’ have a quick introduction.

    A method is a block of codes that defines behaviour of an object of the class.  The method can access all the attributes of the class and can change the object data content.

    Instance Method

    Instance methods are defined using the METHODS statement. They get reinitialized every time an instance is created.  They can access all the attributes of the class and can trigger all its events. Instance methods have the flexibility to be inherited and can be redefined in subclasses.

    Syntax to call an instance method: 

    DATA instance_name TYPE REF TO class_name.
    
    CREATE OBJECT instance_name.
    
    CALL METHOD instance_name->instance_method(…).

     

    Static Method

    Static methods are defined using the CLASS-METHODS statement. They are called only once, when the program is started, irrespective to the class instance, and don’t get reinitialized throughout the execution of the program. They can access only static attributes and can trigger only static events of the class. They can be directly accessed by the class name itself.

    Syntax to call a static method:

    CALL METHOD class_name => static_method(..).

    Now, let’s have a look at their difference.

    Difference between Static Method and Instance Method

    Instance Method Static Method
    It requires instance. It doesn’t require instance.
    It is an example of pass by value. It is an example of pass by reference.
    It is defined using “METHODS”. It is defined using “CLASS-METHODS”.
    Involves declaration of reference variable, instantiating the class and then calling the class. Can be directly accesses by the class name.
    It can access all attributes of a class. It can access only static attribute of a class.
    Can be redefined in sub classes. Cannot be redefined in sub classes.

    Instance method over Static method

    It sounds simpler to use Static methods as it does not involve long steps such as the declaration of a reference variable, instantiating the object and then calling the method. Static methods can be directly accessed by the class name.

    But the question arises is that why instance methods are preferred over Static methods.

    One of the important concepts of OOABAP is Polymorphism i.e. replacing the default implementation of the method with a specific implementation. Polymorphism is achieved by Method Redefinition. Since Static methods are operable on their own and are independent of the instances of the class, they cannot be overridden. Whereas Instance methods can be inherited to sub classes, they can be redefined with the specific implementation.

  • Difference between SELECT-OPTIONS and PARAMETERS

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Before discussing the differences between SELECT-OPTIONS and PARAMETERS, let’s have a quick introduction of the two.

    Introduction

    SELECT-OPTIONS and PARAMETERS are components of the selection screen. SELECT-OPTIONS are assigned a selection table in the ABAP program as well as two input fields along with a push button for multiple selection.  Parameters are assigned a global elementary data object in the ABAP program as well as an input field. The name of the SELECT-OPTIONS and PARAMETERS is limited to a maximum of 8 characters whereas the length of the input field is restricted to a maximum of 255 characters.

    SELECT-OPTIONS and PARAMETERS statements are allowed in the global declaration part of executable programs, function groups, and module pools. In function groups and module pools, these statements are only allowed within the definition of a standalone selection screen. In executable programs, these statements are automatically assigned to the standard selection screen.

    Now, let’s have a look at their difference.

    Difference between SELECT-OPTIONS and PARAMETERS in SAP ABAP

    SELECT-OPTIONS PARAMETERS
    It stores value in an Internal table with a predefined structure. It stores value in a variable.
    It can accept multiple single values, multiple ranges, exclusion of values and ranges. It accepts only a single value.
    It creates a Selection table having 4 columns SIGN, OPTION, LOW and HIGH. It does not create a selection table
    If left blank, then all the data is selected from the database. If left blank, then no data is selected from the database.
    For value checking, IN operator is used in WHERE condition. For value checking, operators like =, >, <, <> are used in WHERE condition.
    Value check cannot be forced through Foreign key or domain values. Value check can be forced through Foreign key or domain values.
    Radio buttons or checkbox cannot be created using it. Radio buttons and checkbox can be created using it.

     

  • Difference Between Abstract Class and Interface

    Preface – This post is part of the Differences in ABAP for Interviews series.

    Before jumping to the Difference between Abstract Class and Interface, let us have a short introduction of the two.

    Introduction

    Classes, their instances/objects, and access to objects using reference variables forms the basics of ABAP Objects.  A class represents a set of properties or methods that are common to all the objects of one type. However, it is necessary for similar classes to have similar functions that behave differently in each class and having a uniform point of contact.

    ABAP objects make it possible by using Interface and abstract class.

    Like Classes, interface acts as a data type for the objects. The components of interface are the same as of classes. Interface is used when similar classes have same method with the same name but different functionalities. Interface along with inheritance provides a base for polymorphism because methods of the interface can behave differently in different classes.

    Abstract class is a class that contains one or more abstract methods i.e. methods without implementation. This class cannot be instantiated, as the abstract methods have to be implemented in a subclass of the inheritance tree. It can inherit only one class and multiple interfaces. You can explore more about Abstract class in SAP ABAP here.

    Now, let us have a look at their difference.

    Difference between Abstract Class and Interface

    Interface Abstract Class
    It is not a class. It is an independent structure that is used in a class to extend the functionality of a class. It is a special class which can’t be instantiated.
    It is an entity that doesn’t have an implementation. It can contain methods with implementation as well as without implementation.
    No common behaviour is implemented via Interface. For non-abstract methods, common behaviour can be implemented.
    It can inherit multiple interfaces but cannot inherit a class. It can inherit a class and multiple interfaces.
    Multiple inheritance can be achieved. There is only one abstract class as Super Class.
    For new methods, all the implementing classes must implement the new methods. For new non-abstract methods, there is no need to redefine the method.
    All components are PUBLIC by default. The visibility of each component can be set.

     

  • SAP ABAP Unit Testing Class

    Preface – This post is part of the Object Oriented ABAP series.

    Introduction

    ABAP unit testing is a methodology for software development. Whenever we create a class, it is mandatory to create its test class to check the behavior of our code if it is working exactly as it was expected.  While developing any object, it helps the developer to cover all possible scenarios. With ABAP unit test class, we can also check the coverage of code lines.

    Definition

    ABAP unit test class provides a methodology to test the individual source of code to determine if they are fit to use.

    The global class CL_UNIT_ASSERT contains a method that can be used for testing, the following are the most common methods:

    1. ASSERT_EQUALS – Checks the equality of two data objects.
    2. ASSERT_DIFFERS – Checks for the difference between two data objects.
    3. ASSERT_BOUND – Checks for the validity of the reference of a reference variable.
    4. ASSERT_INITIAL – Checks whether the reference of a reference variable is invalid.
    5. ASSERT_NOT_INITIAL – Checks whether the data object is not having its initial value.
    6. ASSERT_SUBRC – Checks for the specific value of SY-SUBRC.

    Program

    Below is a method ‘GET_ORDER_DATA’ of class ‘ZCL_PROD_ORD_TEST ‘  to fetch the operation details from the production order.

    Note: Local Test Class will get generated automatically through test class generation wizard.

    Method get_order_data

    Figure 1: Method get_order_data

    OSQL test double Framework : To remove the dependency from database tables.

    Unit Test Class

    Figure 2 : Unit Test Class

    Mocking virtual database tables to test the behavior of code.

    Unit Testing of Method GET_ORDER_DATA

    Figure 3: Unit Testing of Method GET_ORDER_DATA

    Advantages

    1. ABAP Unit provides excellent support for test-driven development in ABAP.
    2. You can run tests quickly and easily right from the development environment.
    3. You can navigate back and forth between a test class and the production code.
    4. With ABAP Unit test class, there is no such dependency on availability on test data to test the functionality.
    5. Fewer bugs reported with ABAP unit testing and less time wasted.