wood burning stoves 2.0
The moose likes Java in General and the fly likes enum extending abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "enum extending abstract class" Watch "enum extending abstract class" New topic

enum extending abstract class

Udayan Kumar
Ranch Hand

Joined: Jan 16, 2007
Posts: 66
Hi All

The current jdk feature of Enums does not allow the enum to inherit an abstract class. I am aware about having an abstract method inside the enum and the enum instances providing implementation for the abstract enum method. But I am trying to find a solution using Enum/abstract class workaround to the below mentioned scenario.
The reason I ask this question is I don't want to break our existing design which has an abstract class and only one abstract method and the rest of the methods are implemented methods. This abstract method is to be overriden by the derived classes. So they also inherit the complete methods defined by the parent class.
I don't want to spawn multiple derived classes but keep the implementation concise in an Enum so all the instances in enum will override the abstract method from the aforementioned abstract class.
How can I achieve this?


Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Udayan an enum is just like a class. Every enum extends the Enum class so they cannot extend any other class. You can achieve what you want by declaring static final fields in your class which has one abstract method or create class and use static final implementations using anonymous inner classes in it like this

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

Can't you simply do that with enums?

How To Ask Questions How To Answer Questions
I agree. Here's the link: http://aspose.com/file-tools
subject: enum extending abstract class
It's not a secret anymore!