1

Super Interface Definition:

package mypackage1;

public Interface MySuperInterface{

  public enum MY_APPLICATION_SPECIFIC_ENUM{

     APP_CONS1 ,

     APP_CONS2

    };

}

Sub Interface Definition:

package mypackage2;

public Interface MySubInterface extends My SuperInterface{

/* sub interface members */

}

Demo class using sub interface:

package mypackage3;

import mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM;

class MyDemoClass{

MY_APPLICATION_SPECIFIC_ENUM myAppEnum = MY_APPLICATION_SPECIFIC_ENUM.APP_CONS1;


}

It gives the compilation Error:

import requires canonical name for mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM

Could you please help on how to import the enum classes of super interface through sub interface.

Yubaraj
  • 3,800
  • 7
  • 39
  • 57
Peddi
  • 92
  • 1
  • 9
  • You do not need to import the enum. Just the interface that contains it. Try import mypackage2.MySubInterface; – Vincent Ramdhanie Oct 30 '13 at 16:23
  • @VincentRamdhanie I don't think that's true. The only way to import the enum directly is from its containing interface. ie. `import mypackage1.MySuperInterface.MY_APPLICATION_SPECIFIC_ENUM;`. Importing MySubInterface is not sufficient. – Chill Oct 30 '13 at 16:28
  • No. Just importing the interface does not work. It gives the following Error: cannot find class MY_APPLICATION_SPECIFIC_ENUM – Peddi Oct 30 '13 at 16:30
  • Think of the enclosing interface like the package that the enum belongs to. That's basically how Java identifies it. It doesn't make sense for MySubInterface to somehow inherit the actual enum because otherwise packages would work a lot differently. The package `com` would inherit everything in `com.java`, `com.apache`, `com.google`, etc. They would inherit from their subpackages and in the end, `com` and `org` would need to have pretty much every class/interface/enum that exists. – Chill Oct 30 '13 at 16:32
  • Furthermore, you can have a different enum called MY_APPLICATION_SPECIFIC_ENUM in MySubInterface. It could have different members. At that point, the behavior would be pretty confusing when referencing MySubInterface.MY_APPLICATION_SPECIFIC_ENUM if it worked the way you want it to. – Chill Oct 30 '13 at 16:34
  • import static mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM.*; does not work. Gives the compilation error. Correct answer is import static mypackage2.MySubInterface.*; – Peddi Oct 30 '13 at 17:02

2 Answers2

0

You can make a static import to access your enum values in a direct way :

import static mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM.*;

You will then be able to use directly the values of your enum in the class/interface that makes the import.

Another way to do so is :

import mypackage2;
...
// Use of the interface's enum by it's real name
MySubInterface.MY_APPLICATION_SPECIFIC_ENUM myMethod(Object anyParam);
Julien
  • 2,544
  • 1
  • 20
  • 25
  • import static mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM.*; does not work. Gives the compilation error. Correct answer is import static mypackage2.MySubInterface.*; (OR) import static mypackage2.MySubInterface.MY_APPLICATION_SPECIFIC_ENUM; – Peddi Oct 30 '13 at 17:07
  • Or use the complete name (which is the best choice I think) as I've written in my solution :) – Julien Oct 30 '13 at 17:21
0

You trying to reference a canonical name for the enum of where it does not exists. The enum is created in your interface MySuperInterface. All variables of an interface are static and final variables (i.e. constants).

Static variables are not inherited as they belong to the class they declared in. To reference the enum variable you would have to do the following:

package mypackage3;
import mypackage.MySuperInterface.MY_APPLICATION_SPECIFIC_ENUM;

class MyDemoClass{

MY_APPLICATION_SPECIFIC_ENUM myAppEnum = MY_APPLICATION_SPECIFIC_ENUM.APP_CONS1;
}

Here is link to where it's discussed why variables in interfaces static and final:

Why are interface variables static and final by default?

Community
  • 1
  • 1
Carlos
  • 358
  • 2
  • 10