VB man goes Java. Casting..

From: Alvin Ryder (alvin321_at_telstra.com)
Date: 05/25/04


Date: 24 May 2004 19:31:09 -0700

Hi,

I'm a VB man gone Java, sometimes I write code like

public aMethod( Object o )
{
   String error = ""

   if o instanceof X
     ProcessX (o)

   else if o instanceof Y
     ProcessY (o)

   else if o instanceof Z {
     int mode = 2
     ProcessX (o, mode) // Similar to X but a little different
   }
   else if ... about 10 of these
     Process
   else
     error = "Error - unknown type " + o.getClass().getName()
}

But my OO-guru friend, in polite words, says this is crap.

He says do
  Base o = new X()
  o.process()

instead of
  ProcessX (o)
  ProcessY (o)

Should I explore that idea?

I am not interested in the exceptional, extreme, fance, guru, or
overly-testosterone cases I just want to know what is the *basic main
line majority of cases OO way*?

Yes I know there's another downcasting thread on this NG, but it's way
too advanced. Can we *please* keep this thread simple and acrnonym
free :-) No I'm not a moron, I'm just not familiar with these
particular concepts.

Thanks in advance
Alvin



Relevant Pages

  • Re: VB man goes Java. Casting..
    ... > I'm a VB man gone Java, ... > public aMethod(Object o) ... > ProcessX ... > else if o instanceof Y ...
    (comp.object)
  • Re: Is instanceof dirty?
    ... > you want to switch on type information. ... when instanceof is unavoidable. ... extend an existing class structure with the visitor pattern. ... in Java, is multi-methods like those found in Nice, ...
    (comp.lang.java.programmer)
  • Re: Is instanceof dirty?
    ... >>The one place where I find myself frequently using instanceof in Java is ... > anything specifically odd about the Java equals() method. ... > avoids the potential problem, or at least ambiguity, with subclasses). ...
    (comp.lang.java.programmer)
  • Re: primitive wrapper classes
    ... processes the argument differently depending on its actual ... divided up by `instanceof' tests or equivalents. ... to enumerate all subclasses of Number. ... succeeding Java versions. ...
    (comp.lang.java.programmer)
  • Re: VB man goes Java. Casting..
    ... >>I'm a VB man gone Java, ... >> ProcessX ... >> else if o instanceof Y ... > of OO is to eliminate type-case statements. ...
    (comp.object)