3

In Java suppose that I have interface A:

public interface A {
    // foo
}

I also have interface B:

public interface B extends A {
    // foo + bar
}

Why does interface B extend interface A and not implement interface A?

Wouldn't it make more sense if B implemented A?

Why did the language designers choose to use the word extend here? Are there any subtleties that I am missing?

2
  • 15
    Because an interface couldn't implement anything before default methods where a thing. Apr 20 '15 at 14:31
  • JLS 9.1.3. Superinterfaces and Subinterfaces: "Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends."
    – user40980
    Apr 20 '15 at 14:36
18

An interface is a contract. It does not implement anything (properly-used default methods are a small exception).

What would it mean for one interface to "implement" another, anyway? It would need to have method bodies for some or all methods on the superinterface, making it a class.

1

Interface implementation implies a finality that cannot be created by another interface - by their nature, an interface is meant to be incomplete.

The only reason you would extend an interface with another interface is if you need to change the defaults in the first one significantly, while still preserving the structure of the original.

As an example, if you have a basic vegetable-counting interface, you could implement it to count a specific type of vegetable. But if you want to change the interface so that it can distinguish between different types of beans, you'd have to extend it first, then implement it for each type of bean. Those final classes would then implement both Bean Counter and Vegetable Counter.

0

Think of extending as broadening the range of behavioural description and implementation as the concrete behaviour.

Say you have an interface quacking. This would be applicable to frogs as well as ducks. You could extend this general interface to swimming and flying which would then only be applicaple to the duck. The mechanism how a duck or frog swims is the implementation.

As @Snowman already mentioned: default methods are an exception to this concept.

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