While implementing a new feature recently, I needed to create a subclass of another concrete class. In the process, I happened to look for references to the superclass. What I found surprised me. There were various places where the code was explicitly testing the class or the class hierarchy of an object. In some cases, the code was explicitly performing a class test and in others it was using isKindOf to check the class hierarchy. I have two problems with this approach. First, it breaks your intuition that any class can be used interoperably with another so long as it implements all the appropriate methods. If you're doing a class or a class hierarchy test, you restrict the class to be in the hierarchy. Second, by explicitly testing against a superclass, it's less likely that developers would think to look for that and would have to run into the problem by accident. It's better to implement methods to test if an object can be used in a certain way or better still, just tell the object what to do without requiring a test.
Download