- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can Enum extend any class in java?
Enumeration (enum) in Java is a 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 etc.
You can define an enumeration using the keyword enum followed by the name of the enumeration as −
enum Days { 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.
Extending a class
All enumerations internally extend a class named Enum the base class of all the language enumeration types. Since Java doesn’t support multiple inheritance you cannot extend another class with enumeration if you try to do so, a compile time error will be generated.
Example
In the following java snippet we have a class with name 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
- Related Articles
- Can we extend an enum in Java?
- Can you extend a static inner class in Java?
- Can we define an enum inside a class in Java?
- Enum in a class in Java
- Can we extend interfaces in Java? Explain?
- Extend data class in Kotlin
- Can an interface in Java extend multiple interfaces?
- Can an interface extend multiple interfaces in Java?\n
- What happens if we try to extend a final class in java?
- Can Enum implements an interface in Java?\n
- Enum in Java
- Can we create an enum with custom values in java?
- Can we define an enum inside a method in Java?
- How to extend Interfaces in Java
- Can we have variables and methods in an enum in Java?
