Can an interface be declared as final in Java?
No. Trying to declare an interface as final in Java results in a compilation error. This is a language design decision - Java interfaces are meant to be extendable.
Interfaces are 100% abstract and the only way to create an instance of an interface is to instantiate a class that implements it. Allowing interfaces to be final
is completely pointless.
EDIT The questions is not as outright outrageous as I first thought. A final interface is one that cannot be extended by other interfaces but can be implemented ostensibly makes sense.
I could think of one difference between a final class and a final interface. Extending a class can compromise its integrity because it contains some state. Extending an interface simply adds operations and cannot compromise the integrity of the implementation because the interface is stateless on its own.
From the Java Language Specification (Third Edition):
9.1.1.1 abstract Interfaces
Every interface is implicitly
abstract
. This modifier is obsolete and should not be used in new programs.
So, abstract
+ final
is sort of an oxymoron.
$ cat -> Test.java
final interface Test {}
$ javac Test.java
Test.java:1: illegal combination of modifiers: interface and final
final interface Test {}
^
1 error
$
Instead of Declaring Interface as a Final, We can avoid Creating an Interface object.
Point of Creating Interface to implements its methods by its subclass. If we are defeating this purpose then I feel its a baseless.
If any have any other suggestions, kindly let us know
While a final intereface would still have uses, but none of them widely considered good practice.
A final interface could be used for
- defining constants. Generally considered a bad idea.
- meta-information to be examined via reflection, e.g. a package descriptor
- grouping together many public inner classes into one file. (I only suggest this is used when cut-and-pasting some sample code which has many classes as it saves you the hassle of creating a file for each class, inner classes are implicitly static)
You can do all these things with a non-final interface and marking an interface as final would not be as useful as a comment saysing you are using an interface for an incedental purpose and why you are doing so
I tried it and apparently you can create a final interface in java. I have no idea why you would do this, but you can. This is how I did it.
Compile a non final interface. I saved the below code in FinalInterface.java. Then I compiled it.
interface FinalInterface { }
Run BCELifier on it. This created a file called FinalInterfaceCreator.java
Edit it. Look for a line similar to below and add ACC_FINAL.
_cg = new ClassGen("FinalInterface", "java.lang.Object", "FinalInterface.java", ACC_INTERFACE | ACC_ABSTRACT | ACC_FINAL , new String[] { });
Compile and run the edited FinalInterfaceCreator.java. This should overwrite the original FinalInterface.class file with a new one that is similar but final.
To test it, I created two new java files TestInterface and TestClass. TestInterface is an interface that extends FinalInterface and TestClass is a class that implements FinalInterface. The compiler refused to compile either because FinalInterface is final.
TestClass.java:2: cannot inherit from final FinalInterface
class TestClass implements FinalInterface
TestInterface.java:2: cannot inherit from final FinalInterface
interface TestInterface extends FinalInterface
In addition, I tried creating an instance of FinalInterface using dynamic proxies
class Main
{
public static void main ( String [ ] args )
{
Class < ? > ntrfc = FinalInterface . class ;
ClassLoader classLoader = ntrfc . getClassLoader ( ) ;
Class < ? > [ ] interfaces = { ntrfc } ;
java . lang . reflect . InvocationHandler invocationHandler = new java . lang . reflect . InvocationHandler ( )
{
public Object invoke ( Object proxy , java . lang . reflect . Method method , Object [ ] args )
{
return ( null ) ;
}
} ;
FinalInterface fi = ( FinalInterface ) ( java . lang . reflect . Proxy . newProxyInstance ( classLoader , interfaces , invocationHandler ) ) ;
}
}
This one compiled but did not run
Exception in thread "main" java.lang.ClassFormatError: Illegal class modifiers in class FinalInterface: 0x610
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:632)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
at Main.main(Main.java:6)
So the evidence suggests that you can create a final interface in java, but why would you want to?
Whenever you create an annotation you are creating an interface that is in some ways effectively final.