There is need to compare two objects based on class they implement? When to compare using getClass()
and when getClass().getName()
?
Is there any difference between this approaches to compare two Objects class types (names)?
public abstract class Monster { ... }
public class MonsterTypeOne extends Monster { ... }
public class MonsterTypeTwo extends Monster { ... }
Monster monster = MonsterTypeOne();
Monster nextMonster = MonsterTypeTwo();
if(nextMonster.getClass().getName().equals(monster.getClass().getName()) )// #1
if(nextMonster.getClass().equals(monster.getClass()) )// #2
EDIT 1
What about: ?
nextMonster.getClass().equals(MonsterTypeOne.class)