‎2010 Nov 26 7:04 PM
Hi expers
I have have a question about variable assigement
Here are my variables declared.
data: a_sofm_document TYPE REF TO cl_document_bcs,
an_if_document TYPE REF TO if_document_bcs,
This portion of code works perpectly
a_sofm_document = cl_document_bcs=>create_from_text( i_text = xt i_subject = subject )
an_if_document = a_sofm_document.
Try to assign back from an_if_document
a_sofm_document = an_if_document . " Doest not work and generate error when compiling
Somebody understand why ?? and How to obtain a_sofm_document back?
Thank you
‎2010 Nov 27 10:21 AM
an_if_document of type if_document_bcs is a superior type to cl_document_bcs
Thise means you need to downcast:
a_sofm_document ?= an_if_document
but ths is not a good programming practice. From OO perspective you should never need to do something like this.
‎2010 Nov 27 10:21 AM
an_if_document of type if_document_bcs is a superior type to cl_document_bcs
Thise means you need to downcast:
a_sofm_document ?= an_if_document
but ths is not a good programming practice. From OO perspective you should never need to do something like this.
‎2010 Nov 27 9:13 PM
Dominik,
but ths is not a good programming practice. From OO perspective you should never need to do something like this.
I must strongly disagree here. The basic usage is in combination with polimorphism . Imagine we have a container type as ref to some superclass. We store there a number of subclasses' references, treating them generically i.e. calling same poliphormical method for each. Now we want some specific operation to objects of only certain type. Downcasting here is ideal for such operation. This would go like
loop at me->ref_container into r_vehicle.
try.
r_plane ?= r_vehicle.
"do addtional calc. only for planes
...
catch cx_sy_move_cast_error.
endloop.
Another example would be RTTS classes where from superclass cl_abap_typedescr we derive some specialized one i.e
data r_tabledescr type ref to cl_abap_structdescr.
r_tabledescr ?= cl_abap_typedescr=>describe_by_data( itab ).
I think these proves downcasting is not only very important aspect of OO design but also let us smoothly switch b/w generalization and specialization which often is required to achieve some kind of functionality.
Any comments are welcome:)
Regards
Marcin
‎2010 Nov 27 11:48 PM
Hello Marcin,
happy to oblige
as Dominik says never without proof, my feeling is: [Absolutism|http://c2.com/cgi-bin/wiki?AbsolutismHasGreaterBurdenOfProof], he might be wrong. But..
your special code for a concrete plane class violates the [program to an interface, not an implementation|http://sourcemaking.com/design_patterns/strategy] principle. My feeling is: design smell !
Your second example should work fine without the knowledge of the subclass created by the factory method, i.e. with the definition
r_tabledescr TYPE REF TO cl_abap_typedescr.I do not see why [Liskov's Substitution|http://c2.com/cgi-bin/wiki?LiskovSubstitutionPrinciple] should not apply. So the downcasting is not needed.
my vote: neither you nor Dominik have proof.
regards,
J.N.N
‎2010 Nov 29 3:16 PM
Thanks a a lot experts, You are so helpful to me. Downcasting works for me even that I do not yet unsertand why. My time is limited and somtimes make it work is the first priority in special cirumstanses, I wish that I could attribute points to all eqauly. Will have to to do a learning curve on OO.
Again thanks a lot