0

Possible Duplicate:
Java extend enum

What I want to make is some class, which has all methods from enum and also my own methods, like

public class enum_with_shift extends enum{
    public enum_with_shift next(){
        ...
    };
}

but you can't extend enum And I want use this to create something like that:

public class Days_of_week extends enum_with_shift{
    MONDAY, TUESDAY, ...
}

public class Months extends enum_with_shift{
    JANUARY, ...
}

How do I do that?

Community
  • 1
  • 1
CrabMan
  • 1,578
  • 18
  • 37

3 Answers3

3

Every enum has an ordinal, so it is valid to get the "next" value for all enums - not just those you are trying to use a base class for. This means you can write the following helper method:

public static <T extends Enum<T>> T getNext(T current) {
    Class<T> enumType = current.getDeclaringClass();
    T[] enumConstants = enumType.getEnumConstants(); // Similar to e.g. Day.values()

    int currentOrdinal = current.ordinal();
    int nextOrdinal = currentOrdinal + 1;
    if (nextOrdinal == enumConstants.length) { // Handle wrapping around to the beginning of the enum values
        nextOrdinal = 0;
    }

    return enumConstants[nextOrdinal];
}

public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

public enum Month  {
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}

public static void main(String... args) {
    System.out.println(getNext(Day.MONDAY)); // TUESDAY
    System.out.println(getNext(Day.SUNDAY)); // MONDAY

    System.out.println(getNext(Month.JANUARY)); // FEBRUARY
    System.out.println(getNext(Month.DECEMBER)); // JANUARY
}

If you really want to not be able to do this for all enums (perhaps it doesn't make sense for an enum that is not strictly ordered), then you can apply a marker interface, like so:

public interface Ordered {}

public enum Day implements Ordered {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

public static <T extends Enum<T> & Ordered> T getNext(T current) {
    ...
}

You can find some more details about reflectively working with enums here.

andersschuller
  • 13,509
  • 2
  • 42
  • 33
1

Since I do not know what enum_with_shift is I cannot answer your question directly.

But you are right. You cannot extend enums. The reason is that enum is just a regular class that extends java.lang.Enum. Since there is no multiple inheritance in java you cannot extend any other class.

I can suggest you either use regular classes or extend java.lang.Enum directly. Take a look on my article: http://java.dzone.com/articles/enum-tricks-dynamic-enums I gave an example how to do this.

AlexR
  • 114,158
  • 16
  • 130
  • 208
0

The answer above is correct Go to the link in the post above it will show you how to do it.

Use extends when a class has an is-a relationship it can only be one thing use implements to ensure ab is-like-a relationship to assert similar behavior is implemented "methods"

Just use them separately you dont need to extend everything you can imagine they both define a type one defines a Day and another defines states of the month. You can now use both of these enums in a third class called Date.

public enum Day {
MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY
}

public enum Month {     
JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER
}
eddiek
  • 1
  • The thing is - I want a new class, extending enum, which will have some new methods (like getting next value and like that) and to make new enums with different values from that class. – CrabMan Oct 04 '12 at 17:06