1

As you know, in a java interface, all methods have to be defined as abstract. But when I define a method as not typing abstract, the compiler says it is okay. I know that an abstract method must not have a body. Does a method somewhere in an interface necessarily have a name abstract or not? : What i mean is, what is the difference between:

public interface blabla {

    public void aMethod();


    //or

    public abstract void aMethod();

}
HexagonSun
  • 39
  • 1
  • 6

8 Answers8

7

No, marking an interface method as abstract has no meaning and is never required.

All interface methods are implicitly abstract (and public too btw).

From the JLS:

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.

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.


Related question (+ answer with a historical reference to a statement saying that abstract was once required for interface methods):

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • my cs instructor writes all interface methods with "abstract" isn't it so interesting? – HexagonSun Mar 29 '12 at 14:04
  • 1
    Perhaps he's trying to be pedagogical and highlight that it doesn't have an implementation. To me it sounds completely unnecessary and if he ever forgets it on some interface method it would look very confusing. – aioobe Mar 29 '12 at 14:06
2

See the sample example below

interface xyz
{
void methodA();
}

Save this to xyz.java

Now compile this using javac tool

and then use the command given belo

javap xyz

the output would be

Compiled from "xyz.java"
interface xyz {
    public abstract void methodA();
}

That means when you compile an interface, compiler makes its signature to public and abstract by default.

So it is not necessary to use abstract keyword for any method of interface.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • You're answering a question on Java semantics by referring to `javac` and bytecode which doesn't make sense from a technical point of view. – aioobe Mar 29 '12 at 14:13
  • @aioobe sorry for this but can you please tell me how should I express this in other way. Just some hint. – Chandra Sekhar Mar 29 '12 at 14:14
  • The fact that `javac` compiles the source-file into something that has the abstract and public flags set, doesn't really say anything about the Java language per see. The Java Language Specification is *the only* authoritative source when it comes to answering questions regarding the language. The fact that `javac` *behaves* in a specific way may be coincidental. – aioobe Mar 29 '12 at 14:18
  • @aioobe thanks for reply. I will consider it while giving next answers. But I just wanted to tell the asker about javap tool, thats why i answered like this. – Chandra Sekhar Mar 29 '12 at 14:20
  • Good. Every Java-programmer should know about that tool! ;) – aioobe Mar 29 '12 at 14:23
0

I don't know that they have to be defined as abstract. Probably because they don't. See Oracle's tutorial.

MByD
  • 135,866
  • 28
  • 264
  • 277
0

The methods of an interface don't have to be explicitly defined as abstract because they are implicitly abstract and public as defined in the Java Language Specification §9.4. A redundant declaration is perfectly legal though.

kostja
  • 60,521
  • 48
  • 179
  • 224
0

you don't need to specify abstract (default) because within an interface it does not make sense as all the method of the interface needs to be implemented

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

All methods in an interface are abstract by definition.

You can't create an object out of an interface (e.g., using Interface i = new Interface();) so there's no difference between marking a method as abstract or not.

Any class that implements the interface needs to decide whether to implement it or to let a subclass do it. So as far as the interface is concerned, all methods are abstract by default.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

An abstract method provides no implementation. A class which has an abstract method is necessarily abstract, which means that you cannot create instances of this class. To create an instance of that class, you need to subclass and provide non-abstract overwrites for the abstract methods.

An interface never provides an implementation of its methods and it cannot be instantiated. Therefore every method of an interface is per definition abstract. You do not need to provide the keyword abstract when declaring a method in an interface. And by convention the keyword abstract is not used within an interface.

Stefan
  • 4,645
  • 1
  • 19
  • 35
0

If you forgot to put abstract keyword before interface method, Java will implicitly put public abstract keyword before it. Because all interface methods must be abstract.

Debadyuti Maiti
  • 1,099
  • 4
  • 18
  • 30