Can Enum Extend Any Class in Java



If you've been coding in Java for some time, you might have wondered why you can't make an enum extend another class. It seems like it should, but Java won't let you. Let's break down why this happens

Enumeration in Java

Enumeration (enum) in Java is a user-defined datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year, and seasons, etc.

If we had to store all seven days of a week into one single variable then we might tend to use data structure which is either an array or a list but the drawback is the array and lists are mutable so during a different operation in a program the data which is stored in array might be variable so in this case we use Enum.

So, in this way, we store all seven days of the week into one single variable, which is week, and the values stored in the variable week will be constant and will not be changed.

Syntax to create an Enumeration

You can define an enumeration using the keyword enum, followed by the name of the enumeration as -

enum Week {
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Just like arrays, the elements/constants in an enumeration are identified using numbers starting from 0. In the above example, the days are identified using numbers as shown in the following illustration -

Enumerations are similar to classes, and you can have variables, methods, and constructors within them. Only concrete methods are allowed in an enumeration.

Can an enum extend a Class?

No, an enum in Java cannot extend a class because when you create an enum in Java, like -

enum Colors {
   RED, GREEN, BLUE
}

Every enum in Java implicitly (internally) extends the java.lang.Enum class. And it looks like fthe ollowing program -

final class Colors extends java.lang.Enum < Colors >{
   public static final Colors RED = new Colors("RED", 0);
   public static final Colors GREEN = new Colors("GREEN", 1);
   public static final Colors BLUE = new Colors("BLUE", 2);
}        

This happens automatically every time you create an enum. But we cannot see this in our code. As we know, Java does not allow a class to extend more than one other class, because Java does not support multiple inheritance. It supports only "single inheritance". Since enum is already extending java.lang.Enum, cannot extend another class. That's why Enum cannot extend any class in Java.

If you try to do so, a compile-time error will be generated.

Example

In the following Java snippet, we have a class named Sample, and we have created an Enum type named Scooters and tried to extend the Sample class.

import java.util.Scanner;
   class Sample {
}
   enum Scooters extends Sample {
}

Output

On executing, this class generates the following compile time error.
D:\>javac EnumExample.java
EnumExample.java:5: error: '{' expected
enum Scooters extends Sample{
^
EnumExample.java:5: error: ',', '}', or ';' expected
enum Scooters extends Sample{
                      ^
2 errors
Updated on: 2025-04-21T15:13:34+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements