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 can a parent class access public and protected methods/variables of child class?

former_member227510
Participant
0 Kudos

We can access the public, protected and private members of parent class in sub classes/child class. But public class can access the protected and public members of child class as well. How to achieve this?

*&---------------------------------------------------------------------*

*& Report  ZSR_TEST

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*

*       CLASS parent DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS child DEFINITION DEFERRED.

*----------------------------------------------------------------------*

*       CLASS parent DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS parent DEFINITION FRIENDS child.

   PUBLIC SECTION.

     DATA: v_pub TYPE char40 VALUE 'Public variable in Parent class'.

     METHODS: m_parent.

   PROTECTED SECTION.

     DATA: v_pro TYPE char40 VALUE 'Protected variable in Parent class'.

   PRIVATE SECTION.

     DATA: v_pri TYPE char40 VALUE 'Private variable in Parent class'.

ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*

*       CLASS parent IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS parent IMPLEMENTATION.

   METHOD m_parent.

     WRITE: / 'Method - M_PARENT :-',

            / 'Parent Class Attributes are as follows: ',

            / '=======================================',

            / v_pub,

            / v_pro,

            / v_pri.

     SKIP 2.

    

*     CALL METHOD m_child.

   ENDMETHOD.                    "m_parent

ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*

*       CLASS child DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS child DEFINITION INHERITING FROM parent.

   PUBLIC SECTION.

     METHODS m_child.

     DATA: c_pub TYPE char40 VALUE 'Hello'.

   PROTECTED SECTION.

   PRIVATE SECTION.

ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*

*       CLASS child IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS child IMPLEMENTATION.

   METHOD m_child.

     DATA oref TYPE REF TO parent.

     CREATE OBJECT oref.

     WRITE  oref->v_pri.

     WRITE: / 'Method - M_CHILD :-',

            / 'Child class can access all Public attributes from Parent',

            / '========================================================',

            / v_pub,

            / v_pro.

     "/ v_pri. Private data can't be accessed from Child

     SKIP.

     v_pub = 'Public has been changed in Child class'.

     v_pro = 'Protected has been changed in Child class'.

     WRITE: / 'Changed Public & Protected attributes:',

           / '======================================',

           / v_pub,

           / v_pro.

*           / v_pri.

   ENDMETHOD.                    "m_child

ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.

   DATA: o_parent TYPE REF TO parent,

         o_child TYPE REF TO child.

   CREATE OBJECT: o_parent, o_child.

   CALL METHOD: o_parent->m_parent,

               o_child->m_child.

   o_parent->v_pub = 'Public data is changed again by Sandip.'.

   SKIP 2.

   WRITE: / 'Public data can be changed outside the Class:',

          / '=============================================',

          / o_parent->v_pub.

Regards,

Gopa

1 ACCEPTED SOLUTION

matt
Active Contributor
0 Kudos

"We can access the public, protected and private members of parent class in sub classes/child class. "

No, you can't.

"But public class can access the protected and public members of child class as well. How to achieve this?" Question makes no sense. What do you mean by "public class"?


Programming requires precision. Please make sure that you read your post before posting to make sure you are using words correctly.

13 REPLIES 13

krishna_k19
Contributor
0 Kudos

This message was moderated.

matt
Active Contributor
0 Kudos

"We can access the public, protected and private members of parent class in sub classes/child class. "

No, you can't.

"But public class can access the protected and public members of child class as well. How to achieve this?" Question makes no sense. What do you mean by "public class"?


Programming requires precision. Please make sure that you read your post before posting to make sure you are using words correctly.

former_member227510
Participant
0 Kudos

Hi Matthew,

Sorry for the mistake. The correct question is as follows:

We can access the public, protected and private members of parent class in sub classes/child class.

Yes we can. I have done it in the above code.


But parent class can access the protected and public members of child class as well. How to achieve this?

0 Kudos

a parent class doesn't know who are their child sub class.

therefore i don't think it is possible.

0 Kudos

We can access the public, protected and private members of parent class in sub classes/child class.

Yes we can. I have done it in the above code.

No. You cannot access private members of the superclass from the subclass. Unless of course, you define the superclass as the friend of the subclass as you have done.

If you want to know if you can access the protected or private members of a subclass, write a program to find out, that does something like this (after removing and FRIEND relationship):

METHOD superclass_method.

  write: / instance_of_subclass->protected_attribute, 'is a protected attribute of the subclass'.

  write: / instance_of_subclass->private_attribute, 'is a private attribute of the subclass'.

ENDMETHOD.

former_member227510
Participant
0 Kudos

Hi Matthew,

Done that. Public  variable c_pub of child is accessible is accessible.

Protected is giving error as in the screenshot. I am exploring. Will write when I get to know the final.

Thanks for the support. Your inputs are welcome.

0 Kudos

This is entirely as expected. I'm curious, why did you post the question instead of just trying it?

former_member227510
Participant
0 Kudos

Hi all,

CLASS child DEFINITION INHERITING FROM parent FRIENDS parent.

I tried this. The parent class is now able to access protected and private members of child class.

This is what I wanted to know.

Regards,

Gopa.

0 Kudos

Why do you want to know this? If one class is a friend of another, it has a access to that classes private members. It makes no difference whether there is any kind of inheritance relationship between them.

0 Kudos

Hi Gopa,

in addition to all the things Matthew said you should probably read some basic literature on OO design and OO software development. The design patterns book by Gamma et. al (Design Patterns - Wikipedia, the free encyclopedia) is probably a good place to start.

Trying to access attributes of a sub-class from within a super-class  is almost certainly the result of  a lack of proper OO design. The same is IMHO true for the usage of friends in almost all cases.

Christian

0 Kudos

The only time I've used friend is when I wanted to encapsulate some logic, but preferred not use a subclass. I created a friend class which has everything private.

0 Kudos

Thanks chris

matt
Active Contributor
0 Kudos

Next time, please try it, before just asking questions that you can easily resolve without wasting anyone's time.