aspose file tools*
The moose likes Beginning Java and the fly likes regarding instanceof operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding instanceof operator" Watch "regarding instanceof operator" New topic
Author

regarding instanceof operator

Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Hi all,
I had just seen somewhere that using the instanceof operator is not advised. I'd like to know the reason why it shouldn't be used.
Regards,
Naresh
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Here's a typical use of instanceof that's problematic.If this is an open-ended hierarchy, then as more classes extend A, you'll have to find the code like method f throughout your program and add more cases. A better solution in this case would be if there were a polymorphic method doF() that you could invoke:This doesn't mean that you should never use instanceof. There are places where it's unavoidable.


There is no emoticon for what I am feeling!
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
There are times when you just cannot avoid using instanceof, like when you override Object.equals or Comparable.compareTo. Sometimes the abstraction and contract just won't let you be specific when taking a parameter.


Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
 
 
subject: regarding instanceof operator