Java
Interfaces
Implementation
Naming Convention
Programming Standards

Java Interfaces/Implementation naming convention

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

In Java, interfaces are a fundamental part of object-oriented design, serving as a contract that sets out methods that a class must implement, without mandating how the details of these methods are handled. Effective naming of interfaces and their implementations is crucial for maintaining clear, understandable, and maintainable code. Here, we explore the conventions and best practices for naming interfaces and their corresponding implementation classes in Java.

Understanding Interfaces

An interface in Java is an abstract type used to specify a behavior that classes must implement. They are abstract in that they can contain methods but these methods are usually without a body. Java uses interfaces to achieve abstraction and multiple inheritances, as Java does not allow multiple inheritances through classes.

Naming Conventions for Interfaces

The naming of an interface should clearly convey what the interface is intended to represent or the functionality it abstracts. Conventionally, interfaces are named using adjectives because interfaces are typically capabilities. For example:

  • Readable
  • Listenable
  • Filterable

In some cases, where the interface represents a capability, nouns or noun phrases might be used:

  • EventListener
  • PermissionChecker

The name should start with an uppercase letter and follow the CamelCase notation, which is a standard practice in Java naming conventions.

Naming Implementations

When naming classes that implement these interfaces, clarity and consistency should be the primary focus. Typically, implementation names are descriptive nouns or phrases that tell what the class does or what its role is in the system.

Prefix or Suffix Strategy

One common approach is to add prefixes or suffixes like Impl, Base, Default, or specific descriptive terms related to the implementation to the interface name:

  • EventListener -> FileEventListener
  • PermissionChecker -> UserPermissionChecker
  • Filterable -> ProductFilterableImpl

The use of Impl (e.g., ListenableImpl) is straightforward but often not very descriptive. It's preferable to use more descriptive names wherever possible.

Context and Functionality

Adding context or functionality-related terms can make implementations easier to distinguish and understand, especially when there are multiple implementations of the same interface:

  • If an interface is meant to provide data from a database, the implementation could be named SQLDataRetriever.
  • For another that retrieves data from an API, it might be named APIDataRetriever.

Best Practices

  • Interface names are typically nouns or adjectives: They describe behavior, like Runnable, or characteristic, like Serializable.
  • Implementations should describe how they do what they do: Use specific, descriptive names based on the implementation strategy.
  • Avoid overly generic names: Names such as Utility, Manager, or overly generic uses of Impl should be avoided as they do not provide enough information about the implementation’s behavior.

Summary Table

ElementConventionExample
InterfaceNouns/Adjectives, DescriptiveReadable, Filterable
ImplementationDescriptive, Context-specificFileEventListener, SQLDataRetriever

Additional Tips

  • Consistency is Key: Consistently following a naming convention makes your code easier to read and maintain.
  • Document the Intent: Use JavaDoc comments to explain the purpose of an interface and how an implementation relates to it.

Conclusion

Proper naming conventions in Java, especially around interfaces and implementations, are crucial for creating clear and maintainable code. The name alone can often explain what an interface or class does, aiding significantly in software comprehension and development. While there is some degree of flexibility, adhering to established norms and being descriptive can go a long way in fostering an understandable codebase.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.