suppose I have some entities classes hierarchy , and I have a util class with a method that get the upper class hierarchy and has to to some change in another entity class (out of the previous hierarchy) according to the specific class that I got . So I will use instanceof in order to find what specific object I got. But I know : Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself. so how can I do it without using instanceof ?
Please notice that I do not want to put the util method in the entities classes because I want the users (the implementers programmers) to be able to implement the method but I do not want them to be able to change the entities.