Can I add a function to enums in Java?

JavaFunctionEnums

Java Problem Overview


I have an enum, which looks like

public enum Animal {
  ELEPHANT,
  GIRAFFE,
  TURTLE,
  SNAKE,
  FROG
}

and I want to do something like

Animal frog = Animal.FROG;
Animal snake = Animal.SNAKE;

boolean isFrogAmphibian = frog.isAmphibian(); //true
boolean isSnakeAmphibian = snake.isAmphibian(); //false

boolean isFrogReptile = frog.isReptile(); //false
boolean isSnakeReptile = snake.isReptile(); //true

boolean isFrogMammal = frog.isMammal(); //false
boolean isSnakeMammal = snake.isMammal(); //false

I simplified the example for didactic purposes, but this would be really useful to me for my real life example. Can I do it in Java?

Java Solutions


Solution 1 - Java

Yes Enum is a class in Java:

public enum Animal 
{
  ELEPHANT(true),
  GIRAFFE(true),
  TURTLE(false),
  SNAKE(false),
  FROG(false);

  private final boolean mammal; 
  private Animal(final boolean mammal) { this.mammal = mammal; }
  public boolean isMammal() { return this.mammal; }
}

but in your case for a real system I would make that an Enum as well since there is a fixed set of types of animals.

public enum Type
{
  AMPHIBIAN,
  MAMMAL,
  REPTILE,
  BIRD
}

public enum Animal 
{
  ELEPHANT(Type.MAMMAL),
  GIRAFFE(Type.MAMMAL),
  TURTLE(Type.REPTILE),
  SNAKE(Type.REPTILE),
  FROG(Type.AMPHIBIAN);

  private final Type type; 
  private Animal(final Type type) { this.type = type; }
  public boolean isMammal() { return this.type == Type.MAMMAL; }
  public boolean isAmphibian() { return this.type == Type.AMPHIBIAN; }
  public boolean isReptile() { return this.type == Type.REPTILE; }
  // etc...
}

Also note that it is important to make any instance variable final as well.

You can find more details about it in the Java Language Specification.

Solution 2 - Java

Yes, you can. It would look like this:

public enum Animal {
  ELEPHANT(false),
  GIRAFFE(false),
  TURTLE(false),
  SNAKE(false),
  FROG(true);

  private final boolean isAmphibian;

  Animal(boolean isAmphibian) {
    this.isAmphibian = isAmphibian;
  }

  public boolean isAmphibian() {
    return this.isAmphibian;
  }
}

Then you would call it like:

Animal.ELEPHANT.isAmphibian()

Solution 3 - Java

I have an other option:

public enum Animal {
    ELEPHANT {
        @Override
        boolean isMammal() {
            return true;
        };
        @Override
        boolean isReptile() {
            return false;
        }
    },
    SNAKE {
        @Override
        boolean isMammal() {
            return false;
        };
        @Override
        boolean isReptile() {
            return true;
        }
    };

    abstract boolean isMammal();
    abstract boolean isReptile();
}

No need of external Interface and I am quite sure (did not test) it works also on Java7.

Solution 4 - Java

As well as using the techniques above which add a field to the enumerated type you can also use a pure method based approach and polymorphism. This is more "OOP style" but I would not say it is necessarily better.

Unfortunately, you may (see the comment below) need to define an interface:

public interface AnimalTraits {
	default boolean isAmphibian()	{ return false; };
	default boolean isReptile()		{ return false; };
	default boolean isMammal() 		{ return false; };
}

But then you can them implement the interface in each of your enumeration elements:

public enum Animal implements AnimalTraits {
	
	 ELEPHANT 	{ @Override public boolean isMammal() 	 { return true; } },
	 GIRAFFE  	{ @Override public boolean isMammal() 	 { return true; } },
	 TURTLE		{ @Override public boolean isReptile() 	 { return true; } },
	 SNAKE		{ @Override public boolean isReptile() 	 { return true; } },
	 FROG		{ @Override public boolean isAmphibian() { return true; } }
}

Note that I use default implementations in the interface to cut down on the amount of typing you need in the enum.

Regarding the necessity of the interface: I tried adding the methods in the interface as abstract methods at the top of the enum and Eclipse seemed to allow it and insisted on implementations in the enum elements, but then failed to compile those properly. So it looks like it ought to be possible without an interface, but perhaps it is not yet implemented in the compiler.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamuel CarrijoView Question on Stackoverflow
Solution 1 - Javauser177800View Answer on Stackoverflow
Solution 2 - JavadanbenView Answer on Stackoverflow
Solution 3 - JavaEmaborsaView Answer on Stackoverflow
Solution 4 - JavarghomeView Answer on Stackoverflow