Java abstract interface
Consider an example (which compiles in java)
public abstract interface Interface {
public void interfacing();
public abstract boolean interfacing(boolean really);
}
Why is it necessary for an interface to be "declared" abstract? Is there other rules that applies with an abstract interface?
Finally: If abstract
is obsolete, why is it included in Java? Is there a history for abstract interface?
Solution 1:
Why is it necessary for an interface to be "declared" abstract?
It's not.
public abstract interface Interface {
\___.__/
|
'----> Neither this...
public void interfacing();
public abstract boolean interfacing(boolean really);
\___.__/
|
'----> nor this, are necessary.
}
Interfaces and their methods are implicitly abstract
and adding that modifier makes no difference.
Is there other rules that applies with an abstract interface?
No, same rules apply. The method must be implemented by any (concrete) implementing class.
If abstract is obsolete, why is it included in Java? Is there a history for abstract interface?
Interesting question. I dug up the first edition of JLS, and even there it says "This modifier is obsolete and should not be used in new Java programs".
Okay, digging even further... After hitting numerous broken links, I managed to find a copy of the original Oak 0.2 Specification (or "manual"). Quite interesting read I must say, and only 38 pages in total! :-)
Under Section 5, Interfaces, it provides 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, I suspect that abstract
was at some point mandatory for interface methods!
Related article: Java: Abstract interfaces and abstract interface methods
Solution 2:
It's not necessary, it's optional, just as public
on interface methods.
See the JLS on this:
http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html
9.1.1.1 abstract Interfaces Every interface is implicitly abstract. This modifier is obsolete and should not be used in new programs.
And
9.4 Abstract Method Declarations
[...]
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.
Solution 3:
It is not necessary to declare the interface abstract.
Just like declaring all those methods public (which they already are if the interface is public) or abstract (which they already are in an interface) is redundant.
No one is stopping you, though.
Other things you can explicitly state, but don't need to:
- call super() on the first line of a constructor
extends Object
- implement inherited interfaces
Is there other rules that applies with an abstract interface?
An interface is already "abstract". Applying that keyword again makes absolutely no difference.
Solution 4:
Be aware that in Spring it has non academic meaning.
The abstract interface is a warning to the developer not to use it for @Autowired
.
I hope that spring/eclipse @Autowired
will look at this attribute and warn/fail about usages of such.
A real example: @Service proxy under @Transnational to a @Repository need to use same basic methods however they should use different interfaces that extends this abstract interface due to @Autowired
.
(I call this XXXSpec interface)