Java Reference
In-Depth Information
You can also nest an enum type inside another enum type or an interface. The following are valid enum type
declarations:
public enum OuterEnum {
C1, C2, C3;
public enum NestedEnum {
C4, C5, C6;
}
}
public interface MyInterface {
int operation1();
int operation2();
public enum AnotherNestedEnum {
CC1, CC2, CC3;
}
}
Implementing an Interface to an Enum Type
An enum type may implement interfaces. The rules for an enum type implementing an interface are the same as the
rules for a class implementing an interface. An enum type is never inherited by another enum type. Therefore, you
cannot declare an enum type as abstract. This also implies that if an enum type implements an interface, it must also
provide implementation for all abstract methods in that interface.
The program in Listing 18-8 declares a
Command
interface. The program in Listing 18-9 declares an enum type
called
CommandList
that implements the
Command
interface. Each enum constant implements the
execute()
method
of the
Command
interface. Alternatively, you can implement the
execute()
method in the enum type body and omit
the implementations from some or all enum constants. Listing 18-10 demonstrates using the enum constants in the
CommandList
enum type as
Command
type.
Listing 18-8.
A Command Interface
// Command.java
package com.jdojo.enums;
public interface Command {
void execute();
}
Listing 18-9.
A CommandList Enum Type Implementing the Command Interface
// CommandList.java
package com.jdojo.enums;
public enum CommandList implements Command {
RUN {