10-23-2014 5:27 AM
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
10-23-2014 7:36 AM
"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.
10-23-2014 6:21 AM
10-23-2014 7:36 AM
"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.
10-23-2014 7:52 AM
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?
10-23-2014 8:02 AM
a parent class doesn't know who are their child sub class.
therefore i don't think it is possible.
10-23-2014 9:14 AM
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.
10-23-2014 9:53 AM
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.
10-23-2014 10:04 AM
This is entirely as expected. I'm curious, why did you post the question instead of just trying it?
10-23-2014 10:30 AM
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.
10-23-2014 10:33 AM
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.
10-23-2014 11:44 AM
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
10-23-2014 12:05 PM
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.
10-23-2014 12:11 PM
10-23-2014 10:49 AM
Next time, please try it, before just asking questions that you can easily resolve without wasting anyone's time.