2

I understand that Interface methods are implicitly public. Java Docs Tutorial says

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

Out of abstract, static and default, which modifiers are implicitly declared in Java 8.

I thought abstract was not implicitly declared anymore as Java 8 introduced default method in interfaces, but I still get a warning in Intellij IDEA.

Modifier 'abstract' is redundant for interface methods.

public interface TestInterface {
    abstract int print();  // abstract redundant ?.
    int print2(); //legal.but public or public abstract ?.
}
  • I have gone through https://stackoverflow.com/questions/20045759/what-is-the-implicit-declaration-of-interface-methods-in-java-8 and could still not clarify my doubt. – Susmitha Kundukulam Feb 20 '18 at 16:19
  • 1
    It has no method body, therefore it is `abstract`. It cannot be implicitly default, you don't provide a method body. Why is this confusing? – Elliott Frisch Feb 20 '18 at 16:28
  • Does that not mean `int print2();` gets 'implicitly' declared as abstract ? – Susmitha Kundukulam Feb 20 '18 at 16:45
  • It does. Which is why the modifier `abstract` is redundant. – Elliott Frisch Feb 20 '18 at 16:49
  • 2
    A method without implementation is abstract, whether you declare it as abstract or not. If you declare a method as default, you do need to provide an implementation, actually a *default* implementation. Besides, everything is public in an interface (except for private methods introduced in java 9). – fps Feb 20 '18 at 17:45
  • Of course, `abstract` is still implied for interfaces. Everything else would break existing code. – Holger Feb 21 '18 at 08:17

3 Answers3

6

The language spec - specifically Section 9.4, states that abstract and public are implicit.

Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface.

An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block. It is permitted, but discouraged as a matter of style, to redundantly specify the abstract modifier for such a method declaration.

This is why IntelliJ warns you about it; by the JLS, you're doing something completely redundant.

As a bonus, fields in interfaces are implicitly public static final:

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
4

In Java 7, as well in Java 8, all fields defined in an interface are ALWAYS public, static, and final. Methods are public and abstract.

Because your print() method does not have a body, it means that is an abstract method. With other words does not need to be declared explicitly abstract, that's why Intellij IDEA says is redundant.

Methods without static or default are not implicitly abstract, even if it has a body. A non-abstract method with a body that is not default or static, cannot exist in an interface.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Even if the method has got a body, the method is implicitly abstract, since it is in an interface and not explicitly declared to be default/static. – Sebin Benjamin Feb 21 '18 at 03:05
  • I agree that 'there are no methods that can have a body and be at the same time abstract'. But inside the interface, methods without static or default are implicitly abstract, even if it has a body. What I am trying to say is that method having a body or not isn't a criterion. Inside the interface, not being static/default makes it implicitly abstract. Thus `public interface Test { int print2(){} }` gives an error which says interface *abstract* methods cannot have a body. – Sebin Benjamin Feb 21 '18 at 09:46
  • @SebinBenjamin Sorry body but I need to tell you that you are wrong again. `methods without static or default are implicitly abstract, even if it has a body.` **is incorrect**. This kind of methods cannot exist within an interface at all. You are getting that error because your editor expects **only** abstract methods within an interface. Getting that error when trying to use a `non-abstract` method doesn't make it abstract. A `non-abstract` method with a body that is not default or static, cannot exist in an interface in the first place, right? – Alex Mamo Feb 21 '18 at 10:03
  • @SebinBenjamin And you error says very clearly: `abstract methods cannot have a body` and that's correct, with other words, body, I'm expecting abstract methods, what are doing? Remove the body to make it abstract or make it default or static. `An abstract method by definition has no body`. That's it! – Alex Mamo Feb 21 '18 at 10:22
  • I had tried already I can't do anything with voting until you edit your answer. "Your vote is now locked in unless this answer is edited" – Sebin Benjamin Feb 21 '18 at 11:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165554/discussion-between-sebin-benjamin-and-alex-mamo). – Sebin Benjamin Feb 21 '18 at 11:44
  • @SebinBenjamin Thanks for doing things right. Cheers! – Alex Mamo Feb 21 '18 at 11:46
  • Welcome, but I'm not sure you understood my point regarding abstract being implicit in the interface. – Sebin Benjamin Feb 21 '18 at 11:50
  • @SebinBenjamin I understood it but the error in editor says the exact same thing that I said in my answer. Cheers and happy coding! – Alex Mamo Feb 21 '18 at 11:55
0

Access modifiers

In Java 8, we have as per Java Docs Tutorials,

All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

Thus the only allowed access modifier in an Interface as of Java 8 is public. (Java 9 introduces private interface methods)

public interface TestInterface {
    int print();//compiles and no IDE warning
    public int print1();//public redundant
}

Optional Specifiers

abstract - methods in an interface that are not declared as default or static are implicitly abstract, so the abstract modifier is optional. But the method implementation must not be provided in such case.

static- static needs to be explicitly specified and implementation provided.

final - A final methods can't be overridden and is not allowed for an interface method.

default - method can be explicitly declared default if the default implementation is provided.

All fields in Interfaces are public, static and final.