How to implement an Interface using an Enum in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday;
  
    public int day()
    {
        return ordinal() + 1;
    }
}
  
// Main Class
public class Daycount {
    public static void main(String args[])
    {
        System.out.println("It is day number "
                           + Day.Wednesday.day()
                           + " of a week.");
    }
}

chevron_right


Output:

It is day number 3 of a week.

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.