12-16-2010 9:47 AM
Hi All,
I am inheriting a standard class which has many instance protected attributes. how do i access the super classes protected attributes from the sub class. i.e i need the value present in the super class attribute to be used in a sub class method.
will i be able to get the value of an instance attribute??
Thanks in advance,
Arun.
12-16-2010 10:02 AM
Hi Arun.
From wich class are you inheriting from. Normal this should not be the problem.
Just use: me->instance_attribute
Marc,
12-16-2010 10:02 AM
Hi Arun.
From wich class are you inheriting from. Normal this should not be the problem.
Just use: me->instance_attribute
Marc,
12-20-2010 5:09 AM
Hi Marc,
The problem is Super class instance would be different, so when i access the instance attribute using my sub class instance will the attribute contain value which is present in the super class?
Thanks,
Arun.
12-20-2010 7:30 AM
Hey u can use Zid's program with small changes as given below :
CLASS s_abc DEFINITION.
PROTECTED SECTION .
DATA: d_abc TYPE i value 3.
ENDCLASS. "s_abc DEFINITION
CLASS sb_abc DEFINITION INHERITING FROM s_abc.
public SECTION.
DATA: sb_d_abc TYPE c." VALUE d_abc.
METHODS: m_abc.
ENDCLASS. "sb_abc DEFINITION
CLASS sb_abc IMPLEMENTATION.
METHOD m_abc.
me->d_abc = me->d_abc + 2.
write :'Superclss attribute' , me->d_abc .
ENDMETHOD. "m_abc
ENDCLASS. "sb_abc IMPLEMENTATION
data : obj_a type ref to sb_abc .
start-of-selection .
create object obj_a .
obj_a->m_abc( ) .
12-20-2010 9:41 AM
Hi,
The problem can be solved by Wide casting. Take a pointer to the sub class, Then using narrow cast operator point the pointer to the super class. Then you can get the value of the attribute of the super class.
12-20-2010 11:10 AM
>
> Hi,
> The problem can be solved by Wide casting. Take a pointer to the sub class, Then using narrow cast operator point the pointer to the super class. Then you can get the value of the attribute of the super class.
It's like getting to your house through basement. Why reinventing own techniques if there is standard one, more direct. Subclass can always use its superclass protected attributes directly, this is why protected section was ever invented for!
Regards
Marcin
12-20-2010 11:15 AM
It's like getting to your house through basement.
Marcin,
That sounds funny ))
12-20-2010 11:39 AM
Well, I think due to approaching Christmas I should say through chimney, but it is probably a task for Santa;)
12-20-2010 11:53 AM
Marcin, I think your comment is a nice conclusion to this (basic) discussion.
12-20-2010 12:16 PM
"Subclass can always use its superclass protected attributes directly, this is why protected section was ever invented for!"
Hi Marcin,
I dont have a problem accessing the protected instance attribute but my only problem is will i be able to access the value of instance attribute of superclass in the sub class.
In my scenario there is a method in super class which returns a table. I want to use the values in that table in my sub class.
Edited by: A.ARUN PRAKASH on Dec 20, 2010 1:23 PM
12-20-2010 2:19 PM
I dont have a problem accessing the protected instance attribute but my only problem is will i be able to access the value of instance attribute of superclass in the sub class.
Don't really know what you mean here. Accessing intance attribute is nothing but accessing its value .
In my scenario there is a method in super class which returns a table. I want to use the values in that table in my sub class.
Still confused. During runtime the superclass method (i.e. get_data ) fills some table which it later returns via parameter (i.e ret_tab ). You can't access this parameter while the method get_data is being executed. You can only call it yourself (in subclass method) and get that ret_tab returned to your subclass method.
If you have some code would be nice to show where , when and what you exactly want to access.
Regards
Marcin
12-20-2010 10:01 PM
Do you mean something like this? LCL_SUPER has a method GET_T001 which returns the value of the T001. You need to hold the returning value in the temporary variable (like LT_T001 in the method PROCESS).
*
CLASS lcl_super DEFINITION.
PROTECTED SECTION.
DATA: t001_lines TYPE i.
METHODS:
get_t001 RETURNING value(rt_t001) TYPE trty_t001.
ENDCLASS.
*
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super .
PUBLIC SECTION.
METHODS: process.
ENDCLASS.
*
CLASS lcl_super IMPLEMENTATION.
METHOD get_t001.
SELECT * FROM t001 INTO TABLE rt_t001
UP TO 10 ROWS.
t001_lines = sy-dbcnt.
ENDMETHOD.
ENDCLASS.
*
CLASS lcl_sub IMPLEMENTATION.
METHOD process.
DATA: lt_t001 TYPE trty_t001.
FIELD-SYMBOLS: <lfs_t001> LIKE LINE OF lt_t001.
lt_t001 = me->get_t001( ).
WRITE: 'Total records', me->t001_lines.
LOOP AT lt_t001 ASSIGNING <lfs_t001>.
WRITE: / <lfs_t001>-bukrs.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lo_main TYPE REF TO lcl_sub.
CREATE OBJECT lo_main.
lo_main->process( ).
What you should do is: Create an attribute T_T001 in the class LCL_SUPER and populate that in the method GET_T001. Use the same attribute using ME-> in the method PROCESS.
Regards,
Naimesh Patel
12-21-2010 12:58 PM
Hi Funny,
I think you should know protected section is only accessible only to the sub class. So there is nothing funny to access a super class protected section through sub class pointer.
I need your suggestion.
Thanks in advance
Edited by: Anjan Paul on Dec 21, 2010 6:36 PM
12-21-2010 1:31 PM
With this joke, I didn't mean to offend you. Sorry for that, but the point we don't understand each other is this part
access a super class protected section through sub class pointer
This isn't true. You can access protected section of superclass only within subclass method, not using its "pointer". This would mean you want to access it externally which is not allowed (as only public section can be accessed outside the class this way). A little proof for my words
CLASS lcl_super DEFINITION.
PUBLIC SECTION.
DATA m_public_mess TYPE string VALUE 'I am public message of the super class'.
PROTECTED SECTION.
DATA m_protec_mess TYPE string VALUE 'I am protected message of the super class'.
ENDCLASS. "lcl_super DEFINITION
CLASS lcl_super IMPLEMENTATION.
ENDCLASS. "lcl_super IMPLEMENTATION
CLASS lcl_sub DEFINITION INHERITING FROM lcl_super.
PUBLIC SECTION.
METHODS: show_super_class_mess.
ENDCLASS. "lcl_sub DEFINITION
CLASS lcl_sub IMPLEMENTATION.
METHOD show_super_class_mess.
WRITE: / m_protec_mess.
ENDMETHOD. "show_super_class_mess
ENDCLASS. "lcl_sub IMPLEMENTATION
START-OF-SELECTION.
DATA: gr_super TYPE REF TO lcl_super,
gr_sub TYPE REF TO lcl_sub.
CREATE OBJECT gr_super.
"first access via super class
ULINE.
WRITE / 'SUPERCLASS'.
WRITE / gr_super->m_public_mess.
"WRITE gr_super->m_protec_mess. "error, protected attr access from outside the class
CREATE OBJECT gr_super TYPE lcl_sub.
gr_sub ?= gr_super.
"then access it via subclass
ULINE.
WRITE / 'SUBCLASS'.
WRITE / gr_sub->m_public_mess.
"write gr_sub->m_protec_mess. "error, protected attr access from outside the class
"so the only way to access it is within the subclass method (not its pointer)
ULINE.
WRITE / 'FROM SUBLASS METHOD'.
gr_sub->show_super_class_mess( ).
Try it out and don't be mad, it's Christmas time:)
Regards
Marcin
12-16-2010 10:09 AM
Arun,
You may get the values of a super class using SUPER key word
SUPER->super_inst_attribute.
Ajit.
12-16-2010 4:36 PM
All protected and public attributes lie within the same namespace are (and lifetime) down the inheritance tree, so are accessible from any subclass as if they were defined in this subclass itself. Therefore as Marc suggested you can use them directly.
Regards
Marcin
12-17-2010 7:15 AM
Hi marc,
I was also wondering as to how we can make use of the public attributes in the sub classes.as in, what the syntax we need to follow.
It'd be great if you could give a simple example with one protecetd attribute accessed in the subclass.
I've written a sample prog.
REPORT YA_TEST_OO.
----
CLASS s_abc DEFINITION
----
*
----
CLASS s_abc DEFINITION.
PRIVATE SECTION.
DATA: d_abc TYPE i.
ENDCLASS. "s_abc DEFINITION
----
CLASS sb_abc DEFINITION
----
*
----
CLASS sb_abc DEFINITION INHERITING FROM s_abc.
public SECTION.
DATA: sb_d_abc TYPE c." VALUE d_abc.
METHODS: m_abc.
ENDCLASS. "sb_abc DEFINITION
----
CLASS sb_abc IMPLEMENTATION
----
*
----
CLASS sb_abc IMPLEMENTATION.
METHOD m_abc.
sb_d_abc = d_abc + 1.
ENDMETHOD. "m_abc
ENDCLASS. "sb_abc IMPLEMENTATION
It doesn't work though.
Thanx,
Zid.
12-17-2010 7:44 AM
Zid,
It's not working because d_abc is private attribute in super class, not protected one. Simply change its section and it will work.
Regards
Marcin
12-17-2010 9:25 AM
04-05-2011 12:53 PM
Thanks Guys, Yupp Subclass can access Super classes attributes. The Attributes are shared when inherited to all the inherting classes.