Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum class member with interface cannot find methods internally

I'm having a strange issue which I'm not sure if it's a compiler problem or my understanding of enums with interfaces. I'm using IntelliJ IDEA 12, building an Android project, and I have a class like this:

public class ClassWithEnum {
    private MyEnum myEnum;

    //Trying to access it internally here throws the error
    public boolean isActionable() {
        return myEnum.isActionable();
    }

    public enum MyEnum implements Action {
        ACTIONABLE() {
            @Override
            public boolean isActionable() { return true; }
        },
        NOT_ACTIONABLE() {
            @Override
            public boolean isActionable() { return false; }
        }
    }

    public interface Action {
        public boolean isActionable();
    }
}

Now, this was working initially, but now the compiler is complaining (and I've tried this in a brand new project as well with the same results) with the error:

java: /Users/kcoppock/Documents/Projects/EnumInterfaceTest/src/com/example/EnumInterfaceTest/ClassWithEnum.java:11: cannot find symbol
symbol  : method isActionable()
location: class com.example.EnumInterfaceTest.ClassWithEnum.MyEnum

I've done this before (enumerations with behaviors defined by an interface) with no issues. Any thoughts?

like image 890
Kevin Coppock Avatar asked Jan 28 '13 20:01

Kevin Coppock


People also ask

Can we use enum in interface?

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.

Is enum as inner class?

Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.

Can we define enum in interface C#?

You have to define the enum outside of the interface. Why are you trying to put an enum definition into an interface? Interface is kind of a contract. VB allows declaring enums (and other types) inside of an interface, but C# does not.

Can enum extend interface C#?

An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).


1 Answers

You need to implement isActionable() method in MyEnum itself. Because the method isActionable() defined inside the ACIONABLE and NOT_ACTIONABLE are local to them . So you need the global method for the MyEnum enum .

use this code instead:

public enum MyEnum implements Action {
        ACTIONABLE() {
            @Override
            public boolean isActionable() { return true; }
        },
        NOT_ACTIONABLE() {
            @Override
            public boolean isActionable() { return false; }
        };
        @Override
        public boolean isActionable() { return false;}
    }
like image 66
Vishal K Avatar answered Sep 16 '22 12:09

Vishal K