tags:

views:

452

answers:

8

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

+2  A: 

Enums don't just have to represent passive sets (e.g. colours). They can represent more complex objects with functionality, and so you're then likely to want to add further functionality to these - e.g. you may have interfaces such as Printable, Reportable etc. and components that support these.

Brian Agnew
+1  A: 

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.

TofuBeer
A: 

It's required for extensibility -- if someone uses an API you've developed, the enums you define are static; they can't be added to or modified. However, if you let it implement an interface, the person using the API can develop their own enum using the same interface. You can then register this enum with an enum manager which conglomerates the enums together with the standard interface.

Edit: @Helper Method has the perfect example of this. Think about having other libraries defining new operators and then telling a manager class that 'hey, this enum exists -- register it'. Otherwise, you'd only be able to define Operators in your own code - there'd be no extensibility.

Chris Dennett
A: 

Since Enums can implement interfaces they can be used for strict enforcing of the singleton pattern. Trying to make a standard class a singleton allows...

  • for the possibility of using reflection techniques to expose private methods as public
  • for inheriting from your singleton and overriding your singleton's methods with something else

Enums as singletons help to prevent these security issues. This might have been one of the contributing reasons to let Enums act as classes and implement interfaces. Just a guess.

See http://stackoverflow.com/questions/427902/java-enum-singleton and http://stackoverflow.com/questions/2111768/singleton-class-in-java for more discussion.

Tansir1
+1  A: 

Here's one example (a similar/better one is found in Effective Java 2nd Edition):

public interface Operator {
    int apply (int a, int b);
}

public enum SimpleOperators implements Operator {
    PLUS { int apply(int a, int b) { return a + b; }
    MINUS { int apply(int a, int b) { return a - b; }
}

public enum ComplexOperators implements Operator {
    // can't think of an example right now :-/
}

Now to get a list of both the Simple + Complex Operators:

List<Operator> operators = new ArrayList<Operator>();

operators.addAll(Arrays.asList(SimpleOperators.values()));
operators.addAll(Arrays.asList(ComplexOperators.values()));

So here you use an interface to simulate extensible enums (which wouldn't be possible without using an interface).

Helper Method
+1  A: 

You can do much more with enums. You can add methods and constructor arguments to them. You may implement Comparable to your enum to compare them. Also, enums are classes, so interface implementations should be allowed.

enum Planet implements Comparable<Planet> {
  EARTH(1),
  MARS(3); // ... and so on

  public final float distance;
  Planet(float distance) {
    this.distance = distance;
  }
  // compare distances
  public int compareTo(Planet other) {
    return this.distance - other.distance;
  }
  // sorry, I didnt check my code
}
SHiNKiROU
No. The `Enum` base class already implements `Comparable`, and you can't override that implementation.
Jorn
+2  A: 

The Comparable example given by several people here is wrong, since Enum already implements that. You can't even override it.

A better example is having an interface that defines, let's say, a data type. You can have an enum to implement the simple types, and have normal classes to implement complicated types:

interface DataType {
  // methods here
}

enum SimpleDataType implements DataType {
  INTEGER, STRING;

  // implement methods
}

class IdentifierDataType implements DataType {
  // implement interface and maybe add more specific methods
}
Jorn
+1  A: 

For example if you have a Logger enum. Then you should have the logger methods such as debug, info, warning and error in the interface. It makes your code loosely coupled.

Johan