This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 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