java why should equals method input parameter be Object

后端 未结 6 923
别那么骄傲
别那么骄傲 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:01

    equals is a method inherited from Object, is defined to be flexible enough so that you can take any object and test if it is equal to any other object (as it rightfully should be able to do), so how could it be any other way?

    Edit 1

    Comment from jhlu87:
    so is it not good form to write an equals method that has an input parameter of vertex?

    You are welcome to create your own overload to any method, including equals, but doing so without changing the name could risk confusing many who would assume that your equals is the one that inherits from Object. If it were my code and I wanted a more specific equals method, I'd name it slightly different from just "equals" just to avoid confusion.

提交回复
热议问题