1

I've refactored some methods to an interface and they were left with abstract modifier in the interface declaration. Something like this was in my code:

public interface TestAbstractMethod {

    public abstract void doSomething();

}

I noticed it accidentally now and I was actually surprised that the declaration wasn't marked as invalid, that it actually compiles and works.

Is abstract modifier allowed in an interface for a reason? Does this alter the interface's behavior in any way?

Dariusz
  • 21,561
  • 9
  • 74
  • 114
  • @Eran thanks for the duplicate, whatever I googled pointed me to sth like "differences between abstract class and interface"... โ€“ Dariusz Sep 14 '15 at 07:02
  • 3
    -1? Please only downvote if you think the *question* is bad. If it's a duplicate then find it and vote to close on that basis. The answer to this question is quite hard to find on Google. โ€“ Bathsheba Sep 14 '15 at 07:03
  • 1
    @Bathsheba I didn't downvote, but it wasn't hard to find the duplicate. I Googled "jls interface abstact method" in order to find the relevant jls reference, and the 4th result was the duplicate question. โ€“ Eran Sep 14 '15 at 07:07

1 Answers1

2

Yes, this is allowed, but its use is discouraged in the Java Language Specification, ยง9.4:

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109