Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Dumb instanceof question

Reply
Thread Tools

Re: Dumb instanceof question

 
 
Paul Tomblin
Guest
Posts: n/a
 
      07-11-2003
In a previous article, said:
>Is instanceof true for a superclass?
>
>So, if I have a Class MyClass and also MyClassSubClass, and I have an
>instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?


Yes.



--
Paul Tomblin <>, not speaking for anybody
"The only thing that interferes with my learning is my education."
-- Albert Einstein.
 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      07-11-2003

"Paul Tomblin" <> wrote in message
news:ben2ln$drf$...
> In a previous article, said:
> >Is instanceof true for a superclass?
> >
> >So, if I have a Class MyClass and also MyClassSubClass, and I have an
> >instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?

>
> Yes.


Likewise, if MyClassSubClass implements MyInterface, then (mcsc instanceof
MyInterface ) is also true.


 
Reply With Quote
 
 
 
 
Chris Rehm
Guest
Posts: n/a
 
      07-11-2003
Mike Schilling wrote:
> "Paul Tomblin" <> wrote in message
> news:ben2ln$drf$...
>>>So, if I have a Class MyClass and also MyClassSubClass, and I have an
>>>instance of MyClassSubClass mcsc, is (mcsc instanceof MyClass) true?

>>Yes.

> Likewise, if MyClassSubClass implements MyInterface, then (mcsc instanceof
> MyInterface ) is also true.


Thanks guys. When I first read instanceof, I didn't understand. Now I
have an embarrassing amount of really crappy code to go clean up.

--
Chris Rehm


Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
Reply With Quote
 
Marco Schmidt
Guest
Posts: n/a
 
      07-11-2003
Chris Rehm:

>Thanks guys. When I first read instanceof, I didn't understand. Now I
>have an embarrassing amount of really crappy code to go clean up.


If you use instanceof very often there is a good chance you could
improve the design of your program. Is there any typical situation
where you would check objects with instanceof?

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
Chris Rehm
Guest
Posts: n/a
 
      07-11-2003
Marco Schmidt wrote:
> If you use instanceof very often there is a good chance you could
> improve the design of your program. Is there any typical situation
> where you would check objects with instanceof?


Well, I guess I can risk a little humiliation to gain a little
education, so here goes:

I have a "primary" Class, with two branches of sub-classes:

MyClassBase

MyClassBaseSquare MyClassBaseNSquare
MyClassBaseSquareX MyClassBaseNSquareA

They are all related objects (um, I suppose you'd guess that from the
superclass) and I have various reasons for loading up Vectors with lots
of MyClass objects. But NSquare objects have different methods that need
to be run.

A specific example is that I have a Vector holding lots of MyClassBase.
I want to create a descriptive list of only the Square, none of the
NSquare. Plus, I need to run a method on all the NSquares, that does not
exist on the Squares.

