Wednesday, 27 October 2010

Enum that implements interfaces

Enum can implement any interfaces. All enum types implicitly implements java.io.Serializable, and java.lang.Comparable.
public enum Color implements Runnable {
WHITE, BLACK, RED, YELLOW, BLUE;

public void run() {
System.out.println("name()=" + name() +
", toString()=" + toString());
}
}
A sample test program to invoke this run() method:
for(Color c : Color.values()) {
c.run();
}
Or,
for(Runnable r : Color.values()) {
r.run();
}

No comments:

Post a Comment