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: 

Nested interfaces and abstract methods

Former Member
0 Kudos

Consider the following design:


interface ZIF_INTERFACE_A.
  methods METHOD_A.
endinterface.

interface ZIF_INTERFACE_B.
  interfaces ZIF_INTERFACE_A.
  methods METHOD_B.
endinterface.

class CLASS_A_B definition abstract.
public section.
  interfaces ZIF_INTERFACE_B
    abstract methods METHOD_B.
  interfaces ZIF_INTERFACE_A
    abstract methods METHOD_A.
  methods METHOD_C.
endclass.
class CLASS_A_B implementation.
  method METHOD_C.
  endmethod.
endclass.

When trying to implement this design, the syntax check is giving me an error:

The method "METHOD_A" was declared as not ABSTRACT in a previous INTERFACES statement.

If I implement the design with Java (either using a public nested interface, or using interface inheritance) there is no problem declaring both the methods METHOD_A and METHOD_B as abstract in the CLASS_A_B class.

So it seems to me this might be a bug in the ABAP Objects implementation. Or perhaps I am missing something else here? Does anyone have any suggestions?

1 ACCEPTED SOLUTION

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

Nested interfaces

When you include an IF in another IF both of them will exist on the same level, there is no concept of hierarchy. So when you add interface ZIF_B in the implementing class ZCL_A_B, the interface ZIF_A is also "included" in the relationship with the class ZCL_A_B implicitly.

Just try to activate the code below & you'll get the error: "Implementation missing for method zif_interface_a~method_a":

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.
*    INTERFACES zif_interface_a
*      ABSTRACT METHODS method_a.
    INTERFACES zif_interface_b
      ABSTRACT METHODS: method_b.
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.
    INTERFACES zif_interface_b
      ABSTRACT METHODS method_b.
    INTERFACES zif_interface_a
      ABSTRACT METHODS method_a.
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

So when the compiler is trying to compile your code it encounter implementation of ZIF_INTERFACE_B it finds that the "included interface" method ZIF_INTERFACE_Amethod_a is not abstract. So when you declare ZIF_INTERFACE_Amethod_a as "abstract" in the next line the compiler throws this error.

So you need to define ZIF_INTERFACE_A~method_a as abstract, before defining ZIF_INTERFACE_B. Like this:

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.

    INTERFACES zif_interface_a
      ABSTRACT METHODS method_a.

    INTERFACES zif_interface_b
      ABSTRACT METHODS method_b.
    
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

In this case the compiler treats method_a as abstract while checking ZIF_INTERFACE_B. Hence it doesn't throw any error.

Hope i'm clear.

BR,

Suhas

PS: Whenever i face any problem while creating local classes, i create a dummy class in the class builder to check

7 REPLIES 7

former_member182670
Contributor
0 Kudos

Seems like a bug.

Class declaration should look like

class CLASS_A_B definition abstract.
public section.
  interfaces ZIF_INTERFACE_B
    abstract methods METHOD_B METHOD_A.
  methods METHOD_C.
endclass.

or

class CLASS_A_B definition abstract.
public section.
  interfaces ZIF_INTERFACE_B
    abstract methods METHOD_B zif_interface_a~method_a .
  methods METHOD_C.
endclass.

but both options give syntax errors.

As a workaround you can use

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.
    INTERFACES zif_interface_b
      ALL METHODS ABSTRACT.

    METHODS method_c.
ENDCLASS.

which works fine

0 Kudos

@Tomek Mackowski

I do not want all methods to become abstract (there are more methods inside the interfaces which I did not copy into my example).

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

Nested interfaces

When you include an IF in another IF both of them will exist on the same level, there is no concept of hierarchy. So when you add interface ZIF_B in the implementing class ZCL_A_B, the interface ZIF_A is also "included" in the relationship with the class ZCL_A_B implicitly.

Just try to activate the code below & you'll get the error: "Implementation missing for method zif_interface_a~method_a":

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.
*    INTERFACES zif_interface_a
*      ABSTRACT METHODS method_a.
    INTERFACES zif_interface_b
      ABSTRACT METHODS: method_b.
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.
    INTERFACES zif_interface_b
      ABSTRACT METHODS method_b.
    INTERFACES zif_interface_a
      ABSTRACT METHODS method_a.
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

So when the compiler is trying to compile your code it encounter implementation of ZIF_INTERFACE_B it finds that the "included interface" method ZIF_INTERFACE_Amethod_a is not abstract. So when you declare ZIF_INTERFACE_Amethod_a as "abstract" in the next line the compiler throws this error.

So you need to define ZIF_INTERFACE_A~method_a as abstract, before defining ZIF_INTERFACE_B. Like this:

CLASS class_a_b DEFINITION ABSTRACT.
  PUBLIC SECTION.

    INTERFACES zif_interface_a
      ABSTRACT METHODS method_a.

    INTERFACES zif_interface_b
      ABSTRACT METHODS method_b.
    
    METHODS method_c.
ENDCLASS.                    "CLASS_A_B DEFINITION

In this case the compiler treats method_a as abstract while checking ZIF_INTERFACE_B. Hence it doesn't throw any error.

Hope i'm clear.

BR,

Suhas

PS: Whenever i face any problem while creating local classes, i create a dummy class in the class builder to check

0 Kudos

Good points Suhas. Definitely the order of declaration matters here. I didn't think about it in the first place.

Former Member
0 Kudos

@Suhas Saha:

Changing the order works, thanks!

It does look like this has to be done manually in a global class though.

0 Kudos

> Changing the order works, thanks!

> It does look like this has to be done manually in a global class though.

You're welcome.

No, you don't have to manually do this while defining a similar global class. The class builder automatically maintains the order of the definition!

Cheers,

Suhas

Former Member
0 Kudos

No, you don't have to manually do this while defining a similar global class. The class builder automatically maintains the order of the definition!

Actually, I did have to do that. The class builder declared the interfaces in the wrong order.

I only wrote the local classes to demonstrate the problem on this forum.