What are the implicit specifier/modifiers of interface methods in Java 8?

前端 未结 3 590
逝去的感伤
逝去的感伤 2021-01-03 13:44

I understand that Interface methods are implicitly public. Java Docs Tutorial says

All abstract, default, and <

3条回答
  •  不知归路
    2021-01-03 14:44

    Access modifiers

    In Java 8, we have as per Java Docs Tutorials,

    All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

    Thus the only allowed access modifier in an Interface as of Java 8 is public. (Java 9 introduces private interface methods)

    public interface TestInterface {
        int print();//compiles and no IDE warning
        public int print1();//public redundant
    }
    

    Optional Specifiers

    abstract - methods in an interface that are not declared as default or static are implicitly abstract, so the abstract modifier is optional. But the method implementation must not be provided in such case.

    static- static needs to be explicitly specified and implementation provided.

    final - A final methods can't be overridden and is not allowed for an interface method.

    default - method can be explicitly declared default if the default implementation is provided.

    All fields in Interfaces are public, static and final.

提交回复
热议问题