Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Class visibility

Former Member
0 Likes
2,271

Hello,

Is in ABAP any possibility to declare classes in section "Class rel. local definitions" and declare public methods or public types with them? I want to create dozen classes and main class-factory which will return some of them by request. Create everything in public will takes a lot of time, and not so beatiful  like(pseudo code):

Class zcl_factory.

     Public section.

          TYPES:  t_point TYPE REF TO LOCAL CLASS lcl_point.                             " in my case it is about 50+ classes

          CLASS-METHODS: get_point RETURNING VALUE(point) TYPE t_point.

EndClass.

And using:

DATA: lv_point TYPE zcl_factory=>t_point.

lv_point = zcl_factory=>get_point().

*do something with lv_point

1 ACCEPTED SOLUTION
Read only

jrg_wulf
Active Contributor
0 Likes
1,960

Hi Egor,

can you point out, wehre in your opinion is the added value for use of local classes in your scenario?

Actually, i have some difficulties to fathom, what it is exactly, that you want to accomplish.

  • Are you talking about 50+ different classes or
  • are you talking about 50+ instances of one class or
  • are you talking about 50+ classes derived from one another
  • ???

The usual scenario, where you'd have some slightly different mechanics at work through the same interface, would imply. that you

  1. build an interface with everything public, including a factory method and the reference variables as class attribute
  2. build a master-class, which would implement all common methods and the factory method
  3. derived from master-class one to many classes for the varying tasks

The factory method would then have to decide, which actual implementation to invoke and store the resulting instance object in class attribute and/or return them to caller.

Any program using this construction, would only know the interface, or at most the master-class, nevertheless all implementation would be globally with "create private"

Serialization is no reason for local implementation. It will work with this as well.

With your concept of local classes, the receiving program could never know, which methods(arguments) or attributes to work. I really don't see, why anyone would want to go that way.

Best regards - Jörg

11 REPLIES 11
Read only

Former Member
0 Likes
1,960

Hello Egor,

I am still not clear with your requirement, I am sorry friend. Please correct me if I am wrong anywhere below.

you are trying to create class factory which is having around 50 class definitions in it.

whenever you need any one object reference of those 50, you will call the factory method to get the object reference.

is it correct Egor ?

Thanks,

Bhaskar

Read only

0 Likes
1,960

Yes, you're right, but i cannot find any possibility to do that.

Read only

0 Likes
1,960

Hi Egor,

Ok that's great, I wanted to know further about your requirement.

as you mentioned you need to access 50 classes through the class factory, are those 50 classes are having different functionalities with them ?

Lets suppose they are different and each class is having different method functionalities associated with it, then with our defining them individually how do we make them work ?

please correct if I am getting deviated from your requirement.

Thanks,

Bhaskar

Read only

0 Likes
1,960

I planed create internal class through factory, change some attributes, add it to container class, which will hold all instances and then serialize this container in xml with all added classes and according to their attributes.

Read only

Former Member
0 Likes
1,960

Hi Egor,


Class zcl_factory.

     Public section.

          TYPES:  t_point TYPE REF TO LOCAL CLASS lcl_point.  


Egor Titov wrote:


Class zcl_factory.

     Public section.

          TYPES:  t_point TYPE REF TO LOCAL CLASS

i think , we can not refer local class reference to to global class variable.
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,960

Hello Egor,

The visibility of a local class is restricted to the program where it is defined.


Class of an ABAP program that can be viewed and used statically from the ABAP program only. Local classes can be created in all program types, with the exception of interface pools and type groups.

Ref - ABAP Keyword Documentation.

Btw i am not sure what do you mean by this -


Create everything in public will takes a lot of time, and not so beatiful

From my experience i can say that it'S far more easy to create & maintain a global class than a local class(via SE80). But if you are using Eclipse may be there's not much difference.

BR,

Suhas

Read only

Former Member
0 Likes
1,960

Thank you for reply, but it very strange why it is possible to create public internal type in class, but not public internal class-type in class(

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,960

Egor Titov wrote:

Thank you for reply, but it very strange why it is possible to create public internal type in class, but not public internal class-type in class

What do you mean by this? Local class types are not part of the Public section of the class, so there is no straightforward way of accessing them from outside. (Read - ABAP Keyword Documentation).

Further you can see that local classes, macros are all kept in separate includes. You can use the menu function GoTo to check them out -

When you try to access the local definitions/implementations you'll get a popup telling you how these elements are separated from each other.

I use local classes very rarely and mostly for modularization purposes (as helper classes). And certainly if i had your requirement i would not think of creating local classes.

BR,

Suhas

Read only

jrg_wulf
Active Contributor
0 Likes
1,961

Hi Egor,

can you point out, wehre in your opinion is the added value for use of local classes in your scenario?

Actually, i have some difficulties to fathom, what it is exactly, that you want to accomplish.

  • Are you talking about 50+ different classes or
  • are you talking about 50+ instances of one class or
  • are you talking about 50+ classes derived from one another
  • ???

The usual scenario, where you'd have some slightly different mechanics at work through the same interface, would imply. that you

  1. build an interface with everything public, including a factory method and the reference variables as class attribute
  2. build a master-class, which would implement all common methods and the factory method
  3. derived from master-class one to many classes for the varying tasks

The factory method would then have to decide, which actual implementation to invoke and store the resulting instance object in class attribute and/or return them to caller.

Any program using this construction, would only know the interface, or at most the master-class, nevertheless all implementation would be globally with "create private"

Serialization is no reason for local implementation. It will work with this as well.

With your concept of local classes, the receiving program could never know, which methods(arguments) or attributes to work. I really don't see, why anyone would want to go that way.

Best regards - Jörg

Read only

Former Member
0 Likes
1,960

Thank you for reply.

Now I realize I chose a wrong way.

I had a more than 50 different classes, and i didn't want to make them global - from my point of view, it would take more time then just write all of them in local class include, another reason - all this classes has very short time of life, in all cases they all should be created in one method and than destroyed at the end of this method(it is just a helpful classes). So I didn't see reasons to make them global.

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,960