Altitude RTTI (real-time authentication) is considered a code smell by some people, there are two alternatives: one should use the instanceof operator:
if(value instanceof MyClass)
On the other hand, you can use the full-fledged method from the Class Class , which defines two objects, you can determine whether they belong to the same hierarchy (much more powerful than instanceof IMO):
if(value.getClass().isAsignableFrom(getClass()))
This second approach determines whether the value is the same class or superclass / superinterface of the current class ( this ) at runtime specified by any kind of object. This isAsignableFrom stands out, since with instanceof you need to know the reference type at compile time.
source share