• 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

abstract method within an enum ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

when I try to define an abstract method within an enum I get the following error :
enumtest is not abstract and does not override abstract method AbstractMetEnum() in enumtest
how to fix it ?

 
Sheriff
Posts: 67745
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
Of what utility would abstract methods in an enum be?
 
Saloon Keeper
Posts: 15440
362
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to CodeRanch!

Enums are already implicitly abstract. Each enum constant is actually an instance of an anonymous subclass of the enum. So when your enum declares an abstract method, your enum constants must implement it.
 
Greenhorn
Posts: 13
Oracle Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need this abstract method elsewhere for some reason, I believe you might accomplish the definition of the abstract method by putting it in an interface and then implementing the method once for all of the constants in your ENUM.

You must still implement the method in your enum. Java will put it in the class it generates for each of your constants.



Otherwise, I think that if you need to have different logic for each constant, the solution mentioned may work, but you may be forced to have different method names for each. You won't be able to put them in the interface, because each Enum constant will be expected to implement all of the methods in the interface.
 
Latif Ben
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THANK ALL OF YOU
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Enums are already implicitly abstract. Each enum constant is actually an instance of an anonymous subclass of the enum.


In general that's not true. If I compile, then decompile (with JAD) the following code the results show no abstract class anywhere:
Now, if I add an abstract method and implement that in the enum constants you do get an abstract class with anonymous sub class implementations:
 
Stephan van Hulst
Saloon Keeper
Posts: 15440
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, that's interesting. Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic