Downcasting - whats the problem?

From: Object Developer (ukobjectdeveloper_at_yahoo.co.uk)
Date: 05/16/04


Date: 16 May 2004 04:39:55 -0700

I would like to ask what will be a very basic question for many on
this list, so I hope those people offer some helpful advice on this.
I would like to understand what the real problem is with downcasting
in a staticly typed language. I know about the OCP, and I understand
the reasons for why this type of thing is wrong:

public SomeMethod(SomeObject obj)
{
        if(obj is typeof(FooClass))
        {
        ///
        }
        else if(obj is typeof(BarClass))
        {
        ///
        }
        etc...
}

What I cannot understand is where else downcasting is not preferred,
and why it is "bad design", which is what I seem to think most people
think it is on here. I recently read some back-posts on this topic,
and Mr Robert Martin states a valid need to apply downcasting (Vehicle
and Operator thread). However some people continued to state that
downcasting is something to avoid, even in these situations (though I
personally failed to see how you would get around it).

I am still learning object oriented development, and I am sure there
are other lurkers who would like to know some of the things that are
not said or explained. The reason for downcasting being bad design is
one of them, certainly for me.

Thanks

uod.



Relevant Pages

  • Re: Downcasting - whats the problem?
    ... That eases maintenance and extension for new ... > I would like to understand what the real problem is with downcasting ... > the reasons for why this type of thing is wrong: ... > and Mr Robert Martin states a valid need to apply downcasting (Vehicle ...
    (comp.object)
  • Re: Downcasting - Is it bad design
    ... >> downcasting a returned object. ... >> I wonder if it is the design which made me downcast the returned object ... additional methods in a derived class API. ... to this kind of OO basics 101, single interface, Circle, ...
    (comp.object)
  • Re: Re: Downcasting - whats the problem?
    ... >> Patterns of Enterprise Application Architecture, by Martin Fowler. ... > appropriate use of downcasting, then darn near 80% of all OO programs ... > type tree base class API, in most OO program type trees is the correct ... > gen/spec anchored in commonality are the lifeblood of OO design. ...
    (comp.object)
  • Re: Downcasting - Is it bad design
    ... > the Class (which I used for downcasting). ... > I wonder if it is the design which made me downcast the returned object ... reference in the message data packet. ... Client2, though, can access only a C through the relationship. ...
    (comp.object)
  • Downcasting - Is it bad design
    ... > I wonder if it is the design which made me downcast the returned object ... > which in effect put an dependency to this new package. ... In these cases, the downcasting *is* bad design, but in the language ...
    (comp.object)