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: 

How to call parent class method using sub class object

vikash_pathak
Participant
0 Kudos

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

1 ACCEPTED SOLUTION

raghug
Active Contributor
0 Kudos

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
15 REPLIES 15

ChrisSolomon
Active Contributor

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().

0 Kudos

Hello christopher,

thanks for your response,

but this super keyword you can use only within the child class cannot access with object ,

You can only use "SUPER->" within instance methods. -

That is correct....you would use it WITHIN you child class....how else do you plan to call the "parent" of the "child" without going through the child? Also, "->" is for instance methods.....you want the INSTANCE of the parent related to the INSTANCE of the child......calling a static method (ie. "=>") would not make since as you could always just call that directly (like calling a function module)....but even then,there are ways to do this as outlined here:

https://help.sap.com/abapdocu_731/en/abeninheritance_statical.htm

And maybe you missed it, but the OO design for ABAP (unlike other languages) does NOT allow you to redefine STATIC methods/attributes of a "parent" class. As someone else said, it would be like taking an existing function module and trying to use the same name for it in a new custom function module....will not work.

..and by the way, matthew.billingham LOVES to answer this question...as he has done often through the years. 🙂

And if you don't get what Christopher is talking about, here is a simple example:

      Method Get_Interface_Name.
*
*            The RFID interface is used for many different interfaces but
*            the interface name is always RFID.  Redefine this get name
*            method to get a different interface name based upon the RFID sub
*            interface being processed.
*
             If Me->Rf_IntName Is Not Initial.
                r_Name = Me->Rf_IntName.
             Else.
                r_Name = Super->Get_Interface_Name( ).
             EndIf.
      EndMethod.

matt
Active Contributor

I do? 🙂

nomssi
Active Contributor
0 Kudos

Hello Vikash,

you have the Classes CLASS_PARENT and CLASS_CHILD

Your Object OBJ references CLASS_CHILD, so you can invoke its DISPLAY( ) method. You must create an object referencing CLASS_PARENT to call a method of CLASS_PARENT outside the CHILD_CLASS.

You have enough information to create the new object in your case as nothing is protected/private.

A warning: Why Final Classes are used in ABAP

JNN

0 Kudos

Hello jacques,

But my concern is here , that i have to call parent class method from child class object . eg.

suppose i have created object with name obj

now i will call display method

obj->display

so here it must call child class method , but i have to call parent class method display using this instance obj.

Thanks

nomssi
Active Contributor
0 Kudos

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

raghug
Active Contributor
0 Kudos

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

ChrisSolomon
Active Contributor

Oh no....you just confused the crap out of him. hahahahaha

After reading and re-reading his question and follow up a few times, I think he was really re-asking that age-old question of "can you redefine a static method of a parent class in your child class ?".

0 Kudos

Hello raghu,

your way is correct as per my requirement, but i have one doubt , that is there a another way to call parent method using child object based on parameter , as there is one concept method overriding in which we can call method based on different no. of parameters , so is it possible in abap or not , and if it is possible so how can we achieve that

Thanks

as there is one concept method overriding in which we can call method based on different no. of parameters. so is it possible in abap or not

Nopes, you can't!

0 Kudos

ok got it ,

thanks

raghug
Active Contributor

Try "IS SUPPLIED" https://help.sap.com/abapdocu_70/en/ABENLOGEXP_SUPPLIED.htm

If parameter_1 IS SUPPLIED.
  ..do this
ELSE.
  ..do that
ENDIF.

raghug
Active Contributor

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...