Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
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?
 
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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!