java why should equals method input parameter be Object

后端 未结 6 932
别那么骄傲
别那么骄傲 2020-12-09 10:42

I\'m going through a book on data structures. Currently I\'m on graphs, and the below code is for the vertex part of the graph.

class Vertex{
    /         


        
6条回答
  •  悲哀的现实
    2020-12-09 11:14

    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.

提交回复
热议问题