There is need to compare two objects based on class they implement? When to compare using getClass()
and when getClass().getName()
?
Is there any differ
As a matter of fact comparing trusted class by name is a weakness. Refer https://cwe.mitre.org/data/definitions/486.html
if (nextMonster.getClass() == monster.getClass())
is the correct way to compare classes
if (nextMonster.getClass().equals(monster.getClass()))
should still work for the same reason @Bohemian mentioned