Defining a Local Class

Objective

After completing this lesson, you will be able to Define a local class inside a global class.

Local Classes

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

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
The definition part of a class contains the definition and declaration of all of the elements in the class, that is, the types, the constants, the attributes, and the methods. It begins with CLASS <class_name> DEFINITION. and ends with ENDCLASS.
Implementation
The implementation part of a class contains the executable code of the class, namely the implementation of its methods. It begins with CLASS <class_name> IMPLEMENTATION. and ends with ENDCLASS. The implementation part of a class is optional. It becomes mandatory as soon as the class definition contains executable methods.
Visibility sections

Each visibility section of a class starts with one of the statements PUBLIC SECTION, PROTECTED SECTION, PRIVATE SECTION and ends implicitly when the next section begins. The last section ends with statement ENDCLASS. All declarations of a class have to be inside one of the sections. In other words: No declarations are allowed between the beginning of the class definition and the beginning of the first section.

The section in which a declaration is located defines the visibility of the declared element of the class.

The three visibility sections of a class are optional; if you do not need a particular section, you do not have to declare it. But if a class definition consists of more than one section, they must follow the order PUBLIC SECTION - PROTECTED SECTION - PRIVATE SECTION.

Definition Part of a Class

The following video 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

In this exercise, you define a local class inside of a global class.

Template:

  • none

Solution:

  • /LRN/CL_S4D400_CLS_LOCAL_CLASS (global Class)

Task 1: Create a Global Class

In your own package, create a new ABAP class.

Steps

  1. Create a new global class that implements interface IF_OO_ADT_CLASSRUN (suggested name: ZCL_##_LOCAL_CLASS, where ## stands for your group number).

    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.

Task 2: Define a Local Class

Define a local class lcl_connection and declare some public attributes.

Steps

  1. Inside the global class, create a new local class lcl_connection. Use code completion to generate the code, but make sure you remove the create private addition from the generated 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 there is still a frame visible around lcl in the line class lcl definition create private., complete the name of the class to lcl_connection.

    5. Delete the words create private at the end of the code row class lcl_connection definition..

  2. 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 code:

      Code Snippet
      Copy code
      Switch to dark mode
      123456
      DATA carrier_id TYPE /dmo/carrier_id. DATA connection_id TYPE /dmo/connection_id. CLASS-DATA conn_counter TYPE i.
  3. 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 point.
    1. Press Ctrl + F3 to activate the class.

    2. Compare your code on tab Local Types to the following extract from the model solution:

      Code Snippet
      Copy code
      Switch to dark mode
      1234567891011121314151617181920212223
      *"* use this source file for the definition and implementation of *"* local helper classes, interface definitions and type *"* declarations CLASS lcl_connection DEFINITION. PUBLIC SECTION. DATA carrier_id TYPE /dmo/carrier_id. DATA connection_id TYPE /dmo/connection_id. CLASS-DATA conn_counter TYPE i. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS lcl_connection IMPLEMENTATION. ENDCLASS.

Log in to track your progress & complete quizzes