-1

I have maybe a simple question.
Here two code-snippets to show what I mean:

Example 1:

public interface SomeInterface{
  public void someMethod(...);
}

Example 2:

public interface AnotherInterface{
  void anotherMethod(...);
}

So, Example 1 is completely clear to me but Example 2 isnt.

In fact, is there any difference between those two examples expect the public-modifier?

On one hand I found that methods from Interfaces are implicitly public but on the other hand I have found that methods declared in an Interface are "package-public" (I dont now if thats the correct description) - saying these are visible to all classes in the same package as the Interface.
For now I am completely confused.. So could someone please explain me whats right?

Thanks anyways.

maximus_de
  • 382
  • 1
  • 3
  • 12
  • see this answer: http://stackoverflow.com/a/161787/4249 – matt b Mar 13 '14 at 17:38
  • Also http://stackoverflow.com/questions/5418464/methods-visibility-in-interface – Thorkil Holm-Jacobsen Mar 13 '14 at 17:40
  • Thanks. Why dont just search or Google gives me this result... Now it is clear to me. There is not difference except more waste of memory... – maximus_de Mar 13 '14 at 17:43
  • Where did you find that _"methods declared in an Interface are "package-public""? Whoever posted that information is wrong (or you misunderstood what was posted). The terminology is usually "package-private" and interface methods are most certainly not package-private (or any access type other than `public`). – Ted Hopp Mar 13 '14 at 17:44
  • Regarding the term: the JLS usually uses "default access", but it uses "package-private" in at least one place. I've seen the term "package-visible" used, but not "package-public". – ajb Mar 13 '14 at 17:46

3 Answers3

1

All interface methods are public abstract and all interface fields are public static final.

So there is no difference in above examples.

Kick
  • 4,823
  • 3
  • 22
  • 29
1

It's redundant to declare it public. In particular JLS 9.4 states:

Every method declaration in the body of an interface is implicitly public (§6.6).

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

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
1

All methods in an interface are public and visible to implementing classes everywhere. But if the interface itself is package local (has no modifier - the default), then those methods are visible only to classes/interfaces within the same package. But the method would still have to be public in the implementing class

In the code you have above, there is no difference. But if it had been:

interface AnotherInterface{ // Note no modifier - default modifier applied
    void anotherMethod(...);
}

In this case, the interface is visible only inside the same package.

NOTE: It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I believe that this is incorrect. Certainly the interface itself might be package private, but every implementation of an interface method must have `public` access, making the concrete method accessible to all code (provided the implementing class is itself accessible, of course), regardless of the accessibility of the interface. – Ted Hopp Mar 13 '14 at 17:48
  • I edited the answer to reflect the public modifier. Yes, implementing classes have to use the modifier public. – ucsunil Mar 13 '14 at 17:54