Ideal to avoid this, but sometimes necessary.
Using instanceof may interfere with the Open / Closed ("O" principle in SOLID). If you are implementing instanceof tests, then your class may need to be changed with new implementation classes.
However, sometimes it is necessary. For example, it can be used in implementations of the Object.equals() method. An argument is an object - so the method can be overridden by arbitrary subclasses - but you usually need to classify it as your class type for comparison.
source share