Should methods in a Java interface be declared with or without a public access modifier?
The JLS makes this clear:
It is permitted, but discouraged as a matter of style, to redundantly specify the
public
and/orabstract
modifier for a method declared in an interface.
Why should I declare implemented interface methods as public?
getGait()
of Camel
implements a method of the Rideable
interface. All interface methods are public
by default (even if you don't specify it explicitly in the interface definition), so all implementing methods must be public too, since you can't reduce the visibility of the interface method.
Why should we declare interface methods as public?
Interfaces are meant to define the public API of a type - and only that, not its implementation. So any method (or static member) you define in an interface is by definition public
.
Since an interface can't contain any concrete implementation, there is no way to call any member methods from within. And declaring such methods but leaving the calls to them to subclasses or totally unrelated clients would mean your type definition is incomplete and brittle. That is why if you need to define protected or package access members, you can do so in an abstract class (which may also contain implementation).
Setting the modifier 'public' in the implementation of an interface method
By default methods have an access of package-private if no access modifier is specified, which means they are public to the package and class only. However interfaces require methods to be implemented by a class using an interface to be public.
See this stack overflow post for what default access modifiers are for classes and interfaces.
Default access modifier for interface methods in Java 9?
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.
Java 9 Interface : Why default Modifier Converted into public Modifier
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 )
Are public and public final redundant for interface fields?
Are “public” and “public final” redundant for interface methods?
Yes.
All methods in an interface are implicitly public
and abstract
(but not final
).
All fields in an interface are implicitly public
, static
and final
.
The JLS states this. It also states that these modifiers can be left out.
Why? Well there are a variety of reasons:
Fields and methods are implicitly
public
because the point of an interface is to declare an ... interface that other classes can see. (If you want / need to restrict access, this is done via an access modifier on the interface itself.)Fields are
static
because if they were not you would be declaring visible instance fields on an object ... and that's bad for encapsulation.Fields are
final
because non-final fields would be another way of declaringpublic
static
fields ... which are terrible from an OO perspective.Methods are
abstract
because allowing method bodies would effectively turn interfaces into abstract classes.
Another reason for making methods abstract and fields static in an interface is that if they didn't, diamond inheritance, and inheritance of a method from two distinct interfaces would both be problematic.
But either way, this is how Java is defined, so the questions are moot ... unless you are thinking of inventing your own programming language.
Note that in Java 8, you can declare methods in an interface, using the default
modifier. And in Java 9, you can declare private
methods, in some cases. But use of the public
keyword is still redundant.
Why should I remove them?
You don't have to remove them. The Java compiler doesn't care.
You can remove them, but you don't have to remove them, unless you are trying to conform to some Java style guidelines that insist on this.
Your code will probably be more readable if you are consistent, but you could make it consistent by using the redundant modifiers everywhere; e.g. adding them rather than removing them.
Doesn't it mean that I want add method be implemented by whatever class while remove method implemented only by classes of my same package?
No it doesn't mean that. Or at least, it might mean that to you, but it won't mean that to the Java compiler, other Java tools ... or other people reading and maintaining your code. IMO, it would be ill-advised to place any meaning on the presence or absence of redundant keywords.
Related Topics
Maximum Size of Hashset, Vector, Linkedlist
Jframe.Dispose() VS System.Exit()
Looking for Java Spell Checker Library
Converting to Upper and Lower Case in Java
How to Get Http Response Code for a Url in Java
Java.Net.Connectexception :Connection Timed Out: Connect
Observer Is Deprecated in Java 9. What Should We Use Instead of It
Uninitialized Object VS Object Initialized to Null
What Is the Jvm Thread Scheduling Algorithm
What Version of Javac Built My Jar
How to Update Maven Repository in Eclipse