146

I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods.

What would be the benefit of that?

I thought that the fact that you were allowed to declare interface methods as abstract was a superfluous and harmless feature of the language that is not particularly encouraged.

Why would Eclipse support such a style, or why would someone voluntarily choose to do so?

Clarification: I am not asking why interface methods are abstract, that is obvious. I am asking why one would explicitly choose to mark them as abstract since if they're in an interface they are abstract anyway.

Jack G.
  • 3,681
  • 4
  • 20
  • 24
Uri
  • 88,451
  • 51
  • 221
  • 321

4 Answers4

150

According to the Java Language Specification, the abstract keyword for interfaces is obsolete and should no longer be used. (Section 9.1.1.1)

That said, with Java's propensity for backwards compatibility, I really doubt it will ever make a difference whether the abstract keyword is present.

j0k
  • 22,600
  • 28
  • 79
  • 90
jdmichal
  • 10,984
  • 4
  • 43
  • 42
  • 1
    That was my understanding (though I was not familiar with the specific JLS section). I'm wondering why Eclipse would offer me the option of creating an obselete marking... – Uri Mar 13 '09 at 05:16
  • Got me. Someone somewhere must of decided it was a desirable "feature" and put it in. You know, one of those wily open-source types :) – jdmichal Mar 13 '09 at 05:27
  • 18
    Although this is the top-rated answer, it is referring to the wrong section of the Specification; 9.1.1.1 is describing the `abstract` keyword on the declaration of the interface itself, not on its members. @Will's answer below is correct and also contains valid link sources. – Shaggy Frog May 18 '12 at 03:21
40

From the Java SE 7 JLS (Java Language Specification): "It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface."

For Java SE 5.0: "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."

Will
  • 6,601
  • 3
  • 31
  • 42
40

"The benefice of that" (adding abstract on interface methods declaration) in eclipse would be an old compatibility issue with jdt eclipse compiler in jdk1.3

Since 1.4, jdk libraries are no longer containing default abstract methods (on abstract classes implementing interfaces).
This is fooling the Eclipse 1.3 compiler diagnosis since their implementation is relying on their existence.
Note that Javac 1.3 would refuse altogether to perform against 1.4 libraries (using -bootclasspath option).

Since the Eclipse compiler is likely to be in 1.4 compliance level (see Workbench>Preferences>Java>Compiler>JDK Compliance), or use at least 1.3 class libraries if using 1.3 compliance mode, the presence of "abstract" is not required in most of the current eclipse projects.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
9

According to JLS methods in interfaces are abstract by default, so the keyword is redundant. Knowing this, I'd never use it to "avoid presentational clutter".

Daniel Hiller
  • 3,415
  • 3
  • 23
  • 33
  • This should be the right answer. Here's an updated [link](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) - see "note" section – mork Apr 25 '14 at 12:27
  • The JLS does not say the keyword is obsolete for methods in interfaces. It says "It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface." [JLS #9.4](http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4). – user207421 Sep 14 '15 at 07:11
  • @EJP I didn't say that the JLS stated the keyword would be obsolete, this was my personal opinion ;) BTW they note this keyword being "redundant" which is not quite the same as obsolete, in this you're right of course. Now that I know I'll edit the answer to clarify this. – Daniel Hiller Sep 14 '15 at 08:45