Alex Rivera | Logout

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

Asked 2010-06-03T09:36:21.600
45

I've noticed that the following snippet...

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

...is not allowed for an Enum, since the method equals(Object x) is defined as final in Enum. Why is this so?

I cannot think of any use case which would require overriding equals(Object) for Enum. I'm just curious to know the reasoning behind this behavior.

Edit
Report

1 Answer

0

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.

answered 2010-06-03T09:49:55.780

Your Answer