2

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();
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49

4 Answers4

4

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.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 2
    @GhostCat Good point :-) I believe though that the JLS is at times overwhelming for some levels of questions (the standard of acceptable quality answers would be too harsh if all answers were to be measured like that). With that said, you have a very valid point - assuming bug-free implementations of the language specification ;-) – ernest_k Feb 06 '19 at 15:36
2

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 )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

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?

BestGuess
  • 221
  • 2
  • 5
  • 1
    "*Private and abstract methods were late additions to interfaces.*" You surely did not mean `abstract` here, as before Java 8, interfaces only had `abstract` methods. – Holger Feb 08 '19 at 19:19
  • @Holger: You are correct. What i meant to say is that interfaces can now also declare implemented methods similar to abstract classes. – BestGuess Feb 11 '19 at 09:41
1

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.

Derek Hulley
  • 334
  • 1
  • 4