views:

2898

answers:

7

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

Technically it doesn't matter of course. A class method that implements an interface is always public. But what is a better convention?

Java itself is not consequent in this. See for instance Collection vs. Comparable or Future vs. ScriptEngine.

A: 

I always write what I would use if there was no interface and I was writing a direct implementation, i.e., I would use public.

JeeBee
Would you also explicitly declare all interface methods abstract?
Dan Dyer
It's an interface, not an abstract class. As regards to 'public', it's 7 characters that you've typed by the time you think about it, big deal! And it's how it will be defined in the implementation as well, which is +1 for consistency balancing out the -1 for redundancy.
JeeBee
+6  A: 

The public modifier should be omitted in Java interfaces (in my opinion).

Since it does not add any extra information, it just draws attention away from the important stuff.

Most style-guides will recommend that you leave it out, but of course, the most important thing is to be consistent across your codebase, and especially for each interface. The following example could easily confuse someone, who is not 100% fluent in Java:

public interface Foo{
  public void MakeFoo();
  void PerformBar();
}
Rasmus Faber
Do you have a link to such a style-guide?
Bno
Consistency is by far the most important thing, and is the answer to 99% of these types of questions.
SCdF
Agreed re: consistency. Something for your coding standards documents guys :)
JeeBee
Bno: One example is the Java Language Specification, another is Checkstyle.
Rasmus Faber
+1  A: 

I would avoid to put modifiers that are applied by default. As pointed out, it can lead to inconsistency and confusion.

The worst I saw is an interface with methods declared abstract...

PhiLho
+1  A: 

Totally subjective. I ommit the redundant public modifier as it seems like clutter. As mentioned by others - consistency is the key to this decision.

Interesting to note that the C# language designers decided to enforce this. Declaring an interface method as public in C# is actually a compile error. Consistency is probably not important across languages though, so I guess this is not really directly relevant to Java.

serg10
A: 

I used declare methods with the public modifier, because it makes the code more readable, especially with syntax highlighting. In our latest project though, we used Checkstyle which shows a warning with the default configuration for public modifiers on interface methods, so I switched to ommitting them.

So I'm not really sure what's best, but one thing I really don't like is using public abstract on interface methods. Eclipse does this sometimes when refactoring with "Extract Interface".

cretzel
But only if you check the two checkboxes declare methods as public, abstract.
MetroidFan2002
+15  A: 

The JLS makes this clear:

It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.

Jon Skeet
A: 

I prefer skipping it, I am not a java guy but I read somewhere that interfaces are by default public abstract. To my surprise the book - head first design patterns is using public with interface declaration and interface methods... that made me rethink once again and I landed up on this post.

Anyways, I think redundant information should be ignored.

-PS

Pradeep Sharma