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

Why a sub object can not execute the super's method?

hagit
Active Participant
0 Likes
4,943

Hello Experts,

I have the following code:

1. Interface zif_4super with a method interface_Super_method

2. Class Zcl_Super which

2.1. implements zif_4super

2.2. is abstract

2.3. has a protected method

3. interface zif_4sub with a method interface_sub_method

4. class zcl_sub which

4.1. implements zif_4sub

4.2. inherits zcl_super

5. In a program Ztry18

5.1. data: lo_sub_typ_if_4sub type REF TO zif_4sub. "line 10
lo_sub_typ_if_4sub->interface_sub_method( ). "line 13
*lo_sub_typ_if_4sub->interface_super_method( ). "line 15

Line 15 raises an activating error: Method "INTERFACE_SUPER_METHOD" does not exist.

Why lo_sub does not have the super's method?

5.2. If I change line 10 to

data: lo_sub_typ_if_4sub type REF TO zif_4super

then line 13 raises an activating error: Method "INTERFACE_SUB_METHOD" does not exist.

My question is why a sub object can not execute the super's method?

interface ZIF_4SUPER
public .

methods INTERFACE_SUPER_METHOD .
endinterface.
class ZCL_SUPER definition
public
abstract
create public .

public section.

interfaces ZIF_4SUPER .
protected section.

methods PROTECTED_SUPER_METHOD .
private section.
ENDCLASS.



CLASS ZCL_SUPER IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Protected Method ZCL_SUPER->PROTECTED_SUPER_METHOD
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method PROTECTED_SUPER_METHOD.
message 'protected in super class' TYPE 'I'.
endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_SUPER->ZIF_4SUPER~INTERFACE_SUPER_METHOD
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method ZIF_4SUPER~INTERFACE_SUPER_METHOD.

MESSAGE 'Super method - super interface' TYPE 'I'.

endmethod.
ENDCLASS.
interface ZIF_4SUB
public .


methods INTERFACE_SUB_METHOD .
endinterface.
class ZCL_SUB definition
public
inheriting from ZCL_SUPER
final
create public .

public section.

interfaces ZIF_4SUB .
protected section.
private section.
ENDCLASS.



CLASS ZCL_SUB IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_SUB->ZIF_4SUB~INTERFACE_SUB_METHOD
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method ZIF_4SUB~INTERFACE_SUB_METHOD.

message 'INTERFACE_SUB_METHOD' TYPE 'I'.
endmethod.
ENDCLASS.
REPORT ZTRY18.
*data: lo_sub_typ_if_4sub type REF TO zif_4sub
data: lo_sub_typ_if_4sub type REF TO zif_4super
,lo_sub_typ_if_4super type REF TO zif_4super .

lo_sub_typ_if_4sub = new zcl_sub( ).
lo_sub_typ_if_4sub->interface_sub_method( ).
*lo_sub_typ_if_4sub->interface_super_method( ). "activate err: Method "INTERFACE_SUPER_METHOD" does not exist.

lo_sub_typ_if_4super = new zcl_sub( ).
lo_sub_typ_if_4super->interface_super_method( ).

Thanks in advanced,

Hagit

1 ACCEPTED SOLUTION
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
4,764

You can statically only access interface components using an interface reference variable that are known by the interface reference variable, meaning components that are defined in the interface used for typing.

You must understand the concept of polymorphism that is enabled by inheritance and interfaces. Interfaces are orthogonal to inheritance. Don't mix this is up.

For more information and an introduction, see:

ABAP Objects - Inheritance

ABAP Objects - Interfaces

Setting Reference Variables

27 REPLIES 27
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
4,765

You can statically only access interface components using an interface reference variable that are known by the interface reference variable, meaning components that are defined in the interface used for typing.

You must understand the concept of polymorphism that is enabled by inheritance and interfaces. Interfaces are orthogonal to inheritance. Don't mix this is up.

For more information and an introduction, see:

ABAP Objects - Inheritance

ABAP Objects - Interfaces

Setting Reference Variables

Read only

0 Likes
4,764

horst.keller , thanks for your answer.

Isn't it a waste to declare the object twice? (In the constructor of zcl_sub there are some fetches from DB)

Read only

4,764

SAP Community has got a so poor text editor, all these <br> in the code 😞

Read only

0 Likes
4,764

Agree with you sandra.rossi, even when you copy / past a part of the code of the question, you have issu. I need always to open a notepad to have something clean.

