Open In App

How to implement an Interface using an Enum in Java

Last Updated : 21 Jun, 2020
Like Article

Enumerations serve the purpose of representing a group of named constants in a programming language. For example, the 4 suits in a deck of playing cards may be 4 enumerators named Club, Diamond, Heart, and Spade, belonging to an enumerated type named Suit. We have already discussed the basics of enum, how its declared in the previous article. In this article, we will understand how an enum implements an interface.

We primarily use enums when a method parameter can only take the value out of a small set of possible values means the input values are fixed and it takes from that fixed set of values. Java enum type is a special kind of java class which can contain constants and methods like normal java classes where the values are the only possible instances of this class. This enum extends the abstract class java.lang.Enum. Consider a situation when we have to implement some business logic which is tightly coupled with a discriminatory property of a given object or class at that time we implement an interface with enum. Think about a case where we need to merge the values of two enums into one group and treat them similarly, there Enum implements the interface.

Since an enum is implicitly extending the abstract class java.lang.Enum, it can not extend any other class or enum and also any class can not extend enum. So it’s clear that enum can not extend or can not be extended. But when there is a need to achieve multiple inheritance enum can implement any interface and in java, it is possible that an enum can implement an interface. Therefore, in order to achieve extensibility, the following steps are followed:

  1. Create an interface.
  2. After creating an interface, implement that interface by Enum.

The following is the code which demonstrates the implementation of an interface in an enum:

// Java program to demonstrate
// how an enum implements an
// interface
// Defining an interface
interface week {
    // Defining an abstract method
    public int day();
// Initializing an enum which
// implements the above interface
enum Day implements week {
    // Initializing the possible
    // days
    public int day()
        return ordinal() + 1;
// Main Class
public class Daycount {
    public static void main(String args[])
        System.out.println("It is day number "
                           + " of a week.");


It is day number 3 of a week.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads