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?
The Java 9 Language Specification says in §9.4::
A method in the body of an interface may be declared
public
orprivate
(§6.6). If no access modifier is given, the method is implicitlypublic
. It is permitted, but discouraged as a matter of style, to redundantly specify thepublic
modifier for a method declaration in an interface.
Unfortunately, I can't find a link that does not lead to a PDF, diffing the old and new JLS.
What I was taught:
All members in an Interface are implicitly public and cannot be declared with any other access modifier, unless specified below:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With