Explain Codes LogoExplain Codes Logo

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

java
best-practices
code-readability
java-8
Alex KataevbyAlex Kataev·Nov 30, 2024
TLDR

All methods in a Java interface are inherently public. Writing public is just like telling a fish it's in water; it's redundant. For concrete methods like default or static, we need explicit modifiers. Here's an example:

interface MyInterface { void myMethod(); // Like a fish in water, 'public' is optional default void defaultMethod() {} // Must declare 'default', like gravity, it's a must! }

<b>Bottom line:</b> It is a common practice to omit public for interface methods since it is their default scope.

To include 'public' or not: What does JLS say?

The Java Language Specification (JLS) prefers lean code. Since interface methods can operate perfectly fine without the "public" modifier, its declaration is seen as superfluous. Essentially, reducing unnecessary components allows us to focus on the meat and potatoes of the code.

interface Simplified { void clearMethod(); // Clear as a sunny sky, no 'public' needed }

Consistency over chaos: A developer's mantra

Consistency is the key to maintaining readability and long-term sanity. In the past, interfaces like Comparable and Collection followed varied conventions. But now, it's widely accepted to omit the public keyword for simplicity and uniformity. Plus, it makes your code look like professionally tailored suit instead of a patchwork quilt.

Java interfaces: An evolutionary saga

From Java 8 onwards, interfaces started lifting weights and got pumped with default and static methods. These changes dish up some food for thought regarding explicit modifiers, especially when they are required for non-abstract methods. Essentially, you need to know when to spice things up for clarity and functionality.

Making peace with 'public'-less interfaces

Holding back from adding public might feel like giving up your favourite junk food. However, Trimming your interfaces to their essentials aligns with modern code-as-craft recommendations.

Public modifier: More harm than good?

Overdressing your interfaces with excess modifiers is not just about aesthetics—it can mislead and confuse. Redundant public declarations might give the false impression that it's mandatory, or even worse, methods can be non-public, pulling a magic trick on standard Java semantics.

Setting and following clear conventions

Java's standard libraries have their share of inconsistencies, leading to valid confusion. However, they are not the Bible of coding conventions. Stick to a clean, minimalist style for defining interface methods, prioritising maintainability and readability.

The 'public' balancing act

When you're on the fence about including public, weigh the costs and benefits. Take into account the project's size, the team's background, and the coding standards you've adopted. Ultimately, you want your interface to be as legible as a children's book.