Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

abstract method within an enum ?  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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 ?

 
Author and ninkuma
Marshal
Posts: 66783
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of what utility would abstract methods in an enum be?
 
Saloon Keeper
Posts: 9123
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
Java Oracle Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
THANK ALL OF YOU
 
Sheriff
Posts: 21421
94
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 9123
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, that's interesting. Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!