Application Development 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: 

Why aliases are allowed in every visibility section of a class?

jitendra_it
Active Contributor
0 Kudos

Hello Experts,

As we know interface can only be implemented in public section of a class but why Aliases are allowed in any visibility section of a class. Any specific purpose for that? Check below sample. In this code getting error at last line of code since m1 is declared in the private section.

SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

INTERFACE if1.
  METHODS : m1.
ENDINTERFACE.


CLASS lcl DEFINITION.
  PUBLIC SECTION.
    INTERFACES if1.
   PRIVATE SECTION.
    aliases m1 for if1~m1.
ENDCLASS.

CLASS lcl IMPLEMENTATION.
  METHOD m1.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA: lo TYPE REF TO lcl.
  CREATE OBJECT lo.
  lo->if1~m1( ).  " Not syntax error
  lo->m1( ).      " Syntax error
2 REPLIES 2

Sandra_Rossi
Active Contributor

I guess your question is from the "OO design" perspective, why SAP designed the possibility to have private aliases? (because technically it's obvious that a private alias can be used only inside the class and by its friends).

My guess:

1) A public alias needs be created when both the objects are to be instantiated via the class rather than the interface, and the aliased member is often used externally (outside the class and its friends).

2) Sometimes, a member is rarely used externally, but often used internally (inside the class or by its friends). To follow the rule, make public only the minimum, declare it as a private alias.

PS: if you want to analyze SAP real cases, the following SQL query lists the (standard) classes which have both public and private aliases:

SELECT DISTINCT CLSNAME
FROM SEOCOMPODF A
WHERE EXISTS (
SELECT * FROM SEOCOMPODF B
WHERE A.CLSNAME = B.CLSNAME AND ALIAS = 'X' AND EXPOSURE = 0 )
AND EXISTS (
SELECT * FROM SEOCOMPODF B
WHERE A.CLSNAME = B.CLSNAME AND ALIAS = 'X' AND EXPOSURE = 2 )

The following SQL query lists the (standard) classes which have both public and private aliases on components with the same name from different interfaces:

SELECT DISTINCT CLSNAME
FROM SEOCOMPODF A
WHERE EXISTS (
SELECT *
FROM SEOCOMPODF B
WHERE A.CLSNAME = B.CLSNAME
AND A.REFCLSNAME <> B.REFCLSNAME
AND A.REFCMPNAME = B.REFCMPNAME
AND A.ALIAS = 'X'
AND B.ALIAS = 'X'
AND A.EXPOSURE = 2
AND B.EXPOSURE <> 2 )

matt
Active Contributor

Generally, call an interface method directly from an instance of an implementing class seems a bit off. It means you're using an instance variable type to REF lcl, which is usually not correct. You should rather use:

  DATA: lo TYPE REF TO lif.
  CREATE OBJECT lo type lcl.
  lo->m1( ).   

Within a class though, you might want to save a bit of typing, so use a private alias.

Using a private alias seems to me to be more reasonable than using a public alias.

The only exception is when you extract an interface from a class, and you don't want to have to change all the programs that use the class to now go via the interface.