Methods visibility in interface

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

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

相关标签:
5条回答
  • 2020-12-15 15:39

    All methods in an interface default to public.

    See Java Language Specification 6.6.1 which states

    All members of interfaces are implicitly public.

    0 讨论(0)
  • 2020-12-15 15:41

    Yes, all methods in an interface are implicitly public and abstract.

    Check Java language specification chapter 9.4

    0 讨论(0)
  • 2020-12-15 15:54

    All interface methods ARE public abstract, all interface fields are public static final...

    see here.

    0 讨论(0)
  • 2020-12-15 16:01

    Yes, all methods of an interface are public, and can't have any other access modifier (i.e. the default public access modifier is the only valid access modifier)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题