Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend enum class from abstract class?

Tags:

java

Having something like this:

public enum Token {      FOO("foo", "f"),      QUIT("quit", "q"),      UNKNOWN("", "");      ...       public parse(String s) {          for (Token token : values()) {               ...               return token;          }          return UNKNOWN;      } } 

An abstract class:

abstract class Base  {     private boolean run;      Base() {         run = true;          while (run) {              inp = getInput();              act(inp);         }     }      public boolean act(String s) {         boolean OK = true;         switch (Token.parse(inp)) { /* Enum */              case FOO:                     do_foo();                     break;              case QUIT:                     run = false;                     break;              case UNKNOWN:                     print "Unknown" + inp;                     OK = false;                     break;              }          }          return OK;     } } 

And the extender:

class Major extends Base {  } 

What I want is to extend act as in if super does not handle it then try to handle it in Major. E.g. add PRINT_STAT("print-statistics", "ps") - but at the same time let the Base class handle defaults like QUIT.

Is this a completely wrong approach?

What I have done so far is add an interface Typically:

public interface BaseFace {       public boolean act_other(String inp); } 

And in class Base implements BaseFace:

      case UNKNOWN:           OK = act_other(inp); 

And in class Major:

  public boolean act_other(String inp) {         if (inp.equals("blah")) {             do_blah();             return true;         }        return false;   } 

Does this look like a usable design?

And, major question:

Is there some good way to extend the Token class such that I can use the same switch approach in Major as in Base? What I wonder is if there for one is a better design and second if I have to make a new Token class for Major or if I somehow can extend or otherwise re-use the existing.


Edit: Point of concept is to have the Base class that I can easily re-use in different projects handling various types of input.

like image 335
Zimzalabim Avatar asked Mar 16 '13 15:03

Zimzalabim


People also ask

Can an enum extend an abstract class?

Can Enum extend a class in java? Enum cannot extend any class in java,the reason is, by default Enum extends abstract base class java.

Can we extend enum class?

No, we cannot extend an enum in Java. Java enums can extend java. lang. Enum class implicitly, so enum types cannot extend another class.

Can abstract class have enum?

Enum . What you can do, is declare an interface in the base class which the subclass' enum will implement. But since you didn't specify any functionality in your enumerations that could be abstracted, there is no point in doing it (yet).

Which class does all the enum extend?

Which class does all the Enums extend? Explanation: All enums implicitly extend java.


1 Answers

All enums implicity extend Enum. In Java, a class can extend at most one other class.

You can, however, have your enum class implement an interface.

From this Java tutorial on Enum Types:

Note: All enums implicitly extend java.lang.Enum. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else.

Edit for Java 8:

As of Java 8, an interface can include default methods. This allows you to include method implementations (but not state) in interfaces. Although the primary purpose of this capability is to allow evolution of public interfaces, you could use this to inherit a custom method defining a common behavior among multiple enum classes.

However, this could be brittle. If a method with the same signature were later added to the java.lang.Enum class, it would override your default methods . (When a method is defined both in a class's superclass and interfaces, the class implementation always wins.)

For example:

interface IFoo {     public default String name() {         return "foo";     } }  enum MyEnum implements IFoo {     A, B, C }  System.out.println( MyEnum.A.name() );  // Prints "A", not "foo" - superclass Enum wins 
like image 116
Andy Thomas Avatar answered Oct 01 '22 18:10

Andy Thomas