Why would an Enum implement an Interface?

前端 未结 16 2713
长发绾君心
长发绾君心 2020-11-28 01:34

I just found out that Java allows enums to implement an interface. What would be a good use case for that?

16条回答
  •  渐次进展
    2020-11-28 01:55

    Enums are just classes in disguise, so for the most part, anything you can do with a class you can do with an enum.

    I cannot think of a reason that an enum should not be able to implement an interface, at the same time I cannot think of a good reason for them to either.

    I would say once you start adding thing like interfaces, or method to an enum you should really consider making it a class instead. Of course I am sure there are valid cases for doing non-traditional enum things, and since the limit would be an artificial one, I am in favour of letting people do what they want there.

提交回复
热议问题