Java Enum implementing an Interface
07 February 2013
(5 / 5 ratings) - Rate this tutorial!
Share this tutorial
In this tutorial you will learn how to implement an Interface with an Enum in Java.
1. Introduction

Some developers are not aware of this fact but we can implement Interfaces with Enums in Java. This may be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. In this tutorial we will see how to do it and also go through an example use case.

Environment:

  1. Ubuntu 12.04
  2. JDK 1.7.0.09

2. The Enum

In this example we will implement a mathematical operation. The type of the operation (or the operator) will be defined as an Enum. Following next is the operator interface:

Operator.java

package com.byteslounge.enums;

public interface Operator {

  int calculate(int firstOperand, int secondOperand);
  
}

Now we define a Enum that implements this interface. In this example we will only consider the sum and the subtraction operators.

EOperator.java

package com.byteslounge.enums;

public enum EOperator implements Operator {
  
  SUM {
    @Override
    public int calculate(int firstOperand, int secondOperand) {
      return firstOperand + secondOperand;
    }
  },
  SUBTRACT {
    @Override
    public int calculate(int firstOperand, int secondOperand) {
      return firstOperand - secondOperand;
    }
  };
}

3. The operation class

Now let's define the Operation class:

Operation.java

package com.byteslounge.enums;

public class Operation {

  private int firstOperand;
  private int secondOperand;
  private EOperator operator;
  
  public Operation(int firstOperand, int secondOperand, 
                    EOperator operator) {

    this.firstOperand = firstOperand;
    this.secondOperand = secondOperand;
    this.operator = operator;
  }
  
  public int calculate(){
    return operator.calculate(firstOperand, secondOperand);
  }
  
}

The firstOperand and secondOperand properties represent the members of the operation. The result of the operation will depend on the operator Enum type property. For the result calculation we will invoke calculate method that is implemented by the Enum itself (operator.calculate(firstOperand, secondOperand)).

4. Testing

Let's define a simple class to test our example:

Main.java

package com.byteslounge.enums;

public class Main {

  public static void main (String [] args){
    
    Operation sum = new Operation(10, 5, EOperator.SUM);
    Operation subtraction = new Operation(10, 5, EOperator.SUBTRACT);
    
    System.out.println("Sum: " + sum.calculate());
    System.out.println("Subtraction: " + subtraction.calculate());
    
  }

}

When we run this test the following output will be generated:

Sum: 15
Subtraction: 5

This example source code is available for download at the end of this page.

Download source code from this tutorial
(5 / 5 ratings) - Rate this tutorial!
Share this tutorial
Comments
Javin Paul
08 February 2013
Nice post. No one can deny importance of Enum in Java programming, it's much powerful than language like C or C++. By the way I have also shared some examples at my post 10 Example of Enum in Java. Let me know how do you find it.
Reply
Kreiger
08 February 2013
Why not just
 
    System.out.println("Sum: " + SUM.calculate(10, 5));
    System.out.println("Subtraction: " + SUBTRACT.calculate(10, 5));
 
Reply
gmarques
10 February 2013
The main idea behind this example was to use an Enum as a complex object property. This way you can call business logic implemented in the Enum directly from your complex instance without checking the Enum itself (or do some kind of if-then-else spaghetti code). But of course, if you don't need to discriminate anything - and depending on the use case - it is also valid to call Enum implementation methods directly.
Reply
Add Comment
Display name
Email
Comment
 
When inserting code please use <pre class="prettyprint">
Example:
<pre class="prettyprint">
int x = 1;
</pre>
 
Please insert the following characters in the box below:
  All fields are required
 
Notify me by email on subsequent replies to this post
 
Submit Comment
Comment registered
Your comment will be approved before
showing up on this page!
Rate this tutorial
Poor
Excellent
You are rating this tutorial as:
-
Please insert the following characters in the box below:
Subscribe
Subscribe and receive the latest tutorials by email
Popular tags
Archive
 
About the author
Gonçalo Marques is a Software Engineer with 7+ years of experience in software development and architecture design. During this period his main focus was delivering software solutions both in banking and telecommunications area. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

He is also the author of the WiFi File Browser Android application: