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: 

can we access public method of a child class specific method by using parent class object ?

Balu483
Participant
1,718

hi to all,

i was trying to access the public method of child class specific method ,by using Parent class reference object by using narrow casting but i was unable to access .

so how can i access public method of a child class by using parent class referred object?

if poss

6 REPLIES 6

raviandela
Explorer
335

When narrow casting we cannot directly call the child class method. you need to dynamically call the method. Please make sure that it's narrow casted otherwise, you will run into short dumps.

for ex: call method parent_class_ref->('child_specific_method').

In this example compiler do not check the existence of child_specific_method against parent_class_ref during compile time.

Hope this helps..

Ravi

0 Kudos
335

thank you ravi Andela

it's working . i can able to call Child Specific method by using Parent class ref. by narrow casting.

fist i was tried like parent_class_ref->('child_specific_method') it given dump err as

"The method could not be found at dynamic call"

then again i tried the same with parent_class_ref->('CHILD_SPECIFIC_METHOD') then it's working.

we have to provide capital letter's inside the single quotes.


thank you for your suppost.

matt
Active Contributor
335

You're adopting less than good practice and adding a weakness to your code.

If you use this approach, and the method name changes, you get a runtime error. If you use the approach I suggested, you get a syntax error.

Syntax errors are easy, quicker and cheaper to fix than runtime errors.

matt
Active Contributor
335
CAST class_child( parent )->child_method( ).

or

data child type ref to class_child.
child ?= parent.
child->child_method( ).

0 Kudos
335

hi mathew Billingham,

first of all thanks for your Quick Response.

as you mention

data child typerefto class_child. child ?= parent. child->child_method().

if i code as above, then syntax error is occurred as

Method "CHILD_METH" is unknown or PROTECTED or PRIVATE

my concern is to access child specific method by using parent class reference variable.

parent_class_ref->('CHILD_SPECIFIC_METHOD')other than this, is their any fusibility to meet my requirement.

if so, please give the code snippet.

thank you.

matt
Active Contributor
335

I have given you a code snippet. I'm not quite sure what you've failed to understand.

Perhaps this will make it clear.

CLASS lcl_parent DEFINITION.

ENDCLASS.

CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.
  PUBLIC SECTION.
    METHODS child_method.
ENDCLASS.

CLASS lcl_child IMPLEMENTATION.
  METHOD child_method.
    WRITE 'Child method called'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  DATA parent TYPE REF TO lcl_parent.
  CREATE OBJECT parent TYPE lcl_child.

  DATA child TYPE REF TO lcl_child.
  child ?= parent.
  child->child_method( ).

If you cast the parent reference variable to a reference variable that is the type of reference to the child class, then you have access to methods defined on the child class. (Assuming the parent is actually a child instance).