Java abstract interface

120,666

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)

Solution 5

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.

Share:
120,666

Related videos on Youtube

Buhake Sindi
Author by

Buhake Sindi

First person in South Africa to receive a Gold Java Badge (Received on 5th November 2011) and 71st overall on StackOverflow. Whoohoo! :-) I also founded Devoxx4Kids South Africa to promote technology education among children. Where can you find me? Sindi Technologies (Founder) My Blog (A Developer's Enterprise Log...) GitHub Twitter LinkedIn Google+ SoundCloud #SOreadytohelp The following shows the experiences I have with computer programming languages & various frameworks. It is not a reflection of who I am. Professional experience: Enterprise Java (Jakarta EE/ Java EE, Spring Framework) JavaScript Frameworks: (Angular 1.x), ReactJS (JavaScript/TypeScript), jQuery VB .NET SQL (RDBMS independent) Web Technologies: HTML/XHTML/HTML 5, JavaScript, JavaScript/jQuery/Angular 1.x and higher --> CSS (from version 1 to 3), including Bootstrap Framework External Experience: Pascal / Delphi VB C/C++ Assembly language Adobe Flex PHP Scala Go (Briefly played with)

Updated on October 29, 2020

Comments

  • Buhake Sindi
    Buhake Sindi over 3 years

    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?

    • hansvb
      hansvb over 12 years
    • aioobe
      aioobe over 12 years
      Not a duplicate considering the "Finally: ...." part.
    • Raedwald
      Raedwald over 12 years
      This related question quotes a real example: stackoverflow.com/questions/4380796/…
    • ModdyFire
      ModdyFire about 10 years
      And why does Eclipse add 'abstract' by default when you 'extract interface'?
    • Buhake Sindi
      Buhake Sindi about 10 years
      @ModdyFire, please elaborate?
  • hansvb
    hansvb over 12 years
    Apparently, the methods are even public if the interface itself is package-private.
  • user85421
    user85421 over 12 years
    but abstract itself is not obsolete, or? It is obsolete for interfaces but there are still abstract classes and methods.
  • aioobe
    aioobe over 12 years
    Thanks ;-) I think I finally nailed down the origin for even allowing abstract in front of interface methods.
  • Lukas Eder
    Lukas Eder over 12 years
    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... :-)
  • Buhake Sindi
    Buhake Sindi over 12 years
    @aioobe, you must be the Google web crawler we don't know about...lol
  • Buhake Sindi
    Buhake Sindi almost 11 years
    I believe you absolutely misunderstood my question.
  • rec
    rec almost 11 years
    Btw. "public" is also not necessary on methods in an interface declaration... they are always public.
  • n611x007
    n611x007 over 10 years
    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...
  • BladeCoder
    BladeCoder almost 10 years
    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.
  • Grim
    Grim over 9 years
    +1 good hit, i looked widly for a seperation of non-passivateable sessionbean injections. Maybe i can use findbugs/checkstyle for a rule....
  • Buhake Sindi
    Buhake Sindi over 9 years
    This answer doesn't answer my question at all. Also "It seams like you are new to Java. Really?
  • Manish
    Manish over 9 years
    "abstract is obsolete"
  • Manish
    Manish over 9 years
    (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
  • Buhake Sindi
    Buhake Sindi over 9 years
    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.
  • user207421
    user207421 about 9 years
    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.
  • Jonathan Benn
    Jonathan Benn over 2 years
    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.