• Post Reply Bookmark Topic Watch Topic
  • New Topic

public API vs published API  RSS feed

 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between a public API and a published API? A Google search reveals that not every public API is a published API, for example, the sun.* packages are not published.

Can a public API contain public as well as protected methods? If so, why is it called public? Is it because public methods provide full public access (i.e. no restriction on public access) and protected methods provide partial public access (i.e. public access restricted to subclasses).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there's a common, generally accepted definition of the difference between these terms, so it means whatever the person or organization who is talking about those terms means with it.

Methods that are protected can be accessed by subclasses, so if you have some library with a class with protected methods, and you create a subclass of that class, you can access those protected methods. In that sense, the protected methods can be regarded as being part of the "public" (visible on the outside) API of the library.

Classes in the sun.* packages have public access because other parts of the JDK need to use those classes, but you're not supposed to use them directly in your own programs. That's probably where the term "published" comes from - you're not supposed to use unpublished API classes, they are for internal use by the JDK only.

Java has a rather simple system for controlling access to classes, interfaces, methods and member variables. There is no way to control access at the package or module level. It would be nice if you could say that for example a certain package must only be visible inside a JAR file, so that you could have internal packages. There are frameworks, such as OSGi, which offer this kind of control. In the future, Java is going to have a module system which will most likely make it possible to control access in such a way.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!