Why Java does not allow overriding equals(Object) in an Enum?

前端 未结 5 1504
一整个雨季
一整个雨季 2020-12-03 04:18

I\'ve noticed that the following snippet...

@Override
public boolean equals(Object otherObject) {
    ...
}

...is not allowed for an Enum,

5条回答
  •  孤街浪徒
    2020-12-03 05:19

    I must confess enums are the last thing I would want to override equals() in.

    I think the reason equals() is final in enums is that Java encourages == for enum comparison, and the implementation of equals() in enums simply uses it, So allowing equals() from being overridden is to prevent == and equals() from behaving differently, which is something other developers would not expect.

提交回复
热议问题