Skip to Content
avatar image
-1
Former Member

Making a Local class (within a program) friend of a global class

Hi,

I have read thru a number of SCN questions and answers and also went thru SAP help documentation to find how to make a local class a friend of the global class in order to access the private methods of the global class.

I did find one of @Horst Kellers answer to the very same idea but that worked for unit test classes.

Similar question

But in my case, the idea is to have a global model and view class but to create the controller as a local class within the program. In this case, how do I make the local controller friends with the global model/view classes?

From the link above, I understand the idea of maintaining the local class as deferred during definition and also the concept of using LOCAL FRIENDS but the local class was also defined and implemented within the main class which does not seem correct in my situation.

Can someone throw some light on the same?

Thanks,

Vikram.M

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Dec 16, 2017 at 05:28 PM

    Oh, I just understood your question. So, the only possibilities to make a local class friend of a global class (*) are:

    1. You can make a local class of a class pool be friend of the global class by defining the local class inside the same class pool.

    2. You can make a class pool be friend of another class pool.

    3. You can make an interface pool be friend of a class pool. This way, any local and global class implementing the interface is a friend of the class pool.

    .

    (*) The global class is the main class of a class pool, it has the same name as the class pool.

    Add comment
    10|10000 characters needed characters exceeded

    • For point 1, here is the whole code of the class pool, containing both the global class and the local class, and the method of the local class is using a private method of the global class:

      CLASS-POOL .
      *"* class pool for class ZCL_YOURGLOBALCLASS
      *"* local type definitions
      *include ZCL_YOURGLOBALCLASS===========ccdef.
      *"* use this source file for any type of declarations (class
      *"* definitions, interfaces or type declarations) you need for
      *"* components in the private section
      
      *"* class ZCL_YOURGLOBALCLASS definition
      *"* public declarations
      *  include ZCL_YOURGLOBALCLASS===========cu.
      CLASS ZCL_YOURGLOBALCLASS DEFINITION
        PUBLIC
        FINAL
        CREATE PUBLIC .
      PUBLIC SECTION.
      *"* protected declarations
      *  include ZCL_YOURGLOBALCLASS===========co.
      PROTECTED SECTION.
      *"* private declarations
      *  include ZCL_YOURGLOBALCLASS===========ci.
      PRIVATE SECTION.
        CLASS-METHODS PRIVATE_METHOD .
      ENDCLASS. "ZCL_YOURGLOBALCLASS definition
      *"* macro definitions
      *include ZCL_YOURGLOBALCLASS===========ccmac.
      *"* use this source file for any macro definitions you need
      *"* in the implementation part of the class
      *"* local class implementation
      *include ZCL_YOURGLOBALCLASS===========ccimp.
      *"* use this source file for the definition and implementation of
      *"* local helper classes, interface definitions and type
      *"* declarations
      CLASS LCL_YOURLOCALCLASS DEFINITION DEFERRED.
      CLASS ZCL_YOURGLOBALCLASS DEFINITION LOCAL FRIENDS LCL_YOURLOCALCLASS.
      CLASS LCL_YOURLOCALCLASS DEFINITION.
        PUBLIC SECTION.
          CLASS-METHODS TEST.
      ENDCLASS.
      CLASS LCL_YOURLOCALCLASS IMPLEMENTATION.
        METHOD TEST.
          CALL METHOD ZCL_YOURGLOBALCLASS=>PRIVATE_METHOD.
        ENDMETHOD.
      ENDCLASS.
      CLASS ZCL_YOURGLOBALCLASS IMPLEMENTATION.
      *"* method's implementations
      *  include methods.
      *INCLUDE ZCL_YOURGLOBALCLASS===========CM001.
      METHOD PRIVATE_METHOD.
      ENDMETHOD.
      ENDCLASS. "ZCL_YOURGLOBALCLASS implementation<br>

  • Dec 15, 2017 at 07:07 PM

    You found it, and you should have tried. It's just that in your case, it's not a test class, but that doesn't matter. To be done in the "local definitions/implementations".

    class lcl_yourlocalclass definition deferred.
    class zcl_yourglobalclass definition local friends lcl_yourlocalclass.
    
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Sandra Rossi

      Sandra,

      This is how Horst Keller has explained in his car reservation example as well. But in his example he was working with ABAP unit and it made sense to have the test class (local class) definition and implementation within the global class.

      But as i had mentioned in the original post, in my case the local class is not a test class but a controller which should be defined and instantiated in the main program. Once the controller is instantiated, I want be able to access the private methods of the model/view without having to give a public method. Hence the question of having the controller (local class) as a friend of the global model/view classes.

      Also, on a different note, if the local class is defined and implemented within the global class, can the local class be called and instantiated from a program?

      Thanks,

      Vikram.M

  • Dec 17, 2017 at 04:09 PM

    Sandra's answers are all correct. Local classes of a class pool can be friends of their global class. But local classes of any other program can't.

    Add comment
    10|10000 characters needed characters exceeded

    • "so as to make these local classes friends of other global classes?"

      As I said, this is not possible.

      A class pool is a special ABAP program that contains exactly one public class that is visible to other programs. All other types of a class pool are local as in any other program.

      Please understand the concept of programs in ABAP first.