Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum implementation inside interface - Java

I have a question about putting a Java enum in the interface. To make it clearer, please see the following code:

public interface Thing{    public enum Number{        one(1), two(2), three(3);        private int value;        private Number(int value) {             this.value = value;        }        public int getValue(){         return value;        }    }     public Number getNumber();    public void method2();    ... } 

I know that an interface consists of methods with empty bodies. However, the enum I used here needs a constructor and a method to get an associated value. In this example, the proposed interface will not just consist of methods with empty bodies. Is this implementation allowed?

I am not sure if I should put the enum class inside the interface or the class that implements this interface.

If I put the enum in the class that implements this interface, then the method public Number getNumber() needs to return the type of enum, which would force me to import the enum in the interface.

like image 903
Joey Avatar asked Mar 10 '13 04:03

Joey


People also ask

Can we have enum in interface Java?

Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. An Enum is a special datatype which is added in Java 1.5 version.

Can enum be declared inside interface?

It's perfectly legal to have an enum declared inside an interface . In your situation the interface is just used as a namespace for the enum and nothing more. The interface is used normally wherever you use it.

How do you implement interface in enum?

Enum, it can not extend any other class or enum and also any class can not extend enum. So it's clear that enum can not extend or can not be extended. But when there is a need to achieve multiple inheritance enum can implement any interface and in java, it is possible that an enum can implement an interface.

Can interface extend enum Java?

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


2 Answers

It's perfectly legal to have an enum declared inside an interface. In your situation the interface is just used as a namespace for the enum and nothing more. The interface is used normally wherever you use it.

like image 76
Jack Avatar answered Oct 02 '22 15:10

Jack


Example for the Above Things are listed below :

public interface Currency {    enum CurrencyType {     RUPEE,     DOLLAR,     POUND   }    public void setCurrencyType(Currency.CurrencyType currencyVal);  }   public class Test {    Currency.CurrencyType currencyTypeVal = null;    private void doStuff() {     setCurrencyType(Currency.CurrencyType.RUPEE);     System.out.println("displaying: " + getCurrencyType().toString());   }    public Currency.CurrencyType getCurrencyType() {     return currencyTypeVal;   }    public void setCurrencyType(Currency.CurrencyType currencyTypeValue) {     currencyTypeVal = currencyTypeValue;   }    public static void main(String[] args) {     Test test = new Test();     test.doStuff();   }  } 
like image 26
MaheshB Avatar answered Oct 02 '22 14:10

MaheshB