• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enum cannot be final or abstract

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Why enum cannot be final or abstract. I am getting the errors as given below
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those error messages just say that an enum must be public.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because my question is not related to comments, I removed comments. My question is "Why enum cannot be final or abstract."

Paul Clapham wrote:Those error messages just say that an enum must be public.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you ever tried to extend an enum?
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no reason provided in even Java language specification ..

just have to remember that they cannot be declared abstract or final
and enum cannot contain abstract methods also ...

hower you can declare nested enum as static .......inner enums are static by default..
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to extend enum. That's why I am looking for a reason why we cannot extend enum.

Bear Bibeault wrote:Have you ever tried to extend an enum?

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sure there will/should be a reason. It doesn't make any sense to remember so many things.

ankur trapasiya wrote:there is no reason provided in even Java language specification ..

just have to remember that they cannot be declared abstract or final
and enum cannot contain abstract methods also ...

hower you can declare nested enum as static .......inner enums are static by default..

 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because enums are already final. Declaring them as final is redundant. Declaring them as abstract is contradictory.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan, How do we know that enum is final. Can you please share API or docs

Stephan van Hulst wrote:Because enums are already final. Declaring them as final is redundant. Declaring them as abstract is contradictory.

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Can you please share API or docs



I think, we should have a look on the JLS first. In that,

Enum types (ยง8.9) must not be declared abstract; doing so will result in a compile-time error. It is a compile-time error for an enum type E to have an abstract method m as a member unless E has one or more enum constants, and all of E's enum constants have class bodies that provide concrete implementations of m. It is a compile-time error for the class body of an enum constant to declare an abstract method.

An enum type is implicitly final unless it contains at least one enum constant that has a class body. In any case, it is a compile-time error to explicitly declare an enum type to be final.


So, now your doubt is cleared?
reply
    Bookmark Topic Watch Topic
  • New Topic