Java OOPS getClass() method

Hi Mosh,

In Java OOPS course, whenever equals method() is overridden using IntelliJ, this line
(if (o == null || getClass() != o.getClass()) return false;) is generated. I would like to why they are comparing getClass() values using != instead of equals method, because whenever String is compared using == or !=, it might give an incorrect result right. Please explain this @programmingwithmosh @SAM !!

Note: the return type of getClass() is runtime class of the object.

Class doesn’t override equals() so it inherits the default behaviour of equals() from Object - which is returning reference equality. So instead of calling equals() on a Class object we can directly do the reference comparison the method call would do anyway. This is perfectly safe since there only can be a single Class object per class in the system.

@SAM thank you for the reply. Got it