Java 9 Interface : Why default Modifier Converted into public Modifier

352 Views Asked by At

My Question is about interface. I create an interface and define four methods: first method is a private method; second is a default method; third is a static method; and fourth is an abstract method.
After compiling this interface and checking its profile: the default method is converted into a public method, and both the static and abstract methods have a prepended public modifier. Why is this?

Code:

 interface InterfaceProfile {

    private void privateM() {   //this method is hidden
        System.out.println("private Method");
    }

    default void defaultM() {
        System.out.println("Default Method");
    }

    static void staticM() {
        System.out.println("Static Method");
    }

    void doStuff(); //by default adds the public modifier
}

InterfaceProfile class

    D:\Linux\IDE\Workspace\OCA-Wrokspace\Ocaexam\src>javap mods\com\doubt\session\InterfaceProfile.class
Compiled from "InterfaceProfile.java"
interface com.doubt.session.InterfaceProfile {
  public void defaultM();
  public static void staticM();
  public abstract void doStuff();
}
4

There are 4 best solutions below

0
On BEST ANSWER

Simple: by default, all methods in an interface are public. You can restrict that by applying private, but whenever you do not do that, that default kicks in. Thus: there is no conversion taking place at all.

Quoting the Java Language Specification:

A method in the body of an interface may be declared public or private (§6.6). If no access modifier is given, the method is implicitly public. It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

( the ability to have private methods in interfaces was introduced with Java 9, as people discovered that Java 8 default methods often created the need to have, well, private methods that such default methods could make use of, without making these helper methods publicly visible )

1
On

The fact that it's a default method doesn't make a difference. The implicit scope is public.

Here's what the tutorial says:

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

2
On

The default modifier is public, because that is how the interface declaration is defined: https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

If you are asking for the reasoning behind this, i would argue that the purpose of defining an interface is to ensure the - well - interface of all implementing classes, meaning that all implementing classes have clear contracts on which public accessable methods they need to provide.

Adding private methods to an interface does not serve this primary purpose and seems to be more of an implementation hint. Private and abstract methods were late additions to interfaces.

Related: Should methods in a Java interface be declared with or without a public access modifier?

0
On

https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

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

In effect, a class that implements an interface exposes all of the interface methods (other than private) to any other code that has visibility of the class.

It would be very confusing if a class had an interface but the methods on the interface would be visible to some code and not other. If you want to selectively expose methods, then you should probably make use of multiple interfaces.

public interface Profile {
    generalMethod() ...
}
public interface SecretProfile extends Profile {
    secretMethod() ...
}

Classes may choose to implement either of the interfaces (or even both). 3rd party code can check for the presence of the interface and know that the method is available or not.