开发者

Can You Write an Interface that Can Not be implemented?

This is related to fina开发者_开发百科l interface in java. Among the discussion there was that the concept of final in relation to interfaces is ambiguous. Would a final interface mean that it can not have subinterfaces? Would it mean that it can not have implementations?

This question is the first: can you write an final interface such that the compiler will prevent you from implementing it?


As I will show, it is possible to implement the interface above using a proxy. The more meaningful question is, why would you try to make a unimplementable interface? Even as a philosophical point, it seems rather shallow.

import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

class NoFinal
{
    public static void main(String[] a) throws Throwable
    {
        FinalInterface o = (FinalInterface) Proxy.newProxyInstance(FinalInterface.class.getClassLoader(), new Class[]{FinalInterface.class}, new InvocationHandler()
        {
            public Object invoke(Object proxy, Method method, Object[] args)
            {
                System.out.println(method);
                return null;
            }
        });
        Method[] methods = FinalInterface.class.getDeclaredMethods();
        methods[0].invoke(o, new Object[]{null});
        methods[1].invoke(o, new Object[]{null});
    }
}

This won't give an error at compile or run-time, and it shows you can make a real instance of this interface with both methods callable.


I submit the following as an interface such that implementation will be prevented at compile-time.

interface FinalInterface
{
    class Types
    {
        private class Alpha { }

        private class Beta { }
    }

    void method ( Types . Alpha param ) ;

    void method ( Types . Beta param ) ;
}


Technically, you can, by specifying parameters that are not accessible to implementors - for example in the same package as the interface, with package-private visibility.

But that does not make any sense. This interface is completely useless in this case.

Update: One use comes into my mind, but this should not be done. If you want to use interfaces for constant definitions, in order to spare the bunch of modifier that an interface assumes, but you don't want to use the anti-pattern of implementing a constants-only interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