Skip to Content
-5

How to call parent class method using sub class object

Hello friends,

I have a question in OOPS ABAP,

i have two class class_parent and class_child , and child class is inheriting parent class and using same method in child class but different implementation , so i s there a way that we could call parent method from child class object Eg.

CLASS CLASS_PARENT DEFINITION.

PUBLIC SECTION.

DATA: A TYPE I,

B TYPE I,

C TYPE I.

METHODS: DISPLAY.

ENDCLASS.

CLASS CLASS_PARENT IMPLEMENTATION.

METHOD DISPLAY.

C = A + B.

WRITE:/ C.

ENDMETHOD.

ENDCLASS.

CLASS CLASS_CHILD DEFINITION INHERITING FROM CLASS_PARENT.

PUBLIC SECTION.

DATA: A1 TYPE I,

B1 TYPE I,

C1 TYPE I.

METHODS: DISPLAY REDEFINITION. "REDEFINE THE SUPER CLASS METHOD ENDCLASS.

CLASS CLASS_CHILD IMPLEMENTATION.

METHOD DISPLAY.

C1 = A1 * B1.

ENDMETHOD.

ENDCLASS.

*CREATE THE OBJECT FOR SUB CLASS.

DATA: OBJ TYPE REF TO CLASS_CHILD.

START-OF-SELECTION.

CREATE OBJECT OBJ.

CALL METHOD OBJ->DISPLAY.

So is there a way that we could access CLASS_PARENT method from object OBJ

Thanks in Advance

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Jan 12, 2017 at 02:33 PM

    If you are asking if you can access the original parent method from an instance of the child class that redefined that same method - the answer is No, you can't directly do that.

    If you really, really must have the ability to have a choice, then declare a parameter (you will have to do this on the parent method) that you can use as a switch in the child method... and then, you can....

    CLASS class_child IMPLEMENTATION.
      METHOD display.
        IF use_parent = abap_true.
          super->display( ).
        ELSE.
          c1 = a1 * b1.
        ENDIF.
      ENDMETHOD.
    ENDCLASS.

    and from your instance of the object, call it with or without the swtich

    ob_child_class->display( use_parent = abap_true). "Use parent method
      OR
    ob_child_class->display( ). "Use child method
    Add comment
    10|10000 characters needed characters exceeded

    • Or maybe he confused the crap out of everyone! I really blame the schools and lack of teaching the art of PrĂ©cis or common logic. There is no way I could have got to the answer I did without reading all the back and forth and looking for tiny hidden clues. The problem statement should be clearly defined and unnecessary crud not burying it. If the OP had stated clearly (which in re-reading he kind-of sort-of did on the very last line of the original post) - "Can I access a redefined parent method conditionally from a declared instance of the child class" - and associated a very succinct example.

      DATA: obj_child TYPE REF TO child_class.
      
      obj_child->redefined_method( ).  "<- Is there a way to access parent method selectively here?

      Then the discussion would have been very different...

  • Jan 12, 2017 at 01:19 PM

    Did you try searching at all? I just Googled "SAP call parent class" and had 10+ hits right off. Also noticed that you just posted a LOT of very basic yet unrelated questions and wondering why. Anyways....

    From within your "child" method...

    super->parent_method().

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Vikash,

      you can create a clone outside of the child class to access the parent method:

      DATA parent TYPE REF TO class_parent.
      CREATE OBJECT parent.
      parent->a = obj->a.
      parent->b = obj->b.
      parent->c = obj->c.
      parent->display( ).

      this however would be misusing inheritance if the subclass does not behaves as the parent. You can achieve your goal with delegation, e.g. strategy.

      JNN