Methods visibility in interface

后端 未结 5 653
借酒劲吻你
借酒劲吻你 2020-12-15 15:16

Do all methods in an Interface has by default Public visibility mode?

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 16:04

    Just to add to other answers here: all methods are public, however, if the interface itself is package-local then effectively all methods are also package-local.

    You can therefore mix public and package-local methods, by making a package-local interface extend a public one.

    public interface P{
      void iAmPublic();
    }
    
    interface L extends P{
      void iAmPackageLocal();
    }
    

    Here L effectively has one public and one package-local method. Clients from outside the package will only see iAmPublic(), whereas ones from inside the package will see both methods.

    In the same way you can nest interfaces inside other classes to achieve even tighter method visibility.

提交回复
热议问题