So, when I get there I have been using a lot of
getClass().getName().indexOf("NSquare)<0 to figure out if I can cast.

I think I might have been better off if I'd done a better job of
designing the classes before I started, but I figured out too much of
what was going to happen after I got going.

>
> Regards,
> Marco


--
Chris Rehm


Thou shalt not avenge, nor bear any grudge against the children of thy
people, but shalt love thy neighbour as thyself. [Lev. 19:18]

 
Reply With Quote
 
Paul Tomblin
Guest
Posts: n/a
 
      07-11-2003
In a previous article, said:
>I think I might have been better off if I'd done a better job of
>designing the classes before I started, but I figured out too much of
>what was going to happen after I got going.


You got that right. Instead of doing "instanceof", deciding if you can
cast, and then calling the method, make a no-op method in the base class,
and call it on all of the instances.


--
Paul Tomblin <>, not speaking for anybody
"He passed away during an important civic function held in his honor when the
platform upon which he was standing collapsed." "I thought he was hanged?"
"That's what I said, isn't it?"
 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      07-12-2003
"Chris Rehm" <> wrote in message
news:benbpv$nf4$...
>I think I might have been better off if I'd done a better job of
>designing the classes before I started, but I figured out too much of
>what was going to happen after I got going.


Often the way I find.

If we knew the language like the back of our (my) hand then there'd be less
excuse, but it always seems to be a many (one heck of a lot) tangents/fact
finding process. Design>try>learn language>learn pattern>try>redesign etc.

Then when you've gone full circle, 1.4 comes out with a lovely neat package
that cuts out a month of previous learning, frustrating business sometimes!

--
Mike W


 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      07-12-2003
Chris Rehm wrote:
> Marco Schmidt wrote:
>
>> If you use instanceof very often there is a good chance you could
>> improve the design of your program. Is there any typical situation
>> where you would check objects with instanceof?


Actually, I use instanceof for validity checking and also flow-of-
control when using a Struts Action which can be fed by multiple
ActionForms.
If I have a daemon which has to respond to multiple input types,
typically through an ObjectInputStream chained to a Socket then
the following code:

if( obj instanceof x ) {
X x = (X) obj;
// process
}
if( obj instanceof Y ) {
Y y = (Y) obj;
// process
}

is preferable to:

try {
X x = (X) obj;
// process
}
catch( ClassCastException e ) {
}
try {
Y y = (Y) obj;
// process
}
catch( ClassCastException e ) {
}

A common mistake seen is newbie code is to even nest these
tests! So instanceof can be your friend in many situations,
especially when you're using an object interface which offers
no guarantees of the class.

 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      07-12-2003
In news:beo7q1$qku$,
Chris Rehm <> spoke unto us:
> VisionSet wrote:
>> Then when you've gone full circle, 1.4 comes out with a lovely neat
>> package that cuts out a month of previous learning, frustrating
>> business sometimes!

>
> Frustrating sometimes? Yes. But since I started programming back in
> the 70s, I've never had as much fun as I do with Java. I don't know
> why, but
> I just find it _fun_ to code in this language.
>
> It's like the language embodies all the things I've always tried to do
> in my coding.


Yes, but I believe that what you like is the language, and not the libraries
(package set), which is what VisionSet was refering to.

And I totally agree: Java has done an amazingly number of things right.

And what I find very interesting as well: in comp.object, where people are
interested in language non-specific OO issues, the pseudo code they end up
posting as examples almost always looks exactly like java. FWIW.



 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      07-12-2003
In news:beofsk$ol5$,
Chris Rehm <> spoke unto us:
> Sudsy wrote:
>> A common mistake seen is newbie code is to even nest these
>> tests! So instanceof can be your friend in many situations,
>> especially when you're using an object interface which offers
>> no guarantees of the class.

>
> I'm in full agreement that instanceof is orders of magnitude faster
> than try catch around a cast. But when I'm using instanceof I sort of
> feel (usually) like I must have designed something wrong if I have to
> know what kind of class I'm dealing with right here in my code.
>
> It's like a goto in C. In all the time I wrote C code, I never used a
> goto. Whenever I am thinking I want to branch like that, I am pretty
> sure I must have not conceptualized my code right in the first place.
> That's how I feel when I need to check what kind of object I have.
>
> Does that make sense? That doesn't mean I know I'm right about it,
> just that's the area where I feel self conscious.


You are half right, and that half is right on the money. I use
instanceof, but c.a.r.e.f.u.l.l.y. In the case of the prior poster example,
he is using instance of as a receiving mechanism for a form of dispatch,
which is fine in my opinion.

The problem with instanceof is that it can be grossly overused to give a
java programmer a way around having to implement proper OO design. This is
the source of many an thread in comp.object with subjects of the form "Is
RTTI really all that bad?"

OO purists almost always want RTTI abolished completely. But I, as you,
liken that to the anti-goto nazi's. I believe that the proper mindset WOULD
be parallel to java's named-break: allow downward goto's only and only out
of code blocks. I believe that instanceof is a little too overused, but I
don't just don't know of a way of providing such functionality without
allowing the abuse.

Thomas


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb, Dumb Vista Au79 Computer Support 4 02-11-2007 03:40 PM
instanceof NOT (always) bad? The instanceof myth. dmx_dawg@hotmail.com Java 21 07-20-2006 07:06 PM
instanceof question Dotty Java 2 02-16-2005 05:15 AM
Dumb, dumb dumb Qestion David Napierkowski Digital Photography 6 10-31-2004 11:14 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57