#
Enum implements an Interface (I)
This tutorial explains to you how we can implement an interface in an Enum in Java.
Enums are used to represent a group of named constants. An enum type has a fixed set of values.
In Java, Enums are static
and final
and are defined by using "enum" keyword.
In this case, could an enum type implement an interfaces ?
YES, it is possible. If an enum type implements an interface, it must also provide implementation for all abstract methods from the interface.
Additional things regarding an enum type:
- An enum type is never inherited by another enum type.
- You cannot declare an enum type as abstract.
The enum type will still be an enum type, however when implements an interface, this enum will also have a method defined for each value in the enum. That method could be defined at the enum type level (as in the example below) or for each value (see Enum implements an Interface (II)).
In the example below we will define a list of fixed values (using an enum type) which keeps the parameters for a particular method call. This pattern is useful when we have a method which is called only using a limited number of predefined parameters.
My example is using Spring Boot, and it is created by using Spring Initializr.
Here are the code I created/updated:
- The interface:
package com.example.operation;
public interface IMyOperation {
public void execute();
}
- The enum type:
package com.example.operation;
public enum MyOperation implements IMyOperation {
PAINT ("Paint the car", "PTC", 5),
FIND ("Find the car", "FTC", 1);
private final String operationName;
private final String operationCode;
private final int durationInHours;
MyOperation(String operationName, String operationCode, int durationInHours) {
this.operationName = operationName;
this.operationCode = operationCode;
this.durationInHours = durationInHours;
}
@Override
public void execute() {
System.out.println("We are executing ..."+operationName+".");
System.out.println("The maximum time allowed for this operation is "+durationInHours+" hours.");
}
}
- The usage:
package com.example;
import com.example.operation.MyOperation;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringApplication {
public static void main(String[] args) {
org.springframework.boot.SpringApplication.run(SpringApplication.class, args);
System.out.println("Here starts the Spring application !");
MyOperation.PAINT.execute();
MyOperation.FIND.execute();
}
}
When we run the application we can see on the console:
Here starts the Spring application !
We are executing ...Paint the car.
The maximum time allowed for this operation is 5 hours.
We are executing ...Find the car.
The maximum time allowed for this operation is 1 hours.
Process finished with exit code 0
- As we can see the
MyOperation.PAINT.execute()
command will run theexecute()
method using the parameters received from "MyOperation.PAINT". - This design pattern is useful when we have a limited predefined calls for the
execute()
method.