1. Introduction

An interface in Java is a core concept typically used to define a contract, implement polymorphism, or simply create a new layer of abstraction. Naming interfaces is an essential step in developing more readable code.

In this tutorial, we’ll look at the naming conventions for interfaces in Java.

2. The Java Naming Convention for Interfaces

First, let’s remember the essential purposes of an interface in Java. If we explore the source code of Java internal libraries, we’ll encounter two main use cases.

It’s typically used as a capability for other classes or to implement polymorphism. In both cases, Java has a naming convention.

Exceptionally, functional interfaces don’t follow a particular naming pattern, so we’ll not discuss them here.

2.1. Interface as a Capability

A capability defines what an object can do if it implements the interface. In that case, the convention is that the interface name is an adjective.

The Comparable interface is a good example of a capability that follows the adjective convention:

public interface Comparable<T>

One Comparable implementation is the Integer class, which has the following signature:

public final class Integer implements Comparable<Integer>

That means that Integer defines an object able to be compared with another Integer.

Other examples of interfaces named as adjectives are the Runnable and Callable interfaces. They define the capability of an object to be used as a task in multi-thread programming.

2.2. Interface as a Polymorphic Type

Another broadly used pattern in Java libraries is polymorphism. One way to achieve it is by using an interface as a supertype with two or more implementations.

When implementing polymorphism, the convention is that the interface name is a noun. Additionally, their subclasses’ names combine their specialty and the interface name. To illustrate that, let’s look at the List interface definition:

public interface List<E> extends Collection<E>

Now, let’s have a look at the signature of one polymorphic implementation of it, the LinkedList:

public class LinkedList<E> implements List<E>

Let’s glance at another implementation of List, the ArrayList:

public class ArrayList<E> implements List<E>

Noticeably, both classes contain the word List in their names. Additionally, their specialty names are Array and Linked, based on internal implementation. Thus the names ArrayList and LinkedList.

The Connection interface, used to define a database connection type, is another example in Java libraries that follows that convention.

3. Exercising the Java Naming Convention

To illustrate the usage of that convention, we’ll use a polymorphic User type capable of being identified. Thus, let’s first define the Identifiable interface:

public interface Identifiable {
    void identify();
}

Now, let’s create the User interface that inherits from Identifiable:

public interface User extends Identifiable {
    void authorize();
}

The User is divided into two implementations, the RegularUser and the RootUser.

Let’s first create the RegularUser class:

public class RegularUser implements User {
    @Override
    public void identify() {
        // some implementation
    }

    @Override
    public void authorize() {
        // some implementation
    }
}

Then, let’s define another implementation of User, the RootUser class:

public class RootUser implements User {
    @Override
    public void identify() {
        // some implementation
    }

    @Override
    public void authorize() {
        // some implementation
    }
}

Noticeably, we’ve used the Java convention to name our interfaces. The capability interface Identifiable is named as an adjective. The polymorphic type User and its subtypes, RootUser and RegularUser, are named as nouns.

This convention makes the code more fluent to read. It’s evident in the interface names that a user is identifiable and that it can be a root or a regular user. The code is closer to plain English, which improves readability.

4. Examples of Incorrect Interface Naming

Another widespread pattern in enterprise Java applications comes from the Hungarian Notation. In this section, we’ll briefly talk about the preceding I and the Impl suffix patterns and why we should avoid them.

The preceding I pattern suggests that any interface name starts with the capital letter I, an abbreviation for Interface. This is more common in C# code, where we don’t have a keyword to differentiate interface implementation from inheritance. However, this wouldn’t be necessary in Java since we can differentiate implementation from inheritance by looking at the keywords implements and extends.

The Impl suffix pattern suggests naming the interface’s implementations instead of the interface itself. Hence, all implementation names end with Impl. This usually appears when we create an interface with a single implementation and we can’t find a name that truly represents its specialization. However, the word Impl doesn’t add anything since the class signature shows it’s implementing something.

Therefore, we must avoid naming interfaces and classes using the I or Impl patterns, for example, UserImpl, IUser, IIdentifiable, and IdentifiableImpl. 

5. Conclusion

In this tutorial, we’ve looked at different naming conventions for interfaces.

The Java convention translates interface names to something closer to plain English. This helps to improve the code readability and maintainability.

Naming conventions are a matter of taste and company standards. However, Java applications align more with the Java internal libraries convention.

As always, the source code is available over on GitHub.

Course – LS (cat=Java)
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.