java why should equals method input parameter be Object

@Override
public boolean equals(Object obj)
{
     if (!(obj instanceof Vertex)) return false;
     else return // blah blah
}

equals(Object) is the method defined in the root - Object. If you don't match the signature exactly, Object's version will be called when someone checks if two objects are equal. Not what you want.

You've probably seen other methods (like Comparator) where you can use the exact time. That's because those APIs were generic-ified with Java 5. Equals can't be because it is valid to call equals with two separate types. It should return false, but it is valid.

Tags:

Java

Types

Equals