How to override equals method in Java

后端 未结 9 1573
自闭症患者
自闭症患者 2020-11-22 01:41

I am trying to override equals method in Java. I have a class People which basically has 2 data fields name and age. Now I want to ove

9条回答
  •  粉色の甜心
    2020-11-22 02:20

    Introducing a new method signature that changes the parameter types is called overloading:

    public boolean equals(People other){
    

    Here People is different than Object.

    When a method signature remains the identical to that of its superclass, it is called overriding and the @Override annotation helps distinguish the two at compile-time:

    @Override
    public boolean equals(Object other){
    

    Without seeing the actual declaration of age, it is difficult to say why the error appears.

提交回复
热议问题