Why is compareTo on an Enum final in Java?

后端 未结 5 742
感动是毒
感动是毒 2020-12-07 23:36

An enum in Java implements the Comparable interface. It would have been nice to override Comparable\'s compareTo method, but here it\

5条回答
  •  -上瘾入骨i
    2020-12-08 00:14

    One possible explanation is that compareTo should be consistent with equals.

    And equals for enums should be consistent with identity equality (==).

    If compareTo where to be non-final it would be possible to override it with a behaviour which was not consistent with equals, which would be very counter-intuitive.

提交回复
热议问题