When to use Local Class and when not to!

By | January 9, 2012 | ABAP Objects, OO Concepts | 30,775 | 0

In SAP ABAP, we can create Local Class as well as Global Classes in class builder. We can create them in the class builder as well. Lets see when it is OK to use Local Class and when we should refrain using it.

What is Local Class

Local classes are defined within an ABAP program. Local classes and interfaces can only be used in the program in which they are defined. When you use a class in an ABAP program, the system first searches for a local class with the specified name. If it does not find one, it then looks for a global class.

If the local class is defined, it is only visible to that particular program or global class. Thus we can design it outwardly visible – means we can all components PUBLIC because there is no other than the program would use them.

Local Class in a Program

We can define the local class in any program. This class would be visible and used in the same program.

Local Class in a Global Class

Local Class can also be defined in the Global Class. We can define the local class in the Local Types section of the Class Editor. This can be implemented in the Implementation section of the class editor.

Some Examples where local classes are used:

  • ABAP Unit: ABAP Unit test class should be a local class. This generally resides in the same productive code for which Unit Test class is required.
  • Enhancement to Class Methods: When we want to enhance the class methods i.e. Pre, Post or OvreWrite Exits, system would create Local Class to achieve this functionality.

Problems

There are some obvious problems for using Local Classes:

  • Violation of Loose-Coupling: Since Local class resides within the program or global class, they are only accessible within that context only. So, if we need to use the same class in any other program, we can’t. These local classes are not even visible to the inherited classes of the super class. This is pure violation of Loose Coupling.
  • Outwardly Visible Components: Because of the nature of the local classes, accessible only within that context, we may tend to design our classes “more open” and don’t much attention to encapsulation. We would end up creating all the components as PUBLIC even though they are not really required as Public. Using Global class, we can certainly pay more attention to Encapsulation and design our classes “Less Open”
  • .

Recommendation

  • Event Handlers: We may use it, While working on a stand alone program to receive some events, e.g. DOUBLE_CLICK from CL_GUI_ALV_GRID.
  • ABAP Unit: When we implement ABAP Unit for any subroutine, FM or Mehtod; it is required to have the Test method resides within the same program. This local Test class can be inherited from a global test class.
  • Enhancement to Class Methods: Since these are system generated local class methods, we don’t have any choice.

For all other usages, we should use Global Classes. Who knows, who will need to use that in future. If you have already created a local class in a program and want to migrate that to Global Class, you can use ABAP Editor Tool Convert Local Classes to Global Classes.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.