Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do ALL methods of an interface have to be public? Why no package access?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really wanted to make a public interface where some methods have package-only access, but it promotes those methods to public. Why?
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer the JLS

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.


The above simply says all interface methods are implicitly public and abstract, if you want defult access you might have to change some compiler code.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly because an interface is supposed to represent a single concept or functionality. If you've got multiple protection levels, then in some sense you've got at least two concepts.

Note that although interface methods have to be public, neither the interface nor the classes that implement it have to be public. If you have package-protected classes that implement a package-protected interface, then those public methods really aren't accessible outside of the package. So what you can do is split your interface into a public one and a package-level one, and have your implementation classes be package-protected and handed out using a factory, if necessary, so that code outside your package can only see the public interface part of the class.

Make sense?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abdulla Mamuwala:

The above simply says all interface methods are implicitly public and abstract


Although all interface methods are implicitly public and abstract, this passage says nothing of the sort. What this passage says is that interfaces implicitly include declarations of the set of public instance methods of java.lang.Object. True, and good, but utterly unrelated to this question.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earnest is right my quoting the JLS for your question is absoultely unrelated. Sorry for the wrong explanation. Thank you very much Earnest for providing such a wonderful exxplaination.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!