Java tutorial says I can have a package-private interface, but I can't

半城伤御伤魂 提交于 2019-12-17 22:22:38

问题


In the Java tutorial "Defining an Interface", it says

If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

However, this

interface PPInterface {
    void foo();
    void bar();
}

class NewClass implements PPInterface {
    void foo() {}
    void bar() {}
}

generates compiler errors in NewClass because I am 'attempting to assign weaker access privileges; was public'. So the documentation is wrong, or I did something wrong, or I misinterpreted the documentation?

I suppose I don't have to use an interface-- I like it because it keeps things nicely organized.


回答1:


It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods. The docs are not incorrect, because using the methods defined in the interface is not the same as using the interface itself.

Also be aware that when defining an interface, not adding public before a method definition doesn't change anything since the methods are all implicitly public.

If the class(es) that you have implementing the interface are themselves package-private, the publicness of the interface methods is obviously not an issue. You could also, of course, use an abstract class instead of an interface if the single-inheritance issue doesn't get in your way:

abstract class Whatever {
  abstract void foo();
  abstract void bar();
}



回答2:


I think (though I could be wrong about this) that the weaker access privileges being discussed here are for the foo() and bar() methods in NewClass. All interface methods are implicitly public, but in NewClass you've left them package-private, which is a weaker guarantee than public. Changing NewClass to read

class NewClass implements PPInterface{
    public void foo() {}
    public void bar() {}
}

probably will fix this.




回答3:


what worked for me to get around the single-inheritance problem:

Instead of A extends B implements C

I have abstract D (package protected interface in C) extends B

and then A extends D

Works fine. Clean, too, tbh.



来源:https://stackoverflow.com/questions/4801642/java-tutorial-says-i-can-have-a-package-private-interface-but-i-cant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!