Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there no final interfaces in Java?

Extending an interface simply adds additional operations to be defined in any implementors and cannot break any existing implementations (unlike extending a class). But it can change (EDIT 3 WHICH CONSTANTS) and hence the PERCIEVED value of constants (EDIT 2 AS SEEN BY THE IMPLEMENTATION CLASSES).

For instance, the following:

interface A {
  int TEST = 6;
}

interface B extends A {
  int TEST = 7;
}

public class InterfacesTest implements B {
  public static void main(final String[] args) {
    System.out.println(TEST);
  }
}

yields 7, when perhaps the intent of interface A was that any implementation of A contain a test field valued at 6.

If A were to be declared final we could be assured all implementations of A see the same value of test.

So does anyone understand why this isn't possible?

  • P.S.: This is NOT a duplicate of this question, I know they can't be final, I'm interested in the thinking behind the design decision that led to this outcome.

  • P.P.S.: I understand constants in interfaces is usually a bad idea, that isn't the issue here.

EDIT: Please check the revision history, the title of this question was edited in a way which did not reflect the question's intent. Sorry to everyone who answered the question I wasn't asking. Yes, interface fields are implicitly public static final, unfortunately that's not what I'm interested in at all.

EDIT 2 To be absolutely clear: this question is about why can't an interface block other interfaces from extending it (by being final or some equivalent).

like image 415
Tom Tresansky Avatar asked Oct 08 '10 19:10

Tom Tresansky


3 Answers

It is final. In fact, it's public static final even if you don't declare those. What you are doing is not overriding the constant variable; you are hiding the parent interface's version.

Check it out, the A.TEST is not overridden. It's still 6.

System.out.println(A.TEST);

like image 197
jbindel Avatar answered Sep 30 '22 18:09

jbindel


Primary purpose of interface is not being a container of constants, it's to define some APIs which to be implemented by concrete classes.

And, from a lang spec (9.4. Abstract method declarations):

Note that a method declared in an interface must not be declared final or a compile-time error occurs. However, a method declared in an interface may be implemented by a method that is declared final in a class that implements the interface.

like image 44
Victor Sorokin Avatar answered Sep 30 '22 16:09

Victor Sorokin


As I understand it, interfaces aren't supposed to place restrictions on implementations. They are supposed to define contract (API).
From this point, both method doIt and constant TEST are elements of API. We know how to use them, but we don't know what's inside.
And how exactly method doIt is implemented in InterfacesTest or what exact value constant TEST has - implementation details.

like image 41
Nikita Rybak Avatar answered Sep 30 '22 17:09

Nikita Rybak