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: 

Variable assignment inconsistency

Former Member
0 Kudos
92

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos
52

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.

4 REPLIES 4

Former Member
0 Kudos
53

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.

0 Kudos
52

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

0 Kudos
52

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

0 Kudos
52

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