5

going through the web I always read something like

Java Interfaces have no state and no behavior

If you look what the common definition of state is then you likely end with

what the objects have, Student have a first name, last name, age, etc

The thing is I guess since Java 8 with their default methods I can also define some fields and use them as well as functions.

So I wonder if it's still valid to say that an interface in Java has no state and no behavior?

public interface DefaultInterface {

    List<Integer> numbers = new LinkedList<>();

    default List<Integer> getNumbers() {
        return numbers;
    }

    default void addId(int value) {
        numbers.add(value);
    }

    default IntStream perform(String string) {
        return string.chars();
    }

    void implementMe();
}

3 Answers 3

5

For a long time, Java interfaces could only be "pure" interfaces without default implementation, state, behavior. Default interface implementation is a fairly recent language feature. It appeared in Java 8 (2014).

The intent behind interfaces with default implementation was to allow adding methods to an interface without having to change the existing classes which implement that interface. The new methods get default implementations (while the old methods likely remain without default implementations), and the existing classes which implement the interface don't have to implement new methods.

Interfaces with default implementation could also be used like abstract classes for multiple [implementation] inheritance.

Let's look at interfaces in other languages.

  • C++ doesn't have a special interface keyword. Pure interfaces (without a default implementation) are declared with class keyword and pure virtual functions.

  • C# does have a special interface keyword, but no default implementation for interfaces. So, a type declared with interface can be only a pure interface without implementation.

4
  • 2
    note that the feature's in discussion for a future version of C# github.com/dotnet/csharplang/issues/288
    – Pac0
    Commented May 5, 2019 at 23:44
  • Abstract classes are not interfaces. They bind you into a class hierarchy. Interfaces don't. Two totally unrelated Java, Objective-C or Swift classes can implement the same interface (called protocol in Objective-C or Swift).
    – gnasher729
    Commented May 6, 2019 at 19:09
  • @gnasher729 that's just called multiple inheritance. The keyword interface gives you that in languages that need it. But c++ has multiple inheritance for it's abstract classes just fine. They ARE interfaces. They don't need no stinking keyword. The interface keyword is a hack for languages that couldn't get inheritance right the first time around. Commented May 11, 2019 at 14:51
  • @gnasher729 I would argue that abstract classes with only purevirtual functions are interfaces as long as the language has multiple inheritance. However, multiple inheritance can become confusing when used with classes which do have state (diamond inheritance). That's the main reason languages like Objective-C and Java opted for concepts like protocols and interfaces, respectively, which support multiple inheritance but no state.
    – joki
    Commented May 11, 2019 at 20:12
5

One thing that may not be clear here is that this definition:

List<Integer> numbers = new LinkedList<>();

Is implicitly static and final in the context of an interface whereas it would be implicitly a member declaration in any (other) class level context. I think it would be fair to say that this is a bit confusing. This is one reason that it's a good idea to set the syntax coloring in your IDE to show static (and/or static-final) declarations as a different color, if possible.

So the state here is associated with the interface class and not instances of this. So the question "is it valid to say that an interface in Java has no state" is a little unclear. Yes, the interface class can have state but that's probably not what most people are thinking when they talk about state e.g. 'field' typically would refer to a member level reference and not a class level unless you specify 'static field'

2

With the addition of default methods, interfaces indeed have behavior. However, this may not be true for state (depending on the definition of state):

The List<Integer> numbers = new LinkedList<>(); in your interface has the implicit modifiers public static final, meaning that numbers is not a field in any implementing class, but is a "static/class constant" and not part of the state of any object that is an instance of your interface. As any state (e.g. properties and fields) is usually an implementation detail, it does not belong on the interface definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.