Implement interface for enum type in Java
Description
The following code shows how to implement interface for enum type.
Example
/*from w ww .ja v a2 s. co m*/
public class Main {
public static void main(String[] args) {
Size[] sizes = Size.values();
for (Size s : sizes) {
System.out.println(s);
}
}
}
enum Size implements Countable {
S, M, L, XL, XXL, XXXL;
@Deprecated
public Size increase() {
Size sizes[] = this.values();
int pos = this.ordinal();
if (pos < sizes.length - 1)
pos++;
return sizes[pos];
}
}
interface Countable {
public Size increase();
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »