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: 

Hook methods in ABAP Objects

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

former_member181962
Active Contributor
0 Kudos

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

4 REPLIES 4

Former Member
0 Kudos

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.

0 Kudos

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'

former_member181962
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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