When overriding equals in Java, why does it not work to use a parameter other than Object?

后端 未结 7 1948
攒了一身酷
攒了一身酷 2020-12-03 18:13

I ran into an interesting behavior recently. It seems that if I override .equals() to take a parameter other than Object, it doesn\'t get called. Can anyone explain to me

7条回答
  •  被撕碎了的回忆
    2020-12-03 18:29

    You're mixing up "overriding" and "overloading".

    Overriding -- adding a replacement definition of an existing method for purposes of polymorphism. The method must have the same signature. The signature consists of the name and argument types. Overridden methods are selected at runtime based on the runtime type of the target object.

    Overloading -- adding a method with the same name but a different signature. Overloaded methods are selected at compile time based on the compile time type of the target object.

提交回复
热议问题