Java: Abstract interfaces and abstract interface methods
You may have noticed that it's possible to mark interfaces and interface methods as abstract
. This is completely redundant and in fact discouraged.
public abstractNeither this… interface Printable {
…nor this, makes any differenceabstract void print();
}
JLS states the following:
Every interface is implicitly abstract.
This modifier is obsolete and should not be used in new programs. JLS §9.1.1.1
and
An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.JLS §9.4
So why is it allowed?
This is a good question. Even the first edition of JLS says
This modifier is obsolete and should not be used in new Java programs. JLS, First Edition, §9.1.2.1
To look even further back in time we need to refer to the Oak language which is the predecessor of Java. And indeed the original Oak 0.2 spec in Section 5, Interfaces has the following example
public interface Storing {
void freezeDry(Stream s) = 0;
void reconstitute(Stream s) = 0;
}
And in the margin it says
In the future, the "=0" part of declaring methods in interfaces may go away.
Assuming =0
got replaced by the abstract
keyword, abstract
was presumably mandatory in some prehistoric version of Java.
Comments (3)
Is there a similar reason why it is allowed but redundant to declare interface methods as public?
by Andreas Sjöberg |
Similar, but it doesn't seem like public
has ever been required for interface methods though. The examples in the Oak spec doesn't put public
in interface method declarations. Same with interface constants which are implicitly static. The examples in the Oak spec doesn't include the static
modifier.
Nice info.
I was not expecting that this were allowed 'till today, specially because the interfaces are already abstract And I didn't know that abstract modifier is "deprecated".
by Isidro Vásquez |