2

Possible Duplicate:
Java abstract interface

public interface Foo {
   abstract public void bar();
}

I guess we don't need to declare abstract as well as public in the above interface. Will the compiler catch this is as a warning or is it allowed by the compiler.

Community
  • 1
  • 1
Jason
  • 12,229
  • 20
  • 51
  • 66

6 Answers6

7

In an interface the the modifiers public and abstract are implied for methods, similarly for fields public static and final are implied. For inner classes static is implied.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
6

It is allowed. public and abstract are automatically added to every interface method.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
5

You don't have to, every interface method is implicitly abstract. It will not be a mistake to write it though.

amit
  • 175,853
  • 27
  • 231
  • 333
3

For interface methods, it is not necessary to declare public and abstract by default those are public and abstract

developer
  • 9,116
  • 29
  • 91
  • 150
3

It is not necessary but it won't hurt to write it. These modifiers are implied.

I like to do it so everything is explicit and may help other programmers that will work with your code.

rlc
  • 5,809
  • 5
  • 38
  • 46
0

You are allowed to declare abstract inside the interface. The complier can pass it.

public interface foointerface {

    abstract public void foo();

    public void bar();
}

But there is no point to declare in abstract since we would not implement or allow to implement methods inside interface.

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158