Blog

  • Exception Class in SAP ABAP

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

    Introduction

    To understand an exception class, we need to understand what is an exception and when it is raised.

    When we execute any program, some discrepancies can occur due to which a normal flow of execution gets interrupted and the behavior will not be the same as it was expected. That discrepancy is like an event which is occurring due to some reason, which is known as an ‘Exception’.

    It is required to handle such discrepancies in order to prevent the execution flow of any program. In order to achieve that, SAP ABAP has a special class known as an ‘Exception Class’.

    Exception Classes can be defined in a global class as well as in a local class. There are three exception classes exists, which are the subclasses of one root class ‘CX_ROOT’. Those three exception classes are:

    1. CX_STATIC_CHECK – which is being checked by both compiler and runtime system.
    2. CX_DYNAMIC_CHECK – Checked only at runtime system. All System exceptions will come under this exception.
    3. CX_NO_CHECK – This type will be chosen if exceptions are frequent.

    Definition

    An Exception class is used to handle the discrepancies(exception) occurs during the execution a of program.

    Whenever an exception occurs, an object is created (known as an exception object). The attributes of this object would contain information about the error. This exception can be caught by using TRY….CATCH…..ENDTRY block.

    TRY….ENDTRY block contains a protected area, where an exception can be caught using the CATCH statement. One TRY….ENDTRY block can catch more than one exception, but it should get caught in a sequence in which it occurs.

    There are two types of exception handling cases:

    1. One where the exception was raised, deleted before or after handling it.
    2. The other, where an exception which was raised is retained and the program is resumed after the statement that raised an exception.

    Program

    Let’s take an example of a simple arithmetic operation of two numbers. An exception should be raised if one of the two operands is missing.

    NOTE: A local class is used in below sample program.

    REPORT ztest_exception.

    CLASS lcx_missing_operand DEFINITION

    INHERITING FROM CX_STATIC_CHECK.

    ENDCLASS.                                                 “lcx_missing_operand  DEFINITION

     

    CLASS lcl_test_exception DEFINITION.

    PUBLIC SECTION.

    METHODS: add_numbers IMPORTING iv_num1 TYPE i OPTIONAL

    iv_num2 TYPE i OPTIONAL

    RETURNING VALUE(rv_calc) TYPE i

    RAISING   lcx_missing_operand

    PRIVATE SECTION.

    METHODS: do_check RAISING  lcx_missing_operand.

    DATA: lv_num1 TYPE i,

    lv_num2 TYPE i,

    lv_result TYPE i.

    ENDCLASS.                                                      “lcl_test_exception DEFINITION

     

    CLASS lcl_test_exception IMPLEMENTATION.

    METHOD add_numbers.

    v_num1 = iv_num1.

    v_num2 = iv_num2.

    me->do_check( ).

    rv_calc = v_result.

    ENDMETHOD.

     

    METHOD do_check.

    IF v_num1 IS INITIAL.

    RAISE EXCEPTION TYPE lcx_missing_operand.

    ENDIF.

    ENDMETHOD.

     

    ENDCLASS.                                                      “lcl_test_exception IMPLEMENTATION

     

    START-OF-SELECTION.

    DATA: lo_obj TYPE REF TO lcl_test_exception.

    DATA: lo_exc_root TYPE REF TO CX_ROOT.

    DATA: lv_msg TYPE STRING.

    CREATE OBJECT lo_obj.

    TRY .

    lo_obj->add_numbers( iv_num1 = 10 ).

    CATCH lcx_missing_operand.

    WRITE: /  ‘Operand is missing’.

    CATCH CX_ROOT INTO lo_exc_root.

    lv_msg = lo_exc_root->get_text( ).

    WRITE: / lv_msg.

    ENDTRY.

    Advantages

    1. With exception classes, you can track the sequence of called methods. Since in exception class, we have try-end-try block, so we can put the called methods in it, to know if one of them get failed.
    2. An exception class has an advantage that non-OO programmers won’t understand your code, increasing your job security a little.
  • Local Classes in SAP ABAP

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

    Introduction

    One of the flavors of ABAP classes (Global Classes) we have already discussed in the last article. Another flavor of ABAP class are Local Classes. The only difference between a local class & global class is visibility & accessibility. A local class can be accessed within that program only in which it is being defined & implemented, whereas a global class can be accessed in any other program.

    NOTE: In some of the previous articles, where different types of classes have been described, there the sample programs were the examples of local class only.

    Definition

    A class which is defined and implemented in a program is known as Local Class. Local classes in SAP ABAP can be created through Transaction code SE-38(tool to create a report).

    Program

    Let’s take an example of this class ‘ZCL_MARA’. This class is having one method ‘GET_MATERIAL_TYPE’  to get the material type of a given material.

    NOTE: ‘MARA’ is a table in SAP ABAP for general material data. So, in this sample program, material (iv_matnr – importing parameter of a method) is given by the user, and this method will fetch the material type ( like finished, semi-finished, raw, etc. )

    TYPES: BEGIN OF ty_mara,                                       “USER-DEFINED TYPES

    Matnr TYPE mara-matnr,

    Mtart TYPE mara-mtart,

    END OF ty_mara.

    CLASS zcl_mara DEFINITION.

    PUBLIC SECTION.

    METHODS: get_material_type

    IMPORTING iv_matnr TYPE mara-matnr

    EXPORTING ls_mara type ty_mara.

    ENDCLASS.                                                                    “CLASS DEFINITION

     

    CLASS zcl_mara IMPLEMENTATION.

    METHOD get_material_type.

    SELECT SINGLE matnr, mtart FROM mara INTO ls_mara

    WHERE matnr = iv_matnr

    ENDMETHOD.

    ENDCLASS.                                                                   “CLASS IMPLEMENTATION

     

    Advantages

    1. To restrict the usage of any functionality, we can use it in a local class of any program.
    2. A Local class is very useful while creating an ABAP Unit Test Class, as a local test class has been generated within a global class.
    3. These classes are useful for the enhancements of class methods.
  • What is SAP MM

    Preface – This post is part of the SAP MM series.

    Introduction :

    SAP MM is one of the modules of the SAP ERP system which deals with the logistics (Material management and inventory management) of the materials. It includes modules such as Sales and Distribution, Production Planning, Plant Maintenance, Project Systems and Warehouse Management. This module contains master data, system configuration and transactions to complete the procure to pay process.

    This process ensures that there is never a shortage of materials in the supply chain process of an organization It deals with the management of materials (products and/or services) and resources of an organization to make the business run smoothly on time and in a cost-effective manner.

    Material Management process :

    What is SAP MM
    What is SAP MM – Image Illustration

    Requirement Determination:

    Planning of what materials are required, when they are required, budget of the enterprise for the material.

    Source Determination :

    Determining potential suppliers of the requested material.

    Vendor Selection :

    On identification of potential vendor, request for quotation(RFQ) is sent by the requester. The contract is awarded to the vendor based on selection criteria.

    Order Processing :

    A formal purchase order is created and sent to the vendor.

    Order Monitoring: 

    Order monitoring is tracking of dates and the goods from the time of ordering to the time the goods are delivered. It is done to ensure that there are no discrepancies and if any, they can be handled beforehand.

     

    Goods Receipt :

    On receiving the material from the supplier, the purchase department prepares the good receipt which can be used later for reconciling if there is any discrepancy .

     

    Invoice Verification :

    The good receipt is compared to purchase order to avoid any mismatch. Materials are checked whether they meet the ordered specification, and then priced according to the terms of the purchase order. For any damaged goods, the buyer contacts the supplier either for replacement or for a refund.

    Payment Processing :

    Once the verification is done, the payment invoice is created and vendor is paid.

    SAP MM integration with other modules

    Integration of MM and FI :

    • On posting of goods receipt, a debit is posted to inventory account and a credit is posted to GR/IR clearing account.
    • On posting of invoice receipt, a debit is posted to GR/IR clearing account and a credit is posted to vendor account.
    • On payment of vendor, a debit is posted to vendor account and a credit is posted to bank clearing account.

    Integration of MM and SD:

    • When sales order is created in SD, the details of the materials are copied from Material Master of MM.
    • Data like availability and MRP is also copied from MM.
    • On creation of inbound or outbound delivery with reference to a sales order, determination of shipping point with the help of plant, loading group etc. which refers to Material Master.

    Integration of MM and PP :

    • On creation of Production order in PP, BOM (Bill of materials) is determined and PR is raised for materials through MM.
    • Stock determination helps to implement various strategies to withdraw materials for goods issue and stock transfer based on material requirement.

    Advantages of MM :

    • Integration with other modules helps to reduce data entry and human error.
    • Ensures there is never a shortage of material.
    • Avoids stocking of unnecessary or obsolete materials.
    • Least inventory loss.
    • Controlled manufacturing cycle time.
    • Reduces expenditure on unnecessary material storage.
    • Different release strategies for different purchasing document based on various criteria like vendors, payment terms etc.
    • Automatically creates Purchase order for specific vendors based on stock.

    Challenges in implementation :

    • Information such as material requirement, planning activities and order input should be managed and reflected carefully.
    • Material planning and demand forecast should be in line with the manufacturing requirement.
    • Standard product quality should be maintained.
    • Vendor must be selected carefully to meet quality management principles to safeguard quality of end-product.

    SAP MM sub-modules:

    • Master Data
    • Purchasing
    • Inventory management
    • Pricing Procedure
  • Super Class in SAP ABAP

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

    Introduction

    One of the three pillars of OOABAP uses this concept of Super class to achieve ‘Inheritance’. Super class allows us to extend that class by creating a subclass of it. This subclass will have all the properties of a super class, plus it can have additional properties as well.

    Definition

    A class which is having a derived class from it, is known as a ‘Super Class’.

    To make a class Super Class, the first step is to uncheck the box indicating Final Class while creating a Class through T-Code SE24 (Class Builder). Because if a class is creating as a Final cannot be extended further, and inheritance cannot be achieved in that case.

    If the developer is using this concept of the super class & subclass, then he/she can have all the advantages of ‘Inheritance’.

    Program of Super Class in SAP ABAP:

    Let’s take an example of this employer class ‘ZCL_EMPLOYER’. This employer class is having one method ‘GET_INCOME’ to get income.

     

    CLASS zcl_employer DEFINITION.

    PUBLIC SECTION.

    METHODS: get_income RETURNING VALUE(income) TYPE F.

    PRIVATE SECTION.

    DATA: income TYPE F VALUE 100.

    ENDCLASS.                                                                    “CLASS DEFINITION

    CLASS zcl_employer IMPLEMENTATION.

    METHOD get_income.

    income = me->income * 80.

    ENDMETHOD.

    ENDCLASS.                                                                   “CLASS IMPLEMENTATION

     

    Now, I want to create a subclass of it, where I want to redefine this method ‘GET_INCOME’.

     

    CLASS zcl_manager DEFINITION INHERITING FROM zcl_employer.

    PUBLIC SECTION.

    METHODS: get_income REDEFINITION.

    PRIVATE SECTION.

    DATA: income TYPE F VALUE 100.

    ENDCLASS.                                                                    “CLASS DEFINITION

    CLASS zcl_manager IMPLEMENTATION.

    METHOD get_income.

    income = me->income * 100.

    ENDMETHOD.

    ENDCLASS.                                                                   “CLASS IMPLEMENTATION

     

    Once you derived a class from another class, the new class will be known as ‘SUBCLASS’ or ‘CHILD CLASS’. And the class from which a new class is deriving will be known as ‘SUPER CLASS’ or ‘PARENT CLASS’.

    As discussed earlier, the subclass can have all the properties, & methods of a super class. The developer can redefine the existing methods of the super class and he/she can also add some new functionality into it.

    Advantages:

    Provides reusability of code functionality & fast implementation time. It can have all the advantages of ‘Inheritance’.

  • Global Class in SAP ABAP

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

    Introduction

    From the reference of an article: SAP ABAP Classes we have discussed the flavors of ABAP Classes & we came to know that ABAP classes are available in two flavors, one of which is Global Class.

    The visibility section of the global class is always public, meaning all the ABAP programs in an R/3 system can access the global class. However, instantiation of that class could be set as either public, protected or private.

    Definition

    A class which is defined in the class builder (Transaction Code – SE24) is known as Global Class.

    While creating a class from Class Builder (T-code: SE24), we get many options that indicate which type of global class we want to create, for example, usual ABAP class, exception class, persistent class or a test class. It also gives an option of whether we want to make it final or not.

    After selecting the type of class and properties ( description & instantiation ), a screen will come where we can see the components of a global class like:

    1. Interfaces – Independent structures which are used in a class to extend the functionality of a class.
    2. Friends – If there is any class, which you want to make a friend of your class, you can define it here.
    3. Attributes – variables or constant declared within the class and can be accessed by all the methods of that class.
    4. Methods – Determines the behavior of an object, it provides some functionality. A method is allowed to access all the attributes of their class.
    5. Events – A mechanism through which one method can raise method of another class.
    6. Types – Table Types can be defined here, that can further be used anywhere in the whole class.
    7. Alias – A concept of providing an alternate method name for an interface method in an implemented class.

     

    While defining an attribute and method, there are two mandatory fields, which we need to provide:

    1. Level: We have 2 level options:
    • Instance – Components declared as instance can only be addressed by the instance(object) of that class only.
    • Static – Components declared as static can be used independently of a class instance.
    1. Visibility: We have 3 Visibility options:
    • Public – Components with this visibility will be available to all.
    • Protected – Components will be visible to that class, where they are defined and to the inherited class.
    • Private – Components will be visible to that class only, where they are defined.

     

    Advantages:

    Provides reusability of code functionality & fast implementation time.

  • Singleton Class in ABAP

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

    Introduction

    Sometimes there is a need to instantiate an object at a time, that means only one instance is required at one point of time. This is a very common requirement while designing an application. To achieve such a scenario, there is one concept of ‘Singleton Pattern’ or ‘Singleton Class’ in OOABAP.

    This was the theoretical part. Now, a question comes, that how to achieve it in real? So, here the answer is: By declaring the constructor as a ‘Private’ one.  This private constructor can be created after changing the instantiation type to private. This helps us to restrict the further creation of an object of a class.

    Definition of Singleton Class in ABAP

    A class is said to be a singleton class if it can have the utmost one instance only.

    Following is a procedure to create a singleton class :

    1. Visibility of class should be private.
    2. Declare a private attribute with reference to the same class, in which it is being declared.
    3. Create a public static method with returning parameter, having a reference of the same class.
    4. Create an implementation and an object inside the implementation of a public static method.
    5. To create an instance for the singleton class, call this public static method in any program.

    Program:

    For the better understanding of this Singleton Concept, let’s consider this below sample program and here as you can see that object creation of a singleton class is not possible outside the class. So, this is how a singleton pattern works.

    Singleton Class in ABAP
    Singleton Class in ABAP – Image Illustration

    Program Explanation:

    In the above program, we have implemented a local class lcl_singleton_test inside an ABAP Report. In the class definition, we have kept the class as private and have declared a Public section. Under the public section, we have  declared a static attribute lo_singleton which refers to the program itself. Also, we have defined a local variable v of type integer. Then, we have defined the class constructor and a method set_name for the class.

    Later, we have implemented the class where we are creating an object under class constructor. Also, we have assigned a value i.e. “GEETANSHU” to the local variable v.
    Now, in the report code, we are doing a static call to our class via its object r1.

    Advantages of Singleton Class in ABAP:

    1. With the use of Singleton class, testability improves.
    2. Singleton pattern supports the main moto of Object Oriented Design principle by allowing it to open for enhancements and closed for modifications.
    3. The advantage of using a private constructor in Singleton class (as no one create an object of your class).
  • Abstract Class in SAP ABAP

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

    Introduction

    In simpler words, a class can be called as an abstract class, if it would contain at least one abstract method. Now, the question comes, what is an abstract method? An abstract method is a method which does not have an implementation. Now, a question will come to your mind, if a method is not having any implementation, then what is the use of this abstract method or class.

    So, the answer to your question is: We can implement that abstract method, but not like any other simple class. There is a particular way to implement it.

    To implement an abstract method, a subclass of an abstract class is required. There is no need to instantiate an object of an abstract class. Instantiation is possible only for the subclass. An abstract class can have non-abstract methods as well and It is not necessary to redefine non-abstract methods in each and every inherited class.

    Following points, we need to remember while creating an abstract method:

    1. Abstract methods can never be private.
    2. Only instance methods are allowed to be an abstract method.

    Definition:

    Class with at least one abstract method (which does have implementation) is known as ‘Abstract class’.

    Example: Let’s take a simple real time scenario, where ‘TELEPHONE’ is your class. This class is having four Methods (each method depicts one functionality):

    1. PICKUP_CALL
    2. DROP_CALL
    3. REJECT_CALL
    4. DIAL_NUMBER

    For the first three methods, the functionality would be the same (meaning, you just need to click on one button). But in the fourth method, there could be many possibilities of dialing a number, say in case of an emergency, the dial number would be of three digits. Similarly, there will be different cases like dialing a landline number or a mobile number or some service number or some toll-free number etc.

    So, here ‘DIAL_NUMBER’ method will be declared as an abstract method. Here, the functionality of this method is same, that is dialing a number. But the implementation could be different in different scenarios.

    Program:

    CLASS ZCL_TELEPHONE DEFINITION ABSTRACT.                                            “Abstract Class”

    PUBLIC SECTION.

    METHODS: DIAL_NUMBER ABSTRACT.                                                       “Abstract Method”

    METHODS: PICKUP_CALL.

    METHODS: DROP_CALL.                                                                                “Non-Abstract Methods”

    METHODS: REJECT_CALL

    ENDCLASS.

     

    CLASS ZCL_SUBCLASS_TELEPHONE DEFINITION INHERITING FROM ZCL_TELEPHONE.        ‘
    PUBLIC SECTION.
    METHODS DIAL_NUMBER REDEFINITION.
    ENDCLASS.

    CLASS ZCL_SUBCLASS_TELEPHONE IMPLEMENTATION.
    METHOD DIAL_NUMBER.

    ENDMETHOD.
    ENDCLASS.

    Advantages:

    1. Provide flexibility to add default operations with multiple flavors.
    2. Abstract classes allow us to partially implement the class.
    3. We can achieve Polymorphism by using an abstract class.
    4. We can achieve Dynamic Binding also. The object of an abstract class can be used as a reference, which further could be replaced by the concrete object at runtime, this is known as dynamic binding.
  • Final Class in SAP ABAP

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

    Introduction

    A class is said to be a ‘Final Class’ when it is no longer available for inheritance. Therefore, we won’t be able to inherit the properties or behavior of a final class. A final class does not have a subclass.

    However, if you don’t want to make a whole class final, and if you want to restrict some of the methods from getting redefined, then there is an option available to declare some of the methods of a class as ‘final’.

    Definition:

    If a class cannot be inherited further, then it is a Final Class. It is up to the developer if he wants to make a class ‘final’ or some of the methods ‘final’.

    Example: Let’s take an example of this ellipse. Say, ‘ELLIPSE’ is your class. A circle inside this class is your subclass.

    This is the first case, where the class ‘ELLIPSE’ is not declared as ‘final’.  Therefore, a subclass ‘CIRCLE’ inherits all the components of the superclass, where the visibility section is public & protected.

    Note: If there is an interface implemented in the superclass, it will get reflected in the subclass as well.

    Final Class in SAP ABAP
    Final Class in SAP ABAP

    Now, if class ‘ELLIPSE’ would be defined as ‘Final’, then this inheritance would not be possible.

    Program:

    Below is the sample program:

    1. CLASS_1 declared as ‘FINAL’ and hence giving error while inheriting CLASS_2 from CLASS_1.
      ABAP Final Class Example 1
      ABAP Final Class Example 1
    2. In second example, one of the methods of a class is defined as ‘Final’. The subclass is not having an implementation of final method.
      ABAP Final Class Example 2
      ABAP Final Class Example 2

    Advantages:

    Inheritance is one of the powerful pillars of OOABAP but not every time, this may lead to weakens encapsulation, can create a bug hazard. To prevent this, sometimes a class is declared as ‘Final’.

    Disclaimer:  All the images here are taken from Google, and the Admin has no possession over it.

  • Friend Class in SAP ABAP

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

    Introduction

    In the previous article, we have discussed the classes, its components and the visibility of components. As per the visibility section, only public methods or attributes of a class are allowed to be used by some other classes. Now, what if we want to access non-public (protected or private) components (methods or attributes) of a class, to solve this problem, OOABAP has a concept of ‘Friend Class’.

    ‘Friend’ in a real life is a person, with whom you can share your secrets. Just like that, in OOABAP, if a class is declared to be a friend of some other class, then it can access the non- public components of that class.

    Definition:

    The class to which friendship is granted can access all the components (irrespective of components visibility) of the friendship granting class.

    This Friendship is unilateral though, that means the friendship granted class cannot access the private and protected components of the class, to which friendship is granted.

    Example:

    Suppose, there is one global class ‘CL_DEMO_TEST_FRND’ class, this class is having 2 private methods. Now, I want to create one test class (ABAP Unit Test Class) for this global class say, LTC_DEMO_TEST_FRND, the task of this class is to check the code coverage of a global class, that means this test class should have access of all the components of a global class. We will make this local test class ‘LTC_DEMO_TEST_FRND’ a local friend of global class ‘CL_DEMO_TEST_FRND’ so that its private methods can be tested.

    Syntax: 

    CLASS LTC_DEMO_TEST_FRND DEFINITION DEFERRED.
    CLASS
    CL_DEMO_TEST_FRND DEFINITION LOCAL FRIENDS LTC_DEMO_TEST_FRND.

    NOTE: In the above example, keyword ‘Definition Deferred’ is used, it makes the class ‘LTC_DEMO_TEST_FRND’ known without having a definition.

    Program:

    Scenario: There are 2 classes, class ‘DEPARTMENT’  having private attribute ‘NAME’. The other class ‘EMPLOYEE’ having one method ‘SET_NAME’. Class ‘EMPLOYEE’ needs the private attribute ‘NAME’ to set the employee name.

    In the first program, class ‘EMPLOYEE’ is trying to access the private attributes of class ‘DEPARTMENT’, without declaring ‘EMPLOYEE’ as a friend of ‘DEPARTMENT’, and hence it is giving an error.

    ABAP Friend Class Example 1
    ABAP Friend Class Example 1

    After declaring ‘EMPLOYYE’ friend of ‘DEPARTMENT’. Now, ‘EMPLOYEE’ class can access the private attributes of class ‘DEPARTMENT’.

    ABAP Friend Class Example 2
    ABAP Friend Class Example 2

    Advantages of Friend Class in SAP ABAP:

    1. Allows sharing non-public information of a class by a non-member function.
    2. The sub-classes and interfaces of a class (to which friendship is granted), can also access the non-public components of a class (friendship granting class).
    3. With the help of friend class, we can use the additional functionality, kept outside the class.
    4. We can use the data which is not being used by that class.

    Disclaimer:  All the images here are taken from Google, and the Admin has no possession over it.

  • SAP ABAP Classes

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

    Introduction

    In the last article, we have learnt about the concepts and advantages of OOABAP through which we came to know that it mainly revolves around the classes & its object. The last article gives us the overview only, now it’s time to take a deep dive into the major concepts of OOABAP. Let’s start with the SAP ABAP Classes.

    Classes in OOABAP defines the object it is having. A class can have infinite number of objects, with a unique combination of state and behavior. Classes contain properties, methods and other things that allow developers to define what the class represents. But a class alone will be of no use, unless it is having some objects, that is why an object is known as the instance of a class. We instantiate an object that points to classes.

    Example:  

    SAP ABAP Classes
    SAP ABAP Classes

     

    The above figure depicts Car as a Class and car models as the Objects of a class ‘CAR’.

    Flavors of SAP ABAP Classes:

    ABAP Classes can be declared as either globally or locally. Following are the flavors in which ABAP classes are available:

    1. Local Class:

      Local class can only be defined within a program, and accessibility of this class is restricted to that program (within which it is defined) only. Defining a local class consist of two things:

    a. Class Declaration: This is being done inside a coding block of CLASS DEFINITION. It contains the declaration of all the components (attributes, methods, events) of the class.

    Syntax:    CLASS ABC DEFINITION.

    Coding Block

    ENDCLASS.

    Note: 

    1. ABC is a name of class.

    2. This Class definition should always be at the beginning of the program because the declaration inside this block belongs to global program data.

    b. Class Implementation: This part contains the implementation of all the methods declared in CLASS DEFINITION. The CLASS IMPLEMENTATION block of local class is a processing block (meaning consist code of functionality).

    Syntax:    CLASS ABC IMPLEMENTATION.

    Coding Block

    ENDCLASS.

    Note: If you are declaring methods in CLASS DEFINITION, then the CLASS IMPLEMENTATION of that method is must.

    1. Global Class:

      To create a global class, T-code is SE24 (Class Builder) in ABAP workbench. The name itself is suggesting the accessibility of global class, all ABAP programs can use these global classes by instantiating the object of that class.

    Structure of Class:

    A class structure comprises of 3 things:

    • Class Components.
    • Visibility section of Components.
    • Implemented methods of class.

     

    1. Components of Class:

      It defines the attribute of the objects in a class. While defining a class, components are declared in the declaration part of the class with one of the three visibility section (Public, Private, Protected). The components of classes are available in two types:

    • Instance Component – These components exist separately for each object in a class.
    • Static Component – Exist only once for whole class.

     

    Following are the components of the class (with each component type):

    a. Attribute – variables or constant declared within the class and can be access by all the methods of that class.

    • Instance Attribute – Attributes that would be instance specific for an object. It can be declared in local class by using DATA
    • Static Attribute – Defines the state of class. It can be declared in local class by using CLASS-DATA

    b. Method – Determines the behavior of an object, it provide some functionality.

    • Instance Method –  It can be declared in local class by using METHODS Accessible by all the attributes of a class.
    • Static Attribute – It can be declared in local class by using CLASS-METHODS They can access static attributes only.

    c. Events – A mechanism through which one method can raise method of another class.

    • Instance Event –  It can be declared in local class by using EVENTS This can only be triggered in an instance method.
    • Static Event – It can be declared in local class by using CLASS- EVENTS All methods (static and instance) can trigger static events.

    Note:  Each component name should be unique within a class.

     

    1. Visibility Section:

      Components of a class have their own visibility section which defines where they can be accessible. There are total three types of visibility:
      a. Private – Components will be visible to that class only, where they are defined.
      b. Public – Components with this visibility will be available to all.
      c. Protected – Components will be visible to that class, where they are defined and to the inherited class.

     

     

    Types of ABAP Class:

    When you create a class through T-Code SE-24(Class Builder), it will ask you which type of class, you want to create. So, there are total five types of classes:

    1. Usual ABAP Class:

    As the name itself suggests, usual or a normal class which we create through SE24.

     

    1. Exception Class:

    A class to handle the exceptions occurred during runtime or program execution.

    Types of Exception classes: Total 3 exceptions classes exist, that are inherited from one super class ‘CX_ROOT’.

    SAP ABAP Exception Class
    SAP ABAP Exception Class

     

    While creating an exception class, a check box ‘WITH MESSAGE CLASS’ is there. If u select this, a message class then will be generated. This message class, we can separately generate from t-code SE91.

    1. Message Class:

      Suppose, one message is being used frequently in one program. One thing we can do is hardcoding, but that would be against coding standards.

    Instead of doing that, we can collect it somewhere (say in a container), and from there we can use it anywhere we want, even in different programs as well. So, a message class is like container only, which holds a bunch of different messages.

    1. Persistent Class:

      Before going for persistent class, let’s first discuss the meaning of ‘persistent’.  A data is said to be persistent if it can be preserved beyond the runtime of program.

    For a session, ABAP program stays in local ABAP memory till the runtime of that program. To store it permanently, we use persistence service (implemented by persistence class).

    1. Test Class (ABAP Unit):

    This class is mainly created for unit testing. Now unit testing of what? When we create any class, we usually check if the required functionality is achieved or not. Before going for any testing, we usually test our code by creating a unit test class.

    This unit test class consists some test methods corresponding to the methods of original class (for which testing is being done), and this executes the comparison between the expected and actual values.