If I have two objects as follows…
ClassA classAInstance = ...
ClassB classBInstance = ...
… where ClassB is a direct sub-class (extension) of ClassA and the actual object assigned to ‘classAInstance’ and ‘classBInstance’ can be an instance of ClassA, ClassB or a direct or indirect sub-class of ClassA…
How do I then check whether the object assigned to each of ‘classAInstance’ and ‘classBInstance’ is of the same Class? Is the following correct…
classAInstance.getClass().equals(classBInstance.getClass());
… or is it some other property that I should be comparing?
No, that’s the way to do it…assuming it’s the right thing to do. Of course, as with most reflection, comparing classes at all is pretty fragile, if you’re expecting to do any refactoring of how
ClassB‘s hierarchy works.