Defining a local class

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Define a local class inside a global class

Define a Local Class

Local And Global Classes

Classes in ABAP can be either local or global.

Global classes are stored centrally and are contained in their own repository object called a class pool. A global class can serve as main program. Global classes can also contain logic to be reused by other ABAP programs, including other global classes.

Local classes are defined as part of an ABAP program, for example a global class. You can use them only in the program or class in which they are defined. Local classes are useful for entities or functions that you only need in a single program.

The ABAP syntax of both local and global classes is almost identical. In this course, you will be working with local classes in your global class. The global class with method if_oo_adt_classrun~main will only serve as a kind of main program.

Local Classes in Global Classes

As shown in the figure below, when you open a global class in ADT, the focus is on tab Global Class, first. Here you find the source code of the global class itself. To see or enter the source code of local classes, you have to navigate to tab Local Types.

ADT provides a source code template for local class. To use this template proceed as follows:

  1. In the ABAP editor, type lcl and press Ctrl + Space.
  2. From the list that displays choose lcl - Local class and press Enter.
  3. Adjust the name of the new local class.

For the classes in this course you have to remove the create private addition from the CLASS … DEFINITION statement.

How To Create A Local Class In A Global Class

Declare Attributes

Source Code of a Class in ABAP

In ABAP. the source code of a class has two parts - the definition and the implementation. The definition part of a class is subdivided into up to three sections, called the visibility sections of the class.

Definition Part of a Class

The figure shows the different kinds of components you can define in a class. You can define any of these components in any visibility section of the class.

Example: Attributes of the Flight Connection Class

Let us start implementing our UML model in ABAP, and take the attributes of the connection class to begin with. To declare an attribute, use the DATA statement within the appropriate visibility section.

The attribute conn_counter is underlined, which denotes a static attribute. You declare static attributes using the CLASS-DATA statement. The syntax of CLASS-DATA is identical to that of DATA.

Note
We begin with public attributes at this point. Later in the course, when we discuss encapsulation, we will turn them into private attributes.

Define a Local Class

Steps

  1. Create a new ABAP class called ZCL_##_LOCAL_CLASS, where ## is your group number. Ensure that the class implements the interface IF_OO_ADT_CLASSRUN.

    1. Choose FileNewABAP Class.

    2. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_LOCAL_CLASS, where ## is your group number. Enter a description.

    3. In the Interfaces group box, choose Add.

    4. Enter IF_OO_ADT_CLASSRUN. When the interface appears in the hit list, double-click it to add it to the class definition.

    5. Choose Next.

    6. Select your transport request and choose Finish.

  2. Create a new local class lcl_connection inside the global class. Use code completion to generate the code.

    1. Switch to the Local Types tab.

    2. Type lcl into the editor and press Ctrl + Space.

    3. Double-click lcl - class in the pop-up.

    4. While lcl is still highlighted in the line class lcl definition create private., complete the name of the class to lcl_connection. Then delete the words create private.

  3. In local class lcl_connection, declare the following public attributes:

    Attributes

    Attribute NameScopeData Type
    carrier_idinstance/DMO/CARRIER_ID
    connection_idinstance/DMO/CONNECTION_ID
    conn_counterstaticI
    1. After line PUBLIC SECTION. and before line PROTECTED SECTION., add the following three statements:

      Code snippet
      
      DATA carrier_id    TYPE /dmo/carrier_id.
      DATA connection_id TYPE /dmo/connection_id.
      
      CLASS-DATA conn_counter TYPE i.
      
      Copy code
  4. Activate the class.

    Note
    Because the if_oo_adt_classrun~main( ) method does not contain executable code, yet, there is nothing to test or debug at this step.
    1. Press Ctrl + F3 to activate the class.

Log in to track your progress & complete quizzes