The equals(Object) Method in Java

In Java, the default notion of equality on objects is defined by an equals method, which accepts any Object as its argument and returns a boolean indicating whether that object should be considered equal to this object.

If you don't define an equals(Object) method in a Java class, then it will inherit an equals method from its superclass.

In the Java classes we have defined so far, we have not specified a superclass. When no superclass is specified, the superclass is Object. When objects of a class are immutable, as has been true for all of the classes we have defined so far, the equals method that would be inherited from the Object class is almost certainly inappropriate.

That means we need to define an equals method in our classes.

For debugging: Click here to validate.