5

I have the following interface in Java

public interface IFoo
{
    public abstract void foo();
    public void bar();
}

What is the difference between foo() and bar()? When should I use abstract?

Both seem to accomplish what I want unless I'm missing something subtle?

Update Duplicate of Why would one declare a Java interface method as abstract?

Community
  • 1
  • 1
John Oxley
  • 14,698
  • 18
  • 53
  • 78
  • 1
    It does compile; just that the abstract modifier is redundant. – Anoop Sep 29 '09 at 12:59
  • 3
    Looks like a dupe of this question - http://stackoverflow.com/questions/641536/why-would-one-declare-a-java-interface-method-as-abstract – Brian Kelly Sep 29 '09 at 13:05

10 Answers10

18

There isn't any functional difference. No implementation is ever provided in a java interface so all method declarations are implicitly abstract.

See [1]: http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

A direct quote form the above:

Note: All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).

Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
17

Interface methods are both public and abstract by default. There's no difference between foo() and bar() and you can safely remove all public and abstract keywords.

Martin Blech
  • 13,135
  • 6
  • 31
  • 35
  • 4
    I wouldn't say "by default" but rather "implicitly". "By default" might be interpreted as other options exist besides "public abstract". – matt b Sep 29 '09 at 13:19
5

You're not missing anything. From the Java Language Specification:

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

Every method declaration in the body of an interface is implicitly public.

In other words, you can omit the public as well as the abstract on the interface methods.

janko
  • 4,123
  • 28
  • 26
4

It's redundant (there's no difference between the two declarations) and explicitly discouraged in section 9.4 of the Java Language specification:

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.

And public is similarly unnecessary and discouraged:

It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.

I don't know why public is strongly discouraged but abstract is just discouraged...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Just speculation... but for the abstract keyword - it's redundant and pretty obvious - there's no implementation there. It's easy to recognize if you know the general concept of interfaces. If the public modifier were specified it might lead someone who doesn't know public is the default (and only) option to think they can change the access modifier to something else. – Nate Sep 29 '09 at 13:10
3

Both accomplish the same, since all methods in an interface are abstract.

dstibbe
  • 1,589
  • 18
  • 33
2

abstract in this scenario is unnecessary (as is marking methods in interfaces as public).

You would instead use this on a method in an abstract class in order to enforce its overriding by a subclass.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

There no are difference between declarations. Conceptually, all methods in an interface are abstract.

Andre Pastore
  • 2,841
  • 4
  • 33
  • 44
2

It makes no difference. All the methods in a java interface are always abstract.

See the first note at this link : http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Anoop
  • 1,307
  • 1
  • 14
  • 27
0

Both foo() and bar() are abstract as they are declared inside an interface. The abstract word you used here is of no significance - you better remove it.

0

Interfaces in java are equal "somehow" to a fully abstract classes, so adding the "abstract" keyword to a method declaration in an interface has nothing to do with the method!

Firas
  • 277
  • 4
  • 11