Darryl said:
True, though I feel confident, again, in saying that if it's necessary to
specifically cast a null object then that would indicate to me that a
no-arg version of the method is required.
I'll give a specific example, then, of a time when I decided (must have
been a few years ago) to design an API where casting null was required.
The interface (obfuscated a little because I know the prior client
wouldn't want the real code with identifying content made public) looked
something like this:
class Gadget { ... }
class FooGadget extends Gadget { ... }
class BarGadget extends Gadget { ... }
class BazGadget extends Gadget { ... }
class GadgetContainer
{
public void add(FooGadget g) { ... }
public void add(BarGadget g) { ... }
public void add(BazGadget g) { ... }
}
The three methods could also be called with a casted 'null' reference,
which actually had meaning in this context... it meant that although no
specific such gadget was being added, that we wanted to know that there
was such a type of gadget added that nevertheless wasn't being tracked
by the system. So we'd do a 'container.add((FooGadget) null)'.
Sure, there were other ways to write that. That one seemed to make
sense at the time. Even if it's not an ideal solution, it seems a far
cry from the kind of silly, pointless code that's otherwise been
discussed (such as your example of upcasting parameters to HashMap.put).
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation