In Java, when should I use an abstract method in an interface?
Asked Answered
H

10

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?

Hypsography answered 29/9, 2009 at 12:54 Comment(2)
It does compile; just that the abstract modifier is redundant.Negotiate
Looks like a dupe of this question - #642036Zymolysis
J
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).

Jolson answered 29/9, 2009 at 12:58 Comment(0)
P
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.

Pitarys answered 29/9, 2009 at 12:57 Comment(1)
I wouldn't say "by default" but rather "implicitly". "By default" might be interpreted as other options exist besides "public abstract".Successive
A
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.

Ambrosane answered 29/9, 2009 at 12:59 Comment(0)
F
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...

Firstling answered 29/9, 2009 at 13:0 Comment(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.Easter
L
3

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

Leninakan answered 29/9, 2009 at 12:57 Comment(0)
E
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.

Etter answered 29/9, 2009 at 12:57 Comment(0)
A
2

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

Audley answered 29/9, 2009 at 12:57 Comment(0)
N
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

Negotiate answered 29/9, 2009 at 12:58 Comment(0)
F
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.

Foramen answered 29/9, 2009 at 13:19 Comment(0)
G
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!

Gotha answered 29/9, 2009 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.