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).
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):