Default access modifier for interface methods in Java 9?

Viewed 5278

Java 9 allows us to have private methods in interface, which means that not explicitly marking public methods is no longer superfluous.

However, is it now mandatory to do so? I hope the specification still assumes public abstract as the default modifier for methods to maintain backward compatibility with earlier source code?

2 Answers

What I was taught:

All members in an Interface are implicitly public and cannot be declared with any other access modifier, unless specified below:

  • Fields & all variables are public static final implicitly
  • method signatures, default methods, (permitted as of Java 8) declared with the 'default' modifier.
  • Static methods (permitted as of Java 8)
  • Private methods (permitted as of Java 9) both static and non-static concrete methods can be private.
  • Nested types.
  • Method bodies exist only for default, private and static methods.

Source: Tim Buschalka's Learn Programming Academy

Also a very clear but somewhat long explanation: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Related