12-16-2009 9:08 AM
Hi All,
Is it possible to call a subclass method from superclass ? If it is possible, how it can be achieved ?
Regards,
Padmam.
12-16-2009 9:29 AM
Hi,
To my 'rusting Java' knowledge, the visibility is always with the sub classes. In the sense, the sub class can access the methods and attributes of its super classes. The super class will not have any clue as to which classes have extended it.
12-16-2009 12:04 PM
Hi,
Only if the subclass overrides the method in superclass. Then by polymorphism we can use the subclass method.
12-16-2009 12:12 PM
Halo Padmam,
A super class reference variable can hold a subclass reference variable . This superclass can call methods which are defined in the superclass only .
What you can do is cast the superclass reference variable to Subclass reference variable and call the method as explained in Case 2
See for eg
data lr_animal type ref to zanimal.
data lr_lion type ref to zlion.
Here zlion is a subclass of zanimal. It is possible to assign zlion to zanimal
create object lr_lion.
create object lr_animal.
First Case
lr_animal = lr_lion.
Now when you try to call method which is specific to lion it will not allow .
Second Case.
You can assign a super class reference variable to a subclass reference variable
you need to use the casting operator for the same
lr_lion ?= lr_animal.
Now you can use lr_lion to call lion specific method
Regards
Arshad
12-16-2009 12:17 PM
If your object reference refers to an instance of the subclass and the method is redfined in the subclass, the subclass implementation of the method will be called, even if the method is called from another method in the base class. The exception is when the method is called during creation (i.e. from the contructor or a mehods called from the contructor). In this case the baseclass implementation is called.
REPORT ztest.
*----------------------------------------------------------------------*
* CLASS lcl_base DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_base DEFINITION.
PUBLIC SECTION.
METHODS:
constructor,
m1,
m2.
ENDCLASS. "lcl_base DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_base IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_base IMPLEMENTATION.
METHOD constructor.
m2( ).
ENDMETHOD. "constructor
METHOD m1.
m2( ).
ENDMETHOD. "m1
METHOD m2.
WRITE / 'lcl_base=>m2'.
ENDMETHOD. "m2
ENDCLASS. "lcl_base IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS lcl_sub DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_sub DEFINITION INHERITING FROM lcl_base.
PUBLIC SECTION.
METHODS:
constructor,
m2 REDEFINITION.
ENDCLASS. "lcl_sub DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_sub IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_sub IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
ENDMETHOD. "constructor
METHOD m2.
WRITE / 'lcl_sub=>m2'.
ENDMETHOD. "m2
ENDCLASS. "lcl_sub IMPLEMENTATION
DATA o TYPE REF TO lcl_base.
START-OF-SELECTION.
CREATE OBJECT o TYPE lcl_sub.
o->m1( ).
The output of this program is:
lcl_base=>m2
lcl_sub=>m2
The first call to m2 is during creation, which results in a call to lcl_base=>m2. The second call lcl_sub=>m2.
/Christoffer
12-16-2009 3:58 PM
You should not call the Subclass method from the Super Class. If you ever need to do this, you can create the "Agent" class and let the Agent class handle all the calls. You can decide in the Agent class, which method you need to call i.e. from Subclass or Superclass.
Regards,
Naimesh Patel