Read only

4,764

sandra.rossi and frdric.girod

could you please refer to my original question and to my comment?

Thanks a lot

Hagit

Read only

4,764

hagit you declare twice the instances (object means nothing)

and each instance need her how declaration because the interface are different.

but, you could declare two instance using two differents interfaces for the same "instance" using ?=

Read only

4,764

horst.keller, I think your link is an internal SAP link.

Read only

0 Likes
4,764

frdric.girod thank you so much for your great answer (using ?=).

Could you please tell me, which of the following approach is better:

I have an attribute in the super interface (M_ZIF_4SUPER)

interface ZIF_4SUPER
public .
data M_ZIF_4SUPER type I .
methods INTERFACE_SUPER_METHOD .

endinterface.

The constructor of zcl_sub put a value to this attribute.



* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_SUB->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
super->constructor( ).
MESSAGE 'in zcl_sub constructore' TYPE 'I'.
* me->zif_4super~m_zif_4super = 1."line 30
DATA: lo_sub_typ_if_4super TYPE REF TO zif_4super. "line 31
lo_sub_typ_if_4super ?= me.
lo_sub_typ_if_4super->m_zif_4super = 1. "line 33
ENDMETHOD.

Is it better to write it as in the above line 30?

Or it is better to write it as in lines 31- 33?

Thank you again for your great help

Hagit

Read only

4,764

I use the first one when I have to access only one time to the interface, and the second when I need to access several time.

(even if the second is more clean)

Read only

0 Likes
4,764

frdric.girod thank you for your answer

Read only

0 Likes
4,764

@ hagit don't forget to close the question & validate the answer

Read only

0 Likes
4,764

frdric.girod , Ok.

I still check the issue

Read only

0 Likes
4,764

frdric.girod ,

Concerning the above issue: The constructor of zcl_sub refers to an attribute (M_ZIF_4SUPER) . This attribute is declared in the super interface.

This attribute should be in every sub class that inherits from the abstract class zcl_super.

Is it better to declare the attribute in the super interface (as it is) or to declare it as protected in zcl_super?

Thank you again for your help

Hagit

Read only

hagit
Active Participant
0 Likes
4,764

I did as frdric.girod suggested : declare two instance using two differents interfaces for the same "instance" using ?=

REPORT ZTRY18.
data: lo_sub_typ_if_4sub type REF TO zif_4sub.
data: "lo_sub_typ_if_4sub type REF TO zif_4super
lo_sub_typ_if_4super type REF TO zif_4super .

lo_sub_typ_if_4sub = new zcl_sub( ).
lo_sub_typ_if_4sub->interface_sub_method( ).
*lo_sub_typ_if_4sub->interface_super_method( ). "activate err: Method "INTERFACE_SUPER_METHOD" does not exist.

*lo_sub_typ_if_4super = new zcl_sub( ).
lo_sub_typ_if_4super ?= lo_sub_typ_if_4sub .
lo_sub_typ_if_4super->interface_super_method( ).
lo_sub_typ_if_4super->M_ZIF_4SUPER = 8.
Read only

FredericGirod
Active Contributor
4,764

An interface is a contract between outside & the class.

You have 2 contracts :

  • if_4Sub
  • if_4Super

When you want to access through a contract, you have to create an instance. The instance you create with a reference to a contract will see only the term of the contract (the method declared in the interface).

--> That mean, if you have public method in the class, and you declare the instance using the interface, you will not see this public method.

--> That mean, if you have several interfaces in the same class, the instance on interface 1 will not see the method of interface 2.

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod thanks for your comment.

I understand that in the outside world the instance on interface 1 will not see the method/attribute of interface 2.

But in the class itself I can refer to the components of interface 2. The question is: Is it better to declare the attribute(component) in the super interface or to declare it as protected in zcl_super?

Thanks

Hagit

Read only

FredericGirod
Active Contributor
0 Likes
4,764

It is a conceptual question, so the answer will be : it depends of the requirement 🙂

The Abstract is not an easy way of working with Oo.

To help you answering your question, I think you have to add the SOLID principle in your thinking. Specificaly the :

  • Avoid dependency (dependency inversion)
  • Single responsibility

You use interface to avoid dependency, if your interface 1 access your interface 2, you will have a dependency. This is little bit exaggerated because you are inside a class referencing the two interfaces. But it means if you need to change interface 2 you will impact interface 1.

