Enums with common method implementation.
Working with JDK 1.4 , i had some typesafe enums with common methods in an abstract class. Because of some issues, i want to migrate them to 'enums' provided in JDK 5.
As enum provided in JDK 5 can neither be extended or derived from any other class, i am unable to achieve my objective.
Is there any way out, or should i continue to live with my old code?
Thanks,
Prabhaker
Well, there's always this ugly pretend-you-have-multiple-inheritance sort of construct:
public class CommonEnumUtilities
{
// the following method is the actual implementation of method foo
public static void foo(Enum e, Object... otherParams)
{
...
}
}
public interface CommonEnumInterface
{
// the following ensures that you have the method foo
public void foo(Object... otherParams);
}
public enum SomeEnum implements CommonEnumInterface
{
...;
public void foo(Object... otherParams)
{
CommonEnumUtilities.foo(this, otherParams);
}
}
I personally find this approach revolting, but I guess it's better than nothing.
tvynra at 2007-7-14 21:41:57 >
