Skip to Content
-1

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

Dec 15, 2017 at 06:53 PM

159

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Sandra Rossi Dec 16, 2017 at 05:28 PM
0

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.

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Honestly I never thought about creating an interface and making it a friend of the global model/view class and then implementing the interface in my local controller class. But in this case the interface should still need to be global, yes?

If yes, why take the trouble of creating an interface at all? I can simple make the controller as a global class and then make it a friend of the global model/view....

Unless I misunderstood your explanation, I am still trying to find a way to make a local class a friend of the global class.

Please correct me if I am wrong here.


Vikram.M

0

You cannot make a local class friend of a global class, except if (cf my point 1) the local class is inside this class pool.

1

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>

1
Sandra Rossi Dec 15, 2017 at 07:07 PM
1

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.
Show 4 Share
10 |10000 characters needed characters left characters exceeded
Former Member

I did the exact same thing but I ended up with the below error and hence the request for clarification/help? Is this because of my N/W version? I am on 7.02.

Below are my declarations and the error that I get while activation of the global class. I did not get any error during syntax check though.

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations

CLASS lcl_vikram_test3 DEFINITION DEFERRED.
CLASS zcl_vikram_test3 DEFINITION LOCAL FRIENDS lcl_vikram_test3.

Class ZCL_VIKRAM_TEST3,Class Include (Local Data Types, Object Types, Macros) IMP "CLASS LCL_VIKRAM_TEST3 DEFINITION DEFERRED" does not have "CLASS LCL_VIKRAM_TEST3 DEFINITION".


Am I missing something?

0

Your local class should follow. So you'd have the following code in the "local definitions/implementations":

class lcl_yourlocalclass definition deferred.
class zcl_yourglobalclass definition local friends lcl_yourlocalclass.

class lcl_yourlocalclass definition.
  public section.
    ...
endclass.
class lcl_yourlocalclass implementation.
  method ...

It works this way since at least 7.02.

0
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

0

Note that if you created your class pool by copying an old one, maybe you have the "old internal structure" of class pools, and thus you need to use the menu Utilities -> Convert class-local types

0
Horst Keller
Dec 17, 2017 at 04:09 PM
0

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.

Show 4 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Thanks much Sandra and Horst.

On the same note:

1. when one refers to class pool, are they referring to the part where we create definitions/implementations within "local definitions/implementations"?

2. What scenarios would one create some local classes within a global class in other words class pool - assuming my understanding of class pool is correct from point 1 ) apart from test classes?

3. Is it correct in terms of design to create a global class and a bunch of local classes within the global class so as to make these local classes friends of other global classes?

Thanks,

Vikram.M

0

Please post the additional questions separately. Otherwise this information will be buried inside the comments to an answer in another question and it'd be very hard for others to find.

Even though this is your thread it is essentially the case for p.13 in ROE.

Thank you.

0

"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.

0
Former Member

Just did. Thanks for pointing out about ROE.

https://answers.sap.com/questions/387122/class-pool.html

Vikram.M

0