So, always work with Interface_1~method_1 --> private_method_a // Interface_2~method_2 --> private_method_a

and never Interface_1~method_1 --> Interface_2~method_2

For the question of, where to put attribute, in final class or in abstract class, it should be related of the responsibility. This is a responsibility of the abstract to have this attribute ? this attribute will be use in every class implementing this abstract ?

And do not forget to simplify as much as possible your abstract. This is not a bag where you could put everything you need.

As you have a lot of questions, I recommend you to read this book "Object Oriented Design With ABAP" from jjamese.mcdonough

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod thank you for your answer.

The question is not: if to put attribute, in final class or in abstract class

The question is : if to put attribute, in the interface of the abstract class or in abstract class

Thank you

Hagit

Read only

FredericGirod
Active Contributor
0 Likes
4,764

The first answer is the visibility.

In the interface, you could see it from the outside using an instance based on the interface

In the class, you couldn't see it from the outside using an instance based on the interface

So, do you want someone access directly this attribute from outside ?

or do you need this attribute only for the private business of the final class ?

This will also be related to responsibility. Is it really the responsibility of the Abstract to manage this attribute ? Especialy if it is a private attribute ?

(it is not easy to answer, and you have a lot of solutions & reasons to use them, do not take it as golden rule)

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod

Visibility- No access from outside.

The attribute should be protected. I.E., this attribute will be accessed by a method in the abstract class & by the constructor of every sub class that inherits the abstract class.

Responsibility-:

In every sub class that inherits the abstract class:

  1. The constructor initializes the attribute
  2. A method refers to this attribute in its method

The method in the abstract class refers to this attribute in its method.

Is the conclusion from the above is that the attribute should be protected in the abstract class?

Read only

FredericGirod
Active Contributor
0 Likes
4,764

private of the abstract class

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod

Shouldn't the attribute be protected in abstract zcl_super?

Otherwise zcl_sub does not know the private attribute.

In zcl_sub constructor: me->M_PRIVATE_IN_ZCL_SUPER = 2.

Gives activating error:

"Field "M_PRIVATE_IN_ZCL_SUPER" is unknown. It is neither in one of the …"

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod ,

Could you please answer my question?

Thank you

Hagit

Read only

FredericGirod
Active Contributor
4,764
*&---------------------------------------------------------------------*
*& Report ztest_fred_001
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_fred_001.


INTERFACE if_4super .
METHODS interface_super_method .
ENDINTERFACE.
INTERFACE if_4sub.
METHODS interface_sub_method .
ENDINTERFACE.

CLASS cl_super DEFINITION
ABSTRACT
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_4super .
PROTECTED SECTION.
METHODS protected_super_method .
data toto type abap_bool.
ENDCLASS.
CLASS cl_sub DEFINITION
INHERITING FROM cl_super
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_4sub .
ENDCLASS.


CLASS cl_super IMPLEMENTATION.
METHOD protected_super_method.
MESSAGE 'protected in super class' TYPE 'I'.
ENDMETHOD.
METHOD if_4super~interface_super_method.
MESSAGE 'Super method - super interface' TYPE 'I'.
ENDMETHOD.
ENDCLASS.
CLASS cl_sub IMPLEMENTATION.
METHOD if_4sub~interface_sub_method.
MESSAGE 'INTERFACE_SUB_METHOD' TYPE 'I'.
move abap_true to toto. "<-- no error
ENDMETHOD.
ENDCLASS.



start-of-selection.


DATA lo_sub_typ_if_4sub TYPE REF TO if_4sub.
DATA lo_sub_typ_if_4super TYPE REF TO if_4super .
lo_sub_typ_if_4sub = NEW cl_sub( ).
lo_sub_typ_if_4sub->interface_sub_method( ).
*lo_sub_typ_if_4sub->interface_super_method( ). "activate err: Method "INTERFACE_SUPER_METHOD" does not exist.
*lo_sub_typ_if_4super = new zcl_sub( ).
lo_sub_typ_if_4super ?= lo_sub_typ_if_4sub .
lo_sub_typ_if_4super->interface_super_method( ).



end-of-selection.
Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod ,

I see that toto is protected (not private).

So did you mean that the attribute should be protected?

Thanks

Read only

FredericGirod
Active Contributor
0 Likes
4,764

yes, you are right

Read only

hagit
Active Participant
0 Likes
4,764

frdric.girod

Thank you so match

Hagit