218

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?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228

9 Answers9

476

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

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 3
    but abstract itself is not obsolete, or? It is obsolete for interfaces but there are still abstract classes and methods. – user85421 Aug 26 '11 at 09:52
  • 1
    Thanks ;-) I think I finally nailed down the origin for even allowing `abstract` in front of interface methods. – aioobe Aug 26 '11 at 09:54
  • 15
    Wow. So it is obsolete "by design". Those JLS designers really were always so afraid of breaking something, even breaking things that never got released... :-) – Lukas Eder Aug 26 '11 at 10:03
  • @aioobe, you must be the Google web crawler we don't know about...lol – Buhake Sindi Aug 26 '11 at 10:19
  • 21
    Btw. "public" is also not necessary on methods in an interface declaration... they are always public. – rec Jul 25 '13 at 20:40
  • I tried removing `public` from my interface in Java 1.8 and it broke my client code... It seems to me that if you omit the `public` keyword then the interface will be implicitly declared as *package private*. – Jonathan Benn Dec 23 '21 at 19:39
  • @JonathanBenn Omitting `public` in a class will make it _package private_, but in an interface, it will still be public. Some IDEs will even provide lint suggesting you remove the redundant keyword `public` in that case. – cdgraham Aug 12 '22 at 16:49
  • @cdgraham, unless Jonathan talks about 'public' in front of 'interface'. – aioobe Aug 13 '22 at 05:07
  • @aioobe that's true. His response came right after rec's response which specified "on methods" but Jonathan does seem to be talking about the interface itself, not methods in it. – cdgraham Aug 14 '22 at 14:20
  • Hi all, sorry for the confusion. I was talking about the declaration of the `interface` itself. As in `public interface Foo` vs `interface Foo`. The latter is definitely _package private_ in Java 1.8. I believe that I got confused in reading @rec 's response and didn't realize they were talking about interface _methods_ only. I confirm that you don't need the `public` keyword for interface _methods_ as they seem to be accessible to anyone that has access to the interface. – Jonathan Benn Aug 22 '22 at 13:07
38

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.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
  • 9
    to JLS: It is permitted, but strongly discouraged, as a matter of style, to redundantly write two sentences with the same meaning and almost exact wording next to each other... – n611x007 Nov 28 '13 at 10:57
12

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656
9

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)

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Adi Lev
  • 91
  • 1
  • 2
  • +1 good hit, i looked widly for a seperation of non-passivateable sessionbean injections. Maybe i can use findbugs/checkstyle for a rule.... – Grim Sep 25 '14 at 18:00
3

Every interface is implicitly abstract.
This modifier is obsolete and should not be used in new programs.

[The Java Language Specification - 9.1.1.1 abstract Interfaces]

Also note that interface member methods are implicitly public abstract.
[The Java Language Specification - 9.2 Interface Members]

Why are those modifiers implicit? There is no other modifier (not even the 'no modifier'-modifier) that would be useful here, so you don't explicitly have to type it.

kapex
  • 28,903
  • 6
  • 107
  • 121
2

It isn't necessary. It's a quirk of the language.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
2

It's not necessary, as interfaces are by default abstract as all the methods in an interface are abstract.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Swagatika
  • 3,376
  • 6
  • 30
  • 39
-2

An abstract Interface is not as redundant as everyone seems to be saying, in theory at least.

An Interface can be extended, just as a Class can. If you design an Interface hierarchy for your application you may well have a 'Base' Interface, you extend other Interfaces from but do not want as an Object in itself.

Example:

public abstract interface MyBaseInterface {
    public String getName();
}

public interface MyBoat extends MyBaseInterface {
    public String getMastSize();
}

public interface MyDog extends MyBaseInterface {
    public long tinsOfFoodPerDay();
}

You do not want a Class to implement the MyBaseInterface, only the other two, MMyDog and MyBoat, but both interfaces share the MyBaseInterface interface, so have a 'name' property.

I know its kinda academic, but I thought some might find it interesting. :-)

It is really just a 'marker' in this case, to signal to implementors of the interface it wasn't designed to be implemented on its own. I should point out a compiler (At least the sun/ora 1.6 I tried it with) compiles a class that implements an abstract interface.

Eurospoofer
  • 614
  • 10
  • 7
  • 3
    I believe you absolutely misunderstood my question. – Buhake Sindi Jul 25 '13 at 20:04
  • 3
    I don't agree with this reasoning. I think every interface must provide a fully usable functionality set and therefore every interface can be implemented on its own. Also there would be no reason for a compiler to refuse to compile a class implementing an interface declared explicitely as abstract, because all interfaces are already implicitely abstract. That would change the meaning of the "abstract" keyword entirely. – BladeCoder May 19 '14 at 13:16
  • One day a `newbie` will write a Class that implements `MyBaseInterface`. You will find it hard to judge the `marker`. – Sany Liew Aug 05 '22 at 05:04
-3

Well 'Abstract Interface' is a Lexical construct: http://en.wikipedia.org/wiki/Lexical_analysis.

It is required by the compiler, you could also write interface.

Well don't get too much into Lexical construct of the language as they might have put it there to resolve some compilation ambiguity which is termed as special cases during compiling process or for some backward compatibility, try to focus on core Lexical construct.

The essence of `interface is to capture some abstract concept (idea/thought/higher order thinking etc) whose implementation may vary ... that is, there may be multiple implementation.

An Interface is a pure abstract data type that represents the features of the Object it is capturing or representing.

Features can be represented by space or by time. When they are represented by space (memory storage) it means that your concrete class will implement a field and method/methods that will operate on that field or by time which means that the task of implementing the feature is purely computational (requires more cpu clocks for processing) so you have a trade off between space and time for feature implementation.

If your concrete class does not implement all features it again becomes abstract because you have a implementation of your thought or idea or abstractness but it is not complete , you specify it by abstract class.

A concrete class will be a class/set of classes which will fully capture the abstractness you are trying to capture class XYZ.

So the Pattern is

Interface--->Abstract class/Abstract classes(depends)-->Concrete class
user207421
  • 305,947
  • 44
  • 307
  • 483
Manish
  • 99
  • 8
  • 1
    This answer doesn't answer my question at all. Also `"It seams like you are new to Java`. Really? – Buhake Sindi Nov 29 '14 at 10:58
  • "abstract is obsolete" – Manish Nov 29 '14 at 15:45
  • (1) "abstract is obsolete" -- > if they remove it and the compiler stops recognizing it then the previous version of source code using "abstract interface" wont compile in newer version .. They need to maintain backward compatibility. When you define abstract interface the meaning of interface keyword is stuck along with it "abstract" which is much cleaner version however for experienced programmers like you they provided the shortcut "interface" .. your question is similar to i=i+1 ==> i++ .. choice is yours what you choose :D – Manish Nov 29 '14 at 16:05
  • Look at the accepted answer. I know `abstract` is obsolete in interface. I wanted to know why is it still acceptable and what is the history behind `abstract` interface. You're giving me a Java 101 guide on abstract vs interface. – Buhake Sindi Nov 29 '14 at 19:40
  • It is not a lexical construct. It is syntax. Semantically it is redundant. It is not 'required by the compiler'. The part about space/time is just drivel. None of this nonsense answers the question that was asked. Don't use code formatting for text that isn't code. – user207421 Apr 29 '15 at 03:45