• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract method within an enum ?  RSS feed

 
Latif Ben
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 ?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • 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.
 
KayCee Clark
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
 
Rob Spoor
Sheriff
Posts: 21135
87
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: 7973
143
  • 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!