How to implement an Interface using an Enum in Java
Last Updated :
15 Jul, 2025
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:
- Create an interface.
- 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
// 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.");
}
}
Output:
It is day number 3 of a week.
Similar Reads
How to Implement Multiple Inheritance by Using Interfaces in Java? Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when methods with the same signature exist in both the superclasses and subclass. On calling the method, the compiler cannot determine which class m
2 min read
Difference Between Package and Interface in Java In Java, packages and interfaces play crucial roles in organizing and structuring code. They serve different purposes and are used in distinct contexts. In this article, we will learn the concepts of the packages and interfaces in Java. syntax provides examples for each and then presents a table hig
2 min read
Enumeration Interface In Java java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable etc.) in a forward direction only and not in the backward direction. This interface has been superceded by an iterator.
3 min read
Class getInterfaces() method in Java with Examples The getInterfaces() method of java.lang.Class class is used to get the interfaces directly implemented by this entity. This entity can be a class or an interface. The method returns an array of interfaces directly implemented by this entity.Syntax: public Class<T>[] getInterfaces() Parameter:
2 min read
Class getInterfaces() method in Java with Examples The getInterfaces() method of java.lang.Class class is used to get the interfaces directly implemented by this entity. This entity can be a class or an interface. The method returns an array of interfaces directly implemented by this entity.Syntax: public Class<T>[] getInterfaces() Parameter:
2 min read
How to Create Interfaces in Android Studio? Interfaces are a collection of constants, methods(abstract, static, and default), and nested types. All the methods of the interface need to be defined in the class. The interface is like a Class. The interface keyword is used to declare an interface. public interface AdapterCallBackListener { void
4 min read