“instanceof” is a tool like the rest of java. Use it where it makes sense. Deprecating would be a mistake. Implementing the equals() method would be very annoying without it
However, surely the real solution is not to check the type of the object externally at all. Wherever you decide you need to check the type externally and perform some type specific functionality, put that functionality where it actually belongs - in the class in question (i.e. type specific functionality for maintainability should be kept with the specific type :))
Poor example:
Interface Fruit. Two implementations Apple and Orange. Using instanceof (or any other external type check) you might have done:
public void eatSomeFruit(Fruit fruit) {
if (myFruit instanceOf Apple) {
System.out.println("Yum Yum!");
} else if (myFruit instanceof Orange) {
System.out.println("Yucky!");
}
The response is specific to the type so put it where it should be, with the type. So we’d add eat() to the interface Fruit and implement appropriately in the types.
public interface Fruit {
public void eat();
}
public class Apple implements Fruit {
public void eat() {
System.out.println("Yum Yum!");
}
}
public class Orange implements Fruit {
public void eat() {
System.out.prinltn("Yucky!");
}
}
and finally our icky instanceof utility fruit eating method becomes:
public void eatSomeFruit(Fruit fruit) {
fruit.eat();
}
HTH,
Kev