Skip to Content

Constants - To be declared in class or interface?

Hello OO gurus,

The place where I work is upgrading SAP version and now all developments must be done using OO concepts. I am already familiar with this concept but i have one doubt regarding its application in ABAP programs.

I transformed a existing procedural report into a object oriented report. I declared all the constants used in this program in a separated class as follows:

CLASS: cl_constants_handler DEFINITION.
  PUBLIC SECTION.
    CONSTANTS:
            c_h1(10)          TYPE c    VALUE   'Purch.Doc.',
            c_h2(09)          TYPE c    VALUE   'Doc. date',
" (...)

However i saw that constants are declared in interfaces in standards programs. Then, classes need to implement these interfaces to used them. Examples: IF_BCB_ITEM_CONSTANTS, IF_CRM_CONSTANT1, IF_UA_APPLICATION_CONSTANTS

My question is: what is the advantage to declare constants in interfaces instead of classes?

Thanks,

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Mar 29, 2010 at 02:48 PM

    I know it is done in the standard, but personally I do not like the practice of using interfaces for constants. Coming from a Java background, I consider this an anti-pattern. The main idea of an interface is to expose the behavior of a class and hiding the implementation detail. Inventing an interface and throwing in constants without regard to the previous consideration makes me squirm.

    If I absolutely have to have a place for global constants, I prefer an abstract class. An added advantage is that you can have table-type constants as read-only static variables that are initialized in the class constructor.

    -- Sebastian

    Add comment
    10|10000 characters needed characters exceeded

    • the problem is there is no multiple inheritance in ABAP OO : a class has only one parent class

      that's why we use interfaces to store constants: this way we just have to "implement" each interface with needed constants (I mean it is possible to have several interfaces in the same class, and so to get constants from several at the same time)

  • Mar 29, 2010 at 02:10 PM

    I think there is no big advantage in defining constants in an interface. All constants are address as static components so you can use any public constant of any class you want.

    Here the interface just introduce new logical grouping. You can store all the constants in one place and call the interface some more meaningfully like i.e. "if_physics" than just addressing them from different classes.

    Personally I prefer to use interfaces as a central point for types which I use accross several classes. This way I don't need to define them separately in each class, I just implement right interface.

    So, in my opinion this is all about logical data grouping, to make the program more meaningful to other developer.

    Regards

    Marcin

    Add comment
    10|10000 characters needed characters exceeded