‎2006 Aug 09 9:00 AM
Hello,
i discovered that method calling in the class-inheritance of ABAP Objects doesn't work similar to other OO languages.
Example:
1 Superclass named "Z_CLASS"
1 Subclass of "Z_CLASS" named "Z_SUB"
Z_CLASS defines a method "initialize" and has a constructor which calls "initialize".
Z_SUB redefines the method "initialize" assigning some values to instance-attributes.
If i instantiate a new "Z_SUB"-object then the contructor of "Z_CLASS" is called which calls ITS OWN "initialize" not the one of the subclass. For comparison: Java would call the initialize of the subclass.
I have to implement a constructor in every subclass of "Z_CLASS" which calls the super constructor and the own "initialize" after that to solve this problem. This isn't the proper OO way!
Did i miss something or am i right?
Thanks in advance,
Stefan
‎2006 Aug 09 12:03 PM
Hi Stefan,
What you say is right but there are so many things in OOABAP that are not so 'OOPS'ish..:).For now, the programmer should take the pains of making OOABAP work on the lines of JAVA like langueages..
Regards,
Ravi
‎2006 Aug 09 10:56 AM
Check out this, it might be helpfull...
----
INTERFACE I_COUNTER
----
INTERFACE i_counter.
METHODS: initialize.
ENDINTERFACE.
----
CLASS C_COUNTER1 DEFINITION
----
CLASS c_counter1 DEFINITION.
PUBLIC SECTION.
INTERFACES i_counter.
METHODS: constructor.
DATA count TYPE i.
ENDCLASS.
----
CLASS C_COUNTER1 IMPLEMENTATION
----
CLASS c_counter1 IMPLEMENTATION.
METHOD constructor.
CALL METHOD me->i_counter~initialize.
ENDMETHOD.
METHOD i_counter~initialize.
count = count + 10.
write:/ 'count from super', count.
ENDMETHOD.
ENDCLASS.
----
CLASS C_COUNTER2 DEFINITION
----
CLASS c_counter2 DEFINITION INHERITING FROM c_counter1.
PUBLIC SECTION.
METHODS: constructor,
initialize.
DATA count1 TYPE i.
ENDCLASS.
----
CLASS C_COUNTER2 IMPLEMENTATION
----
CLASS c_counter2 IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
CALL METHOD me->initialize.
ENDMETHOD.
METHOD initialize.
count = ( count / 10 ).
write:/ 'count from sub', count.
ENDMETHOD.
ENDCLASS.
DATA: count TYPE REF TO c_counter2.
START-OF-SELECTION.
CREATE OBJECT count TYPE c_counter2.
‎2006 Aug 09 11:52 AM
Thanks for your answer Kesava.
in your example the subclass "c_counter2" calls the initialize in its own constructor instead of being called by the superclass.
*---------------------------------------------------------------------*
* CLASS C_COUNTER2 IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS c_counter2 IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
CALL METHOD me->initialize.
ENDMETHOD.i want the superclass to call "initialize" and automatically delegate this to the subclass which implements the initialize on its own. this is a common behaviour in OO.
my example would be:
*---------------------------------------------------------------------*
* CLASS Z_CLASS DEFINITION
*---------------------------------------------------------------------*
CLASS Z_CLASS DEFINITION.
PUBLIC SECTION.
METHODS: constructor, initialize.
DATA inittest TYPE string.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS Z_CLASS IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS Z_CLASS IMPLEMENTATION.
METHOD constructor.
initialize( ).
ENDMETHOD.
METHOD initialize.
inittest = 'Superclass'.
ENDMETHOD.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS Z_SUB DEFINITION
*---------------------------------------------------------------------*
CLASS Z_SUB DEFINITION INHERITING FROM Z_CLASS.
PUBLIC SECTION.
METHODS: initialize redefinition.
ENDCLASS.
*---------------------------------------------------------------------*
* CLASS Z_SUB IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS Z_SUB IMPLEMENTATION.
METHOD initialize.
inittest = 'Subclass'.
ENDMETHOD.
ENDCLASS.
DATA: test TYPE REF TO z_sub.
START-OF-SELECTION.
CREATE OBJECT test.
write test->inittest.
but this doesn't initialize the subclass with the string 'Subclass'
‎2006 Aug 09 12:03 PM
Hi Stefan,
What you say is right but there are so many things in OOABAP that are not so 'OOPS'ish..:).For now, the programmer should take the pains of making OOABAP work on the lines of JAVA like langueages..
Regards,
Ravi
‎2006 Aug 09 12:09 PM
OK, thanks,
i just wanted to know that i wasnt doing things wrong. This means i'll work around this.
So... now i should check out how the reward